統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Utility / ObjectCollectionTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (17.018 KB)

1 635eef61 spyder1211
<?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 A collection.
31
 * @param array $settings 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
 * @return void
49
 */
50
        public function callback() {
51
        }
52
53
}
54
55
/**
56
 * Second Extension of Generic Object
57
 */
58
class SecondGenericObject extends GenericObject {
59
60
/**
61
 * @return void
62
 */
63
        public function callback() {
64
        }
65
66
}
67
68
/**
69
 * Third Extension of Generic Object
70
 */
71
class ThirdGenericObject extends GenericObject {
72
73
/**
74
 * @return void
75
 */
76
        public function callback() {
77
        }
78
79
}
80
81
/**
82
 * A collection of Generic objects
83
 */
84
class GenericObjectCollection extends ObjectCollection {
85
86
/**
87
 * Loads a generic object
88
 *
89
 * @param string $object Object name
90
 * @param array $settings Settings array
91
 * @return array List of loaded objects
92
 */
93
        public function load($object, $settings = array()) {
94
                list(, $name) = pluginSplit($object);
95
                if (isset($this->_loaded[$name])) {
96
                        return $this->_loaded[$name];
97
                }
98
                $objectClass = $name . 'GenericObject';
99
                $this->_loaded[$name] = new $objectClass($this, $settings);
100
                $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
101
                if ($enable === true) {
102
                        $this->enable($name);
103
                }
104
                return $this->_loaded[$name];
105
        }
106
107
/**
108
 * Helper method for adding/overwriting enabled objects including
109
 * settings
110
 *
111
 * @param string $name Name of the object
112
 * @param Object $object The object to use
113
 * @param array $settings Settings to apply for the object
114
 * @return array Loaded objects
115
 */
116
        public function setObject($name, $object, $settings = array()) {
117
                $this->_loaded[$name] = $object;
118
                if (isset($settings['priority'])) {
119
                        $this->setPriority($name, $settings['priority']);
120
                }
121
                $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
122
                if ($enable === true) {
123
                        $this->enable($name);
124
                }
125
                return $this->_loaded;
126
        }
127
128
}
129
130
class ObjectCollectionTest extends CakeTestCase {
131
132
/**
133
 * setUp
134
 *
135
 * @return void
136
 */
137
        public function setUp() {
138
                parent::setUp();
139
                $this->Objects = new GenericObjectCollection();
140
        }
141
142
/**
143
 * tearDown
144
 *
145
 * @return void
146
 */
147
        public function tearDown() {
148
                parent::tearDown();
149
                unset($this->Objects);
150
        }
151
152
/**
153
 * test triggering callbacks on loaded helpers
154
 *
155
 * @return void
156
 */
157
        public function testLoad() {
158
                $result = $this->Objects->load('First');
159
                $this->assertInstanceOf('FirstGenericObject', $result);
160
                $this->assertInstanceOf('FirstGenericObject', $this->Objects->First);
161
162
                $result = $this->Objects->loaded();
163
                $this->assertEquals(array('First'), $result, 'loaded() results are wrong.');
164
165
                $this->assertTrue($this->Objects->enabled('First'));
166
167
                $result = $this->Objects->load('First');
168
                $this->assertSame($result, $this->Objects->First);
169
        }
170
171
/**
172
 * test unload()
173
 *
174
 * @return void
175
 */
176
        public function testUnload() {
177
                $this->Objects->load('First');
178
                $this->Objects->load('Second');
179
180
                $result = $this->Objects->loaded();
181
                $this->assertEquals(array('First', 'Second'), $result, 'loaded objects are wrong');
182
183
                $this->Objects->unload('First');
184
                $this->assertFalse(isset($this->Objects->First));
185
                $this->assertTrue(isset($this->Objects->Second));
186
187
                $result = $this->Objects->loaded();
188
                $this->assertEquals(array('Second'), $result, 'loaded objects are wrong');
189
190
                $result = $this->Objects->loaded();
191
                $this->assertEquals(array('Second'), $result, 'enabled objects are wrong');
192
        }
193
194
/**
195
 * Tests set()
196
 *
197
 * @return void
198
 */
199
        public function testSet() {
200
                $this->Objects->load('First');
201
202
                $result = $this->Objects->loaded();
203
                $this->assertEquals(array('First'), $result, 'loaded objects are wrong');
204
205
                $result = $this->Objects->set('First', new SecondGenericObject($this->Objects));
206
                $this->assertInstanceOf('SecondGenericObject', $result['First'], 'set failed');
207
208
                $result = $this->Objects->set('Second', new SecondGenericObject($this->Objects));
209
                $this->assertInstanceOf('SecondGenericObject', $result['Second'], 'set failed');
210
211
                $this->assertEquals(2, count($result));
212
        }
213
214
/**
215
 * creates mock classes for testing
216
 *
217
 * @return void
218
 */
219
        protected function _makeMockClasses() {
220
                $this->FirstGenericObject = $this->getMock('FirstGenericObject', array(), array(), '', false);
221
                $this->SecondGenericObject = $this->getMock('SecondGenericObject', array(), array(), '', false);
222
                $this->ThirdGenericObject = $this->getMock('ThirdGenericObject', array(), array(), '', false);
223
        }
224
225
/**
226
 * test triggering callbacks.
227
 *
228
 * @return void
229
 */
230
        public function testTrigger() {
231
                $this->_makeMockClasses();
232
                $this->Objects->setObject('TriggerMockFirst', $this->FirstGenericObject);
233
                $this->Objects->setObject('TriggerMockSecond', $this->SecondGenericObject);
234
235
                $this->Objects->TriggerMockFirst->expects($this->once())
236
                        ->method('callback')
237
                        ->will($this->returnValue(true));
238
                $this->Objects->TriggerMockSecond->expects($this->once())
239
                        ->method('callback')
240
                        ->will($this->returnValue(true));
241
242
                $this->assertTrue($this->Objects->trigger('callback'));
243
        }
244
245
/**
246
 * test trigger and disabled objects
247
 *
248
 * @return void
249
 */
250
        public function testTriggerWithDisabledObjects() {
251
                $this->_makeMockClasses();
252
                $this->Objects->setObject('TriggerMockFirst', $this->FirstGenericObject);
253
                $this->Objects->setObject('TriggerMockSecond', $this->SecondGenericObject, array('enabled' => false));
254
255
                $this->Objects->TriggerMockFirst->expects($this->once())
256
                        ->method('callback')
257
                        ->will($this->returnValue(true));
258
                $this->Objects->TriggerMockSecond->expects($this->never())
259
                        ->method('callback')
260
                        ->will($this->returnValue(true));
261
262
                $this->assertTrue($this->Objects->trigger('callback', array()));
263
        }
264
265
/**
266
 * test that the collectReturn option works.
267
 *
268
 * @return void
269
 */
270
        public function testTriggerWithCollectReturn() {
271
                $this->_makeMockClasses();
272
                $this->Objects->setObject('TriggerMockFirst', $this->FirstGenericObject);
273
                $this->Objects->setObject('TriggerMockSecond', $this->SecondGenericObject);
274
275
                $this->Objects->TriggerMockFirst->expects($this->once())
276
                        ->method('callback')
277
                        ->will($this->returnValue(array('one', 'two')));
278
                $this->Objects->TriggerMockSecond->expects($this->once())
279
                        ->method('callback')
280
                        ->will($this->returnValue(array('three', 'four')));
281
282
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
283
                $expected = array(
284
                        array('one', 'two'),
285
                        array('three', 'four')
286
                );
287
                $this->assertEquals($expected, $result);
288
        }
289
290
/**
291
 * test that trigger with break & breakOn works.
292
 *
293
 * @return void
294
 */
295
        public function testTriggerWithBreak() {
296
                $this->_makeMockClasses();
297
                $this->Objects->setObject('TriggerMockFirst', $this->FirstGenericObject);
298
                $this->Objects->setObject('TriggerMockSecond', $this->SecondGenericObject);
299
300
                $this->Objects->TriggerMockFirst->expects($this->once())
301
                        ->method('callback')
302
                        ->will($this->returnValue(false));
303
                $this->Objects->TriggerMockSecond->expects($this->never())
304
                        ->method('callback');
305
306
                $result = $this->Objects->trigger(
307
                        'callback',
308
                        array(),
309
                        array('break' => true, 'breakOn' => false)
310
                );
311
                $this->assertFalse($result);
312
        }
313
314
/**
315
 * test that trigger with modParams works.
316
 *
317
 * @return void
318
 */
319
        public function testTriggerWithModParams() {
320
                $this->_makeMockClasses();
321
                $this->Objects->setObject('TriggerMockFirst', $this->FirstGenericObject);
322
                $this->Objects->setObject('TriggerMockSecond', $this->SecondGenericObject);
323
324
                $this->Objects->TriggerMockFirst->expects($this->once())
325
                        ->method('callback')
326
                        ->with(array('value'))
327
                        ->will($this->returnValue(array('new value')));
328
329
                $this->Objects->TriggerMockSecond->expects($this->once())
330
                        ->method('callback')
331
                        ->with(array('new value'))
332
                        ->will($this->returnValue(array('newer value')));
333
334
                $result = $this->Objects->trigger(
335
                        'callback',
336
                        array(array('value')),
337
                        array('modParams' => 0)
338
                );
339
                $this->assertEquals(array('newer value'), $result);
340
        }
341
342
/**
343
 * test that setting modParams to an index that doesn't exist doesn't cause errors.
344
 *
345
 * @expectedException CakeException
346
 * @return void
347
 */
348
        public function testTriggerModParamsInvalidIndex() {
349
                $this->_makeMockClasses();
350
                $this->Objects->setObject('TriggerMockFirst', $this->FirstGenericObject);
351
                $this->Objects->setObject('TriggerMockSecond', $this->SecondGenericObject);
352
353
                $this->Objects->TriggerMockFirst->expects($this->never())
354
                        ->method('callback');
355
356
                $this->Objects->TriggerMockSecond->expects($this->never())
357
                        ->method('callback');
358
359
                $this->Objects->trigger(
360
                        'callback',
361
                        array(array('value')),
362
                        array('modParams' => 2)
363
                );
364
        }
365
366
/**
367
 * test that returning null doesn't modify parameters.
368
 *
369
 * @return void
370
 */
371
        public function testTriggerModParamsNullIgnored() {
372
                $this->_makeMockClasses();
373
                $this->Objects->setObject('TriggerMockFirst', $this->FirstGenericObject);
374
                $this->Objects->setObject('TriggerMockSecond', $this->SecondGenericObject);
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->setObject('TriggerMockFirst', $this->FirstGenericObject);
402
                $this->Objects->setObject('TriggerMockSecond', $this->SecondGenericObject, array('priority' => 5));
403
404
                $this->Objects->TriggerMockFirst->expects($this->any())
405
                        ->method('callback')
406
                        ->will($this->returnValue('1st'));
407
                $this->Objects->TriggerMockSecond->expects($this->any())
408
                        ->method('callback')
409
                        ->will($this->returnValue('2nd'));
410
411
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
412
                $expected = array(
413
                        '2nd',
414
                        '1st'
415
                );
416
                $this->assertEquals($expected, $result);
417
418
                $this->Objects->setObject('TriggerMockThird', $this->ThirdGenericObject, array('priority' => 7));
419
                $this->Objects->TriggerMockThird->expects($this->any())
420
                        ->method('callback')
421
                        ->will($this->returnValue('3rd'));
422
423
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
424
                $expected = array(
425
                        '2nd',
426
                        '3rd',
427
                        '1st'
428
                );
429
                $this->assertEquals($expected, $result);
430
431
                $this->Objects->disable('TriggerMockFirst');
432
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
433
                $expected = array(
434
                        '2nd',
435
                        '3rd'
436
                );
437
                $this->assertEquals($expected, $result);
438
439
                $this->Objects->enable('TriggerMockFirst');
440
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
441
                $expected = array(
442
                        '2nd',
443
                        '3rd',
444
                        '1st'
445
                );
446
                $this->assertEquals($expected, $result);
447
448
                $this->Objects->disable('TriggerMockThird');
449
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
450
                $expected = array(
451
                        '2nd',
452
                        '1st'
453
                );
454
                $this->assertEquals($expected, $result);
455
456
                $this->Objects->enable('TriggerMockThird', false);
457
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
458
                $expected = array(
459
                        '2nd',
460
                        '1st',
461
                        '3rd'
462
                );
463
                $this->assertEquals($expected, $result);
464
465
                $this->Objects->setPriority('TriggerMockThird', 1);
466
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
467
                $expected = array(
468
                        '3rd',
469
                        '2nd',
470
                        '1st'
471
                );
472
                $this->assertEquals($expected, $result);
473
474
                $this->Objects->disable('TriggerMockThird');
475
                $this->Objects->setPriority('TriggerMockThird', 11);
476
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
477
                $expected = array(
478
                        '2nd',
479
                        '1st'
480
                );
481
                $this->assertEquals($expected, $result);
482
483
                $this->Objects->enable('TriggerMockThird');
484
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
485
                $expected = array(
486
                        '2nd',
487
                        '1st',
488
                        '3rd'
489
                );
490
                $this->assertEquals($expected, $result);
491
492
                $this->Objects->setPriority('TriggerMockThird');
493
                $result = $this->Objects->trigger('callback', array(), array('collectReturn' => true));
494
                $expected = array(
495
                        '2nd',
496
                        '1st',
497
                        '3rd'
498
                );
499
                $this->assertEquals($expected, $result);
500
        }
501
502
/**
503
 * test normalizeObjectArray
504
 *
505
 * @return void
506
 */
507
        public function testnormalizeObjectArray() {
508
                $components = array(
509
                        'Html',
510
                        'Foo.Bar' => array('one', 'two'),
511
                        'Something',
512
                        'Banana.Apple' => array('foo' => 'bar')
513
                );
514
                $result = ObjectCollection::normalizeObjectArray($components);
515
                $expected = array(
516
                        'Html' => array('class' => 'Html', 'settings' => array()),
517
                        'Bar' => array('class' => 'Foo.Bar', 'settings' => array('one', 'two')),
518
                        'Something' => array('class' => 'Something', 'settings' => array()),
519
                        'Apple' => array('class' => 'Banana.Apple', 'settings' => array('foo' => 'bar')),
520
                );
521
                $this->assertEquals($expected, $result);
522
523
                // This is the result after Controller::_mergeVars
524
                $components = array(
525
                        'Html' => null,
526
                        'Foo.Bar' => array('one', 'two'),
527
                        'Something' => null,
528
                        'Banana.Apple' => array('foo' => 'bar')
529
                );
530
                $result = ObjectCollection::normalizeObjectArray($components);
531
                $this->assertEquals($expected, $result);
532
        }
533
534
/**
535
 * tests that passing an instance of CakeEvent to trigger will prepend the subject to the list of arguments
536
 *
537
 * @return void
538
 * @triggers callback $subjectClass, array('first argument')
539
 */
540
        public function testDispatchEventWithSubject() {
541
                $this->_makeMockClasses();
542
                $this->Objects->setObject('TriggerMockFirst', $this->FirstGenericObject);
543
                $this->Objects->setObject('TriggerMockSecond', $this->SecondGenericObject);
544
545
                $subjectClass = new Object();
546
                $this->Objects->TriggerMockFirst->expects($this->once())
547
                        ->method('callback')
548
                        ->with($subjectClass, 'first argument')
549
                        ->will($this->returnValue(true));
550
                $this->Objects->TriggerMockSecond->expects($this->once())
551
                        ->method('callback')
552
                        ->with($subjectClass, 'first argument')
553
                        ->will($this->returnValue(true));
554
555
                $event = new CakeEvent('callback', $subjectClass, array('first argument'));
556
                $this->assertTrue($this->Objects->trigger($event));
557
        }
558
559
/**
560
 * tests that passing an instance of CakeEvent to trigger with omitSubject property
561
 * will NOT prepend the subject to the list of arguments
562
 *
563
 * @return void
564
 * @triggers callback $subjectClass, array('first argument')
565
 */
566
        public function testDispatchEventNoSubject() {
567
                $this->_makeMockClasses();
568
                $this->Objects->setObject('TriggerMockFirst', $this->FirstGenericObject);
569
                $this->Objects->setObject('TriggerMockSecond', $this->SecondGenericObject);
570
571
                $subjectClass = new Object();
572
                $this->Objects->TriggerMockFirst->expects($this->once())
573
                        ->method('callback')
574
                        ->with('first argument')
575
                        ->will($this->returnValue(true));
576
                $this->Objects->TriggerMockSecond->expects($this->once())
577
                        ->method('callback')
578
                        ->with('first argument')
579
                        ->will($this->returnValue(true));
580
581
                $event = new CakeEvent('callback', $subjectClass, array('first argument'));
582
                $event->omitSubject = true;
583
                $this->assertTrue($this->Objects->trigger($event));
584
        }
585
586
/**
587
 * test that the various methods ignore plugin prefixes
588
 *
589
 * plugin prefixes should be removed consistently as load() will
590
 * remove them. Furthermore the __get() method does not support
591
 * names with '.' in them.
592
 *
593
 * @return void
594
 */
595
        public function testPluginPrefixes() {
596
                $this->Objects->load('TestPlugin.First');
597
                $this->assertTrue($this->Objects->loaded('First'));
598
                $this->assertTrue($this->Objects->loaded('TestPlugin.First'));
599
600
                $this->assertTrue($this->Objects->enabled('First'));
601
                $this->assertTrue($this->Objects->enabled('TestPlugin.First'));
602
603
                $this->assertNull($this->Objects->disable('TestPlugin.First'));
604
                $this->assertFalse($this->Objects->enabled('First'));
605
                $this->assertFalse($this->Objects->enabled('TestPlugin.First'));
606
607
                $this->assertNull($this->Objects->enable('TestPlugin.First'));
608
                $this->assertTrue($this->Objects->enabled('First'));
609
                $this->assertTrue($this->Objects->enabled('TestPlugin.First'));
610
                $this->Objects->setPriority('TestPlugin.First', 1000);
611
612
                $result = $this->Objects->prioritize();
613
                $this->assertEquals(1000, $result['First'][0]);
614
        }
615
}