Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * ObjectCollectionTest file
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Utility
15
 * @since         CakePHP(tm) v 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ObjectCollection', 'Utility');
20
App::uses('CakeEvent', 'Event');
21
 
22
/**
23
 * A generic object class
24
 */
25
class GenericObject {
26
 
27
/**
28
 * Constructor
29
 *
30
 * @param GenericObjectCollection $collection
31
 * @param array $settings
32
 */
33
	public function __construct(GenericObjectCollection $collection, $settings = array()) {
34
		$this->_Collection = $collection;
35
		$this->settings = $settings;
36
	}
37
 
38
}
39
 
40
/**
41
 * First Extension of Generic Object
42
 */
43
class FirstGenericObject extends GenericObject {
44
 
45
/**
46
 * A generic callback
47
 */
48
	public function callback() {
49
	}
50
 
51
}
52
 
53
/**
54
 * Second Extension of Generic Object
55
 */
56
class SecondGenericObject extends GenericObject {
57
 
58
	public function callback() {
59
	}
60
 
61
}
62
 
63
/**
64
 * Third Extension of Generic Object
65
 */
66
class ThirdGenericObject extends GenericObject {
67
 
68
	public function callback() {
69
	}
70
 
71
}
72
 
73
/**
74
 * A collection of Generic objects
75
 */
76
class GenericObjectCollection extends ObjectCollection {
77
 
78
/**
79
 * Loads a generic object
80
 *
81
 * @param string $object Object name
82
 * @param array $settings Settings array
83
 * @return array List of loaded objects
84
 */
85
	public function load($object, $settings = array()) {
86
		list(, $name) = pluginSplit($object);
87
		if (isset($this->_loaded[$name])) {
88
			return $this->_loaded[$name];
89
		}
90
		$objectClass = $name . 'GenericObject';
91
		$this->_loaded[$name] = new $objectClass($this, $settings);
92
		$enable = isset($settings['enabled']) ? $settings['enabled'] : true;
93
		if ($enable === true) {
94
			$this->enable($name);
95
		}
96
		return $this->_loaded[$name];
97
	}
98
 
99
}
100
 
101
class ObjectCollectionTest extends CakeTestCase {
102
 
103
/**
104
 * setUp
105
 *
106
 * @return void
107
 */
108
	public function setUp() {
109
		parent::setUp();
110
		$this->Objects = new GenericObjectCollection();
111
	}
112
 
113
/**
114
 * tearDown
115
 *
116
 * @return void
117
 */
118
	public function tearDown() {
119
		parent::tearDown();
120
		unset($this->Objects);
121
	}
122
 
123
/**
124
 * test triggering callbacks on loaded helpers
125
 *
126
 * @return void
127
 */
128
	public function testLoad() {
129
		$result = $this->Objects->load('First');
130
		$this->assertInstanceOf('FirstGenericObject', $result);
131
		$this->assertInstanceOf('FirstGenericObject', $this->Objects->First);
132
 
133
		$result = $this->Objects->loaded();
134
		$this->assertEquals(array('First'), $result, 'loaded() results are wrong.');
135
 
136
		$this->assertTrue($this->Objects->enabled('First'));
137
 
138
		$result = $this->Objects->load('First');
139
		$this->assertSame($result, $this->Objects->First);
140
	}
141
 
142
/**
143
 * test unload()
144
 *
145
 * @return void
146
 */
147
	public function testUnload() {
148
		$this->Objects->load('First');
149
		$this->Objects->load('Second');
150
 
151
		$result = $this->Objects->loaded();
152
		$this->assertEquals(array('First', 'Second'), $result, 'loaded objects are wrong');
153
 
154
		$this->Objects->unload('First');
155
		$this->assertFalse(isset($this->Objects->First));
156
		$this->assertTrue(isset($this->Objects->Second));
157
 
158
		$result = $this->Objects->loaded();
159
		$this->assertEquals(array('Second'), $result, 'loaded objects are wrong');
160
 
161
		$result = $this->Objects->loaded();
162
		$this->assertEquals(array('Second'), $result, 'enabled objects are wrong');
163
	}
164
 
165
/**
166
 * Tests set()
167
 *
168
 * @return void
169
 */
170
	public function testSet() {
171
		$this->Objects->load('First');
172
 
173
		$result = $this->Objects->loaded();
174
		$this->assertEquals(array('First'), $result, 'loaded objects are wrong');
175
 
176
		$result = $this->Objects->set('First', new SecondGenericObject($this->Objects));
177
		$this->assertInstanceOf('SecondGenericObject', $result['First'], 'set failed');
178
 
179
		$result = $this->Objects->set('Second', new SecondGenericObject($this->Objects));
180
		$this->assertInstanceOf('SecondGenericObject', $result['Second'], 'set failed');
181
 
182
		$this->assertEquals(2, count($result));
183
	}
184
 
185
/**
186
 * creates mock classes for testing
187
 *
188
 * @return void
189
 */
190
	protected function _makeMockClasses() {
191
		if (!class_exists('TriggerMockFirstGenericObject')) {
192
			$this->getMock('FirstGenericObject', array(), array(), 'TriggerMockFirstGenericObject', false);
193
		}
194
		if (!class_exists('TriggerMockSecondGenericObject')) {
195
			$this->getMock('SecondGenericObject', array(), array(), 'TriggerMockSecondGenericObject', false);
196
		}
197
		if (!class_exists('TriggerMockThirdGenericObject')) {
198
			$this->getMock('ThirdGenericObject', array(), array(), 'TriggerMockThirdGenericObject', false);
199
		}
200
	}
201
 
202
/**
203
 * test triggering callbacks.
204
 *
205
 * @return void
206
 */
207
	public function testTrigger() {
208
		$this->_makeMockClasses();
209
		$this->Objects->load('TriggerMockFirst');
210
		$this->Objects->load('TriggerMockSecond');
211
 
212
		$this->mockObjects[] = $this->Objects->TriggerMockFirst;
213
		$this->mockObjects[] = $this->Objects->TriggerMockSecond;
214
 
215
		$this->Objects->TriggerMockFirst->expects($this->once())
216
			->method('callback')
217
			->will($this->returnValue(true));
218
		$this->Objects->TriggerMockSecond->expects($this->once())
219
			->method('callback')
220
			->will($this->returnValue(true));
221
 
222
		$this->assertTrue($this->Objects->trigger('callback'));
223
	}
224
 
225
/**
226
 * test trigger and disabled objects
227
 *
228
 * @return void
229
 */
230
	public function testTriggerWithDisabledObjects() {
231
		$this->_makeMockClasses();
232
		$this->Objects->load('TriggerMockFirst');
233
		$this->Objects->load('TriggerMockSecond');
234
 
235
		$this->mockObjects[] = $this->Objects->TriggerMockFirst;
236
		$this->mockObjects[] = $this->Objects->TriggerMockSecond;
237
 
238
		$this->Objects->TriggerMockFirst->expects($this->once())
239
			->method('callback')
240
			->will($this->returnValue(true));
241
		$this->Objects->TriggerMockSecond->expects($this->never())
242
			->method('callback')
243
			->will($this->returnValue(true));
244
 
245
		$this->Objects->disable('TriggerMockSecond');
246
 
247
		$this->assertTrue($this->Objects->trigger('callback', array()));
248
	}
249
 
250
/**
251
 * test that the collectReturn option works.
252
 *
253
 * @return void
254
 */
255
	public function testTriggerWithCollectReturn() {
256
		$this->_makeMockClasses();
257
		$this->Objects->load('TriggerMockFirst');
258
		$this->Objects->load('TriggerMockSecond');
259
 
260
		$this->mockObjects[] = $this->Objects->TriggerMockFirst;
261
		$this->mockObjects[] = $this->Objects->TriggerMockSecond;
262
 
263
		$this->Objects->TriggerMockFirst->expects($this->once())
264
			->method('callback')
265
			->will($this->returnValue(array('one', 'two')));
266
		$this->Objects->TriggerMockSecond->expects($this->once())
267
			->method('callback')
268
			->will($this->returnValue(array('three', 'four')));
269
 
270
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
271
		$expected = array(
272
			array('one', 'two'),
273
			array('three', 'four')
274
		);
275
		$this->assertEquals($expected, $result);
276
	}
277
 
278
/**
279
 * test that trigger with break & breakOn works.
280
 *
281
 * @return void
282
 */
283
	public function testTriggerWithBreak() {
284
		$this->_makeMockClasses();
285
		$this->Objects->load('TriggerMockFirst');
286
		$this->Objects->load('TriggerMockSecond');
287
 
288
		$this->mockObjects[] = $this->Objects->TriggerMockFirst;
289
		$this->mockObjects[] = $this->Objects->TriggerMockSecond;
290
 
291
		$this->Objects->TriggerMockFirst->expects($this->once())
292
			->method('callback')
293
			->will($this->returnValue(false));
294
		$this->Objects->TriggerMockSecond->expects($this->never())
295
			->method('callback');
296
 
297
		$result = $this->Objects->trigger(
298
			'callback',
299
			array(),
300
			array('break' => true, 'breakOn' => false)
301
		);
302
		$this->assertFalse($result);
303
	}
304
 
305
/**
306
 * test that trigger with modParams works.
307
 *
308
 * @return void
309
 */
310
	public function testTriggerWithModParams() {
311
		$this->_makeMockClasses();
312
		$this->Objects->load('TriggerMockFirst');
313
		$this->Objects->load('TriggerMockSecond');
314
 
315
		$this->mockObjects[] = $this->Objects->TriggerMockFirst;
316
		$this->mockObjects[] = $this->Objects->TriggerMockSecond;
317
 
318
		$this->Objects->TriggerMockFirst->expects($this->once())
319
			->method('callback')
320
			->with(array('value'))
321
			->will($this->returnValue(array('new value')));
322
 
323
		$this->Objects->TriggerMockSecond->expects($this->once())
324
			->method('callback')
325
			->with(array('new value'))
326
			->will($this->returnValue(array('newer value')));
327
 
328
		$result = $this->Objects->trigger(
329
			'callback',
330
			array(array('value')),
331
			array('modParams' => 0)
332
		);
333
		$this->assertEquals(array('newer value'), $result);
334
	}
335
 
336
/**
337
 * test that setting modParams to an index that doesn't exist doesn't cause errors.
338
 *
339
 * @expectedException CakeException
340
 * @return void
341
 */
342
	public function testTriggerModParamsInvalidIndex() {
343
		$this->_makeMockClasses();
344
		$this->Objects->load('TriggerMockFirst');
345
		$this->Objects->load('TriggerMockSecond');
346
 
347
		$this->mockObjects[] = $this->Objects->TriggerMockFirst;
348
		$this->mockObjects[] = $this->Objects->TriggerMockSecond;
349
 
350
		$this->Objects->TriggerMockFirst->expects($this->never())
351
			->method('callback');
352
 
353
		$this->Objects->TriggerMockSecond->expects($this->never())
354
			->method('callback');
355
 
356
		$this->Objects->trigger(
357
			'callback',
358
			array(array('value')),
359
			array('modParams' => 2)
360
		);
361
	}
362
 
363
/**
364
 * test that returning null doesn't modify parameters.
365
 *
366
 * @return void
367
 */
368
	public function testTriggerModParamsNullIgnored() {
369
		$this->_makeMockClasses();
370
		$this->Objects->load('TriggerMockFirst');
371
		$this->Objects->load('TriggerMockSecond');
372
 
373
		$this->mockObjects[] = $this->Objects->TriggerMockFirst;
374
		$this->mockObjects[] = $this->Objects->TriggerMockSecond;
375
 
376
		$this->Objects->TriggerMockFirst->expects($this->once())
377
			->method('callback')
378
			->with(array('value'))
379
			->will($this->returnValue(null));
380
 
381
		$this->Objects->TriggerMockSecond->expects($this->once())
382
			->method('callback')
383
			->with(array('value'))
384
			->will($this->returnValue(array('new value')));
385
 
386
		$result = $this->Objects->trigger(
387
			'callback',
388
			array(array('value')),
389
			array('modParams' => 0)
390
		);
391
		$this->assertEquals(array('new value'), $result);
392
	}
393
 
394
/**
395
 * test order of callbacks triggering based on priority.
396
 *
397
 * @return void
398
 */
399
	public function testTriggerPriority() {
400
		$this->_makeMockClasses();
401
		$this->Objects->load('TriggerMockFirst');
402
		$this->Objects->load('TriggerMockSecond', array('priority' => 5));
403
 
404
		$this->mockObjects[] = $this->Objects->TriggerMockFirst;
405
		$this->mockObjects[] = $this->Objects->TriggerMockSecond;
406
 
407
		$this->Objects->TriggerMockFirst->expects($this->any())
408
			->method('callback')
409
			->will($this->returnValue('1st'));
410
		$this->Objects->TriggerMockSecond->expects($this->any())
411
			->method('callback')
412
			->will($this->returnValue('2nd'));
413
 
414
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
415
		$expected = array(
416
			'2nd',
417
			'1st'
418
		);
419
		$this->assertEquals($expected, $result);
420
 
421
		$this->Objects->load('TriggerMockThird', array('priority' => 7));
422
		$this->mockObjects[] = $this->Objects->TriggerMockThird;
423
		$this->Objects->TriggerMockThird->expects($this->any())
424
			->method('callback')
425
			->will($this->returnValue('3rd'));
426
 
427
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
428
		$expected = array(
429
			'2nd',
430
			'3rd',
431
			'1st'
432
		);
433
		$this->assertEquals($expected, $result);
434
 
435
		$this->Objects->disable('TriggerMockFirst');
436
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
437
		$expected = array(
438
			'2nd',
439
			'3rd'
440
		);
441
		$this->assertEquals($expected, $result);
442
 
443
		$this->Objects->enable('TriggerMockFirst');
444
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
445
		$expected = array(
446
			'2nd',
447
			'3rd',
448
			'1st'
449
		);
450
		$this->assertEquals($expected, $result);
451
 
452
		$this->Objects->disable('TriggerMockThird');
453
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
454
		$expected = array(
455
			'2nd',
456
			'1st'
457
		);
458
		$this->assertEquals($expected, $result);
459
 
460
		$this->Objects->enable('TriggerMockThird', false);
461
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
462
		$expected = array(
463
			'2nd',
464
			'1st',
465
			'3rd'
466
		);
467
		$this->assertEquals($expected, $result);
468
 
469
		$this->Objects->setPriority('TriggerMockThird', 1);
470
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
471
		$expected = array(
472
			'3rd',
473
			'2nd',
474
			'1st'
475
		);
476
		$this->assertEquals($expected, $result);
477
 
478
		$this->Objects->disable('TriggerMockThird');
479
		$this->Objects->setPriority('TriggerMockThird', 11);
480
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
481
		$expected = array(
482
			'2nd',
483
			'1st'
484
		);
485
		$this->assertEquals($expected, $result);
486
 
487
		$this->Objects->enable('TriggerMockThird');
488
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
489
		$expected = array(
490
			'2nd',
491
			'1st',
492
			'3rd'
493
		);
494
		$this->assertEquals($expected, $result);
495
 
496
		$this->Objects->setPriority('TriggerMockThird');
497
		$result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
498
		$expected = array(
499
			'2nd',
500
			'1st',
501
			'3rd'
502
		);
503
		$this->assertEquals($expected, $result);
504
	}
505
 
506
/**
507
 * test normalizeObjectArray
508
 *
509
 * @return void
510
 */
511
	public function testnormalizeObjectArray() {
512
		$components = array(
513
			'Html',
514
			'Foo.Bar' => array('one', 'two'),
515
			'Something',
516
			'Banana.Apple' => array('foo' => 'bar')
517
		);
518
		$result = ObjectCollection::normalizeObjectArray($components);
519
		$expected = array(
520
			'Html' => array('class' => 'Html', 'settings' => array()),
521
			'Bar' => array('class' => 'Foo.Bar', 'settings' => array('one', 'two')),
522
			'Something' => array('class' => 'Something', 'settings' => array()),
523
			'Apple' => array('class' => 'Banana.Apple', 'settings' => array('foo' => 'bar')),
524
		);
525
		$this->assertEquals($expected, $result);
526
 
527
		// This is the result after Controller::_mergeVars
528
		$components = array(
529
			'Html' => null,
530
			'Foo.Bar' => array('one', 'two'),
531
			'Something' => null,
532
			'Banana.Apple' => array('foo' => 'bar')
533
		);
534
		$result = ObjectCollection::normalizeObjectArray($components);
535
		$this->assertEquals($expected, $result);
536
	}
537
 
538
/**
539
 * tests that passing an instance of CakeEvent to trigger will prepend the subject to the list of arguments
540
 *
541
 * @return void
542
 */
543
	public function testDispatchEventWithSubject() {
544
		$this->_makeMockClasses();
545
		$this->Objects->load('TriggerMockFirst');
546
		$this->Objects->load('TriggerMockSecond');
547
 
548
		$this->mockObjects[] = $this->Objects->TriggerMockFirst;
549
		$this->mockObjects[] = $this->Objects->TriggerMockSecond;
550
 
551
		$subjectClass = new Object();
552
		$this->Objects->TriggerMockFirst->expects($this->once())
553
			->method('callback')
554
			->with($subjectClass, 'first argument')
555
			->will($this->returnValue(true));
556
		$this->Objects->TriggerMockSecond->expects($this->once())
557
			->method('callback')
558
			->with($subjectClass, 'first argument')
559
			->will($this->returnValue(true));
560
 
561
		$event = new CakeEvent('callback', $subjectClass, array('first argument'));
562
		$this->assertTrue($this->Objects->trigger($event));
563
	}
564
 
565
/**
566
 * tests that passing an instance of CakeEvent to trigger with omitSubject property
567
 * will NOT prepend the subject to the list of arguments
568
 *
569
 * @return void
570
 */
571
	public function testDispatchEventNoSubject() {
572
		$this->_makeMockClasses();
573
		$this->Objects->load('TriggerMockFirst');
574
		$this->Objects->load('TriggerMockSecond');
575
 
576
		$this->mockObjects[] = $this->Objects->TriggerMockFirst;
577
		$this->mockObjects[] = $this->Objects->TriggerMockSecond;
578
 
579
		$subjectClass = new Object();
580
		$this->Objects->TriggerMockFirst->expects($this->once())
581
			->method('callback')
582
			->with('first argument')
583
			->will($this->returnValue(true));
584
		$this->Objects->TriggerMockSecond->expects($this->once())
585
			->method('callback')
586
			->with('first argument')
587
			->will($this->returnValue(true));
588
 
589
		$event = new CakeEvent('callback', $subjectClass, array('first argument'));
590
		$event->omitSubject = true;
591
		$this->assertTrue($this->Objects->trigger($event));
592
	}
593
 
594
}