Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/controller/component.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * ComponentTest file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The Open Group Test Suite License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
15 * @package cake | |
16 * @subpackage cake.tests.cases.libs.controller | |
17 * @since CakePHP(tm) v 1.2.0.5436 | |
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
19 */ | |
20 App::import('Controller', 'Controller', false); | |
21 App::import('Controller', 'Component', false); | |
22 | |
23 if (!class_exists('AppController')) { | |
24 | |
25 /** | |
26 * AppController class | |
27 * | |
28 * @package cake | |
29 * @subpackage cake.tests.cases.libs.controller | |
30 */ | |
31 class AppController extends Controller { | |
32 | |
33 /** | |
34 * name property | |
35 * | |
36 * @var string 'App' | |
37 * @access public | |
38 */ | |
39 var $name = 'App'; | |
40 | |
41 /** | |
42 * uses property | |
43 * | |
44 * @var array | |
45 * @access public | |
46 */ | |
47 var $uses = array(); | |
48 | |
49 /** | |
50 * helpers property | |
51 * | |
52 * @var array | |
53 * @access public | |
54 */ | |
55 var $helpers = array(); | |
56 | |
57 /** | |
58 * components property | |
59 * | |
60 * @var array | |
61 * @access public | |
62 */ | |
63 var $components = array('Orange' => array('colour' => 'blood orange')); | |
64 } | |
65 } elseif (!defined('APP_CONTROLLER_EXISTS')){ | |
66 define('APP_CONTROLLER_EXISTS', true); | |
67 } | |
68 | |
69 /** | |
70 * ParamTestComponent | |
71 * | |
72 * @package cake | |
73 * @subpackage cake.tests.cases.libs.controller | |
74 */ | |
75 class ParamTestComponent extends Object { | |
76 | |
77 /** | |
78 * name property | |
79 * | |
80 * @var string 'ParamTest' | |
81 * @access public | |
82 */ | |
83 var $name = 'ParamTest'; | |
84 | |
85 /** | |
86 * components property | |
87 * | |
88 * @var array | |
89 * @access public | |
90 */ | |
91 var $components = array('Banana' => array('config' => 'value')); | |
92 | |
93 /** | |
94 * initialize method | |
95 * | |
96 * @param mixed $controller | |
97 * @param mixed $settings | |
98 * @access public | |
99 * @return void | |
100 */ | |
101 function initialize(&$controller, $settings) { | |
102 foreach ($settings as $key => $value) { | |
103 if (is_numeric($key)) { | |
104 $this->{$value} = true; | |
105 } else { | |
106 $this->{$key} = $value; | |
107 } | |
108 } | |
109 } | |
110 } | |
111 | |
112 /** | |
113 * ComponentTestController class | |
114 * | |
115 * @package cake | |
116 * @subpackage cake.tests.cases.libs.controller | |
117 */ | |
118 class ComponentTestController extends AppController { | |
119 | |
120 /** | |
121 * name property | |
122 * | |
123 * @var string 'ComponentTest' | |
124 * @access public | |
125 */ | |
126 var $name = 'ComponentTest'; | |
127 | |
128 /** | |
129 * uses property | |
130 * | |
131 * @var array | |
132 * @access public | |
133 */ | |
134 var $uses = array(); | |
135 } | |
136 | |
137 /** | |
138 * AppleComponent class | |
139 * | |
140 * @package cake | |
141 * @subpackage cake.tests.cases.libs.controller | |
142 */ | |
143 class AppleComponent extends Object { | |
144 | |
145 /** | |
146 * components property | |
147 * | |
148 * @var array | |
149 * @access public | |
150 */ | |
151 var $components = array('Orange'); | |
152 | |
153 /** | |
154 * testName property | |
155 * | |
156 * @var mixed null | |
157 * @access public | |
158 */ | |
159 var $testName = null; | |
160 | |
161 /** | |
162 * startup method | |
163 * | |
164 * @param mixed $controller | |
165 * @access public | |
166 * @return void | |
167 */ | |
168 function startup(&$controller) { | |
169 $this->testName = $controller->name; | |
170 } | |
171 } | |
172 | |
173 /** | |
174 * OrangeComponent class | |
175 * | |
176 * @package cake | |
177 * @subpackage cake.tests.cases.libs.controller | |
178 */ | |
179 class OrangeComponent extends Object { | |
180 | |
181 /** | |
182 * components property | |
183 * | |
184 * @var array | |
185 * @access public | |
186 */ | |
187 var $components = array('Banana'); | |
188 | |
189 /** | |
190 * initialize method | |
191 * | |
192 * @param mixed $controller | |
193 * @access public | |
194 * @return void | |
195 */ | |
196 function initialize(&$controller, $settings) { | |
197 $this->Controller = $controller; | |
198 $this->Banana->testField = 'OrangeField'; | |
199 $this->settings = $settings; | |
200 } | |
201 | |
202 /** | |
203 * startup method | |
204 * | |
205 * @param Controller $controller | |
206 * @return string | |
207 * @access public | |
208 */ | |
209 function startup(&$controller) { | |
210 $controller->foo = 'pass'; | |
211 } | |
212 } | |
213 | |
214 /** | |
215 * BananaComponent class | |
216 * | |
217 * @package cake | |
218 * @subpackage cake.tests.cases.libs.controller | |
219 */ | |
220 class BananaComponent extends Object { | |
221 | |
222 /** | |
223 * testField property | |
224 * | |
225 * @var string 'BananaField' | |
226 * @access public | |
227 */ | |
228 var $testField = 'BananaField'; | |
229 | |
230 /** | |
231 * startup method | |
232 * | |
233 * @param Controller $controller | |
234 * @return string | |
235 * @access public | |
236 */ | |
237 function startup(&$controller) { | |
238 $controller->bar = 'fail'; | |
239 } | |
240 } | |
241 | |
242 /** | |
243 * MutuallyReferencingOneComponent class | |
244 * | |
245 * @package cake | |
246 * @subpackage cake.tests.cases.libs.controller | |
247 */ | |
248 class MutuallyReferencingOneComponent extends Object { | |
249 | |
250 /** | |
251 * components property | |
252 * | |
253 * @var array | |
254 * @access public | |
255 */ | |
256 var $components = array('MutuallyReferencingTwo'); | |
257 } | |
258 | |
259 /** | |
260 * MutuallyReferencingTwoComponent class | |
261 * | |
262 * @package cake | |
263 * @subpackage cake.tests.cases.libs.controller | |
264 */ | |
265 class MutuallyReferencingTwoComponent extends Object { | |
266 | |
267 /** | |
268 * components property | |
269 * | |
270 * @var array | |
271 * @access public | |
272 */ | |
273 var $components = array('MutuallyReferencingOne'); | |
274 } | |
275 | |
276 /** | |
277 * SomethingWithEmailComponent class | |
278 * | |
279 * @package cake | |
280 * @subpackage cake.tests.cases.libs.controller | |
281 */ | |
282 class SomethingWithEmailComponent extends Object { | |
283 | |
284 /** | |
285 * components property | |
286 * | |
287 * @var array | |
288 * @access public | |
289 */ | |
290 var $components = array('Email'); | |
291 } | |
292 | |
293 Mock::generate('Object', 'ComponentMockComponent', array('startup', 'beforeFilter', 'beforeRender', 'other')); | |
294 | |
295 /** | |
296 * ComponentTest class | |
297 * | |
298 * @package cake | |
299 * @subpackage cake.tests.cases.libs.controller | |
300 */ | |
301 class ComponentTest extends CakeTestCase { | |
302 | |
303 /** | |
304 * setUp method | |
305 * | |
306 * @access public | |
307 * @return void | |
308 */ | |
309 function setUp() { | |
310 $this->_pluginPaths = App::path('plugins'); | |
311 App::build(array( | |
312 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS) | |
313 )); | |
314 } | |
315 | |
316 /** | |
317 * tearDown method | |
318 * | |
319 * @access public | |
320 * @return void | |
321 */ | |
322 function tearDown() { | |
323 App::build(); | |
324 ClassRegistry::flush(); | |
325 } | |
326 | |
327 /** | |
328 * testLoadComponents method | |
329 * | |
330 * @access public | |
331 * @return void | |
332 */ | |
333 function testLoadComponents() { | |
334 $Controller =& new ComponentTestController(); | |
335 $Controller->components = array('RequestHandler'); | |
336 | |
337 $Component =& new Component(); | |
338 $Component->init($Controller); | |
339 | |
340 $this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent')); | |
341 | |
342 $Controller =& new ComponentTestController(); | |
343 $Controller->plugin = 'test_plugin'; | |
344 $Controller->components = array('RequestHandler', 'TestPluginComponent'); | |
345 | |
346 $Component =& new Component(); | |
347 $Component->init($Controller); | |
348 | |
349 $this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent')); | |
350 $this->assertTrue(is_a($Controller->TestPluginComponent, 'TestPluginComponentComponent')); | |
351 $this->assertTrue(is_a( | |
352 $Controller->TestPluginComponent->TestPluginOtherComponent, | |
353 'TestPluginOtherComponentComponent' | |
354 )); | |
355 $this->assertFalse(isset($Controller->TestPluginOtherComponent)); | |
356 | |
357 $Controller =& new ComponentTestController(); | |
358 $Controller->components = array('Security'); | |
359 | |
360 $Component =& new Component(); | |
361 $Component->init($Controller); | |
362 | |
363 $this->assertTrue(is_a($Controller->Security, 'SecurityComponent')); | |
364 $this->assertTrue(is_a($Controller->Security->Session, 'SessionComponent')); | |
365 | |
366 $Controller =& new ComponentTestController(); | |
367 $Controller->components = array('Security', 'Cookie', 'RequestHandler'); | |
368 | |
369 $Component =& new Component(); | |
370 $Component->init($Controller); | |
371 | |
372 $this->assertTrue(is_a($Controller->Security, 'SecurityComponent')); | |
373 $this->assertTrue(is_a($Controller->Security->RequestHandler, 'RequestHandlerComponent')); | |
374 $this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent')); | |
375 $this->assertTrue(is_a($Controller->Cookie, 'CookieComponent')); | |
376 } | |
377 | |
378 /** | |
379 * test component loading | |
380 * | |
381 * @return void | |
382 */ | |
383 function testNestedComponentLoading() { | |
384 $Controller =& new ComponentTestController(); | |
385 $Controller->components = array('Apple'); | |
386 $Controller->uses = false; | |
387 $Controller->constructClasses(); | |
388 $Controller->Component->initialize($Controller); | |
389 | |
390 $this->assertTrue(is_a($Controller->Apple, 'AppleComponent')); | |
391 $this->assertTrue(is_a($Controller->Apple->Orange, 'OrangeComponent')); | |
392 $this->assertTrue(is_a($Controller->Apple->Orange->Banana, 'BananaComponent')); | |
393 $this->assertTrue(is_a($Controller->Apple->Orange->Controller, 'ComponentTestController')); | |
394 $this->assertTrue(empty($Controller->Apple->Session)); | |
395 $this->assertTrue(empty($Controller->Apple->Orange->Session)); | |
396 } | |
397 | |
398 /** | |
399 * Tests Component::startup() and only running callbacks for components directly attached to | |
400 * the controller. | |
401 * | |
402 * @return void | |
403 */ | |
404 function testComponentStartup() { | |
405 $Controller =& new ComponentTestController(); | |
406 $Controller->components = array('Apple'); | |
407 $Controller->uses = false; | |
408 $Controller->constructClasses(); | |
409 $Controller->Component->initialize($Controller); | |
410 $Controller->beforeFilter(); | |
411 $Controller->Component->startup($Controller); | |
412 | |
413 $this->assertTrue(is_a($Controller->Apple, 'AppleComponent')); | |
414 $this->assertEqual($Controller->Apple->testName, 'ComponentTest'); | |
415 | |
416 $expected = !(defined('APP_CONTROLLER_EXISTS') && APP_CONTROLLER_EXISTS); | |
417 $this->assertEqual(isset($Controller->foo), $expected); | |
418 $this->assertFalse(isset($Controller->bar)); | |
419 } | |
420 | |
421 /** | |
422 * test that triggerCallbacks fires methods on all the components, and can trigger any method. | |
423 * | |
424 * @return void | |
425 */ | |
426 function testTriggerCallback() { | |
427 $Controller =& new ComponentTestController(); | |
428 $Controller->components = array('ComponentMock'); | |
429 $Controller->uses = null; | |
430 $Controller->constructClasses(); | |
431 | |
432 $Controller->ComponentMock->expectOnce('beforeRender'); | |
433 $Controller->Component->triggerCallback('beforeRender', $Controller); | |
434 | |
435 $Controller->ComponentMock->expectNever('beforeFilter'); | |
436 $Controller->ComponentMock->enabled = false; | |
437 $Controller->Component->triggerCallback('beforeFilter', $Controller); | |
438 } | |
439 | |
440 /** | |
441 * test a component being used more than once. | |
442 * | |
443 * @return void | |
444 */ | |
445 function testMultipleComponentInitialize() { | |
446 $Controller =& new ComponentTestController(); | |
447 $Controller->uses = false; | |
448 $Controller->components = array('Orange', 'Banana'); | |
449 $Controller->constructClasses(); | |
450 $Controller->Component->initialize($Controller); | |
451 | |
452 $this->assertEqual($Controller->Banana->testField, 'OrangeField'); | |
453 $this->assertEqual($Controller->Orange->Banana->testField, 'OrangeField'); | |
454 } | |
455 | |
456 /** | |
457 * Test Component declarations with Parameters | |
458 * tests merging of component parameters and merging / construction of components. | |
459 * | |
460 * @return void | |
461 */ | |
462 function testComponentsWithParams() { | |
463 if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) { | |
464 return; | |
465 } | |
466 | |
467 $Controller =& new ComponentTestController(); | |
468 $Controller->components = array('ParamTest' => array('test' => 'value', 'flag'), 'Apple'); | |
469 $Controller->uses = false; | |
470 $Controller->constructClasses(); | |
471 $Controller->Component->initialize($Controller); | |
472 | |
473 $this->assertTrue(is_a($Controller->ParamTest, 'ParamTestComponent')); | |
474 $this->assertTrue(is_a($Controller->ParamTest->Banana, 'BananaComponent')); | |
475 $this->assertTrue(is_a($Controller->Orange, 'OrangeComponent')); | |
476 $this->assertFalse(isset($Controller->Session)); | |
477 $this->assertEqual($Controller->Orange->settings, array('colour' => 'blood orange')); | |
478 $this->assertEqual($Controller->ParamTest->test, 'value'); | |
479 $this->assertEqual($Controller->ParamTest->flag, true); | |
480 | |
481 //Settings are merged from app controller and current controller. | |
482 $Controller =& new ComponentTestController(); | |
483 $Controller->components = array( | |
484 'ParamTest' => array('test' => 'value'), | |
485 'Orange' => array('ripeness' => 'perfect') | |
486 ); | |
487 $Controller->constructClasses(); | |
488 $Controller->Component->initialize($Controller); | |
489 | |
490 $expected = array('colour' => 'blood orange', 'ripeness' => 'perfect'); | |
491 $this->assertEqual($Controller->Orange->settings, $expected); | |
492 $this->assertEqual($Controller->ParamTest->test, 'value'); | |
493 } | |
494 | |
495 /** | |
496 * Ensure that settings are not duplicated when passed into component initialize. | |
497 * | |
498 * @return void | |
499 */ | |
500 function testComponentParamsNoDuplication() { | |
501 if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) { | |
502 return; | |
503 } | |
504 $Controller =& new ComponentTestController(); | |
505 $Controller->components = array('Orange' => array('setting' => array('itemx'))); | |
506 $Controller->uses = false; | |
507 | |
508 $Controller->constructClasses(); | |
509 $Controller->Component->initialize($Controller); | |
510 $expected = array('setting' => array('itemx'), 'colour' => 'blood orange'); | |
511 $this->assertEqual($Controller->Orange->settings, $expected, 'Params duplication has occured %s'); | |
512 } | |
513 | |
514 /** | |
515 * Test mutually referencing components. | |
516 * | |
517 * @return void | |
518 */ | |
519 function testMutuallyReferencingComponents() { | |
520 $Controller =& new ComponentTestController(); | |
521 $Controller->components = array('MutuallyReferencingOne'); | |
522 $Controller->uses = false; | |
523 $Controller->constructClasses(); | |
524 $Controller->Component->initialize($Controller); | |
525 | |
526 $this->assertTrue(is_a( | |
527 $Controller->MutuallyReferencingOne, | |
528 'MutuallyReferencingOneComponent' | |
529 )); | |
530 $this->assertTrue(is_a( | |
531 $Controller->MutuallyReferencingOne->MutuallyReferencingTwo, | |
532 'MutuallyReferencingTwoComponent' | |
533 )); | |
534 $this->assertTrue(is_a( | |
535 $Controller->MutuallyReferencingOne->MutuallyReferencingTwo->MutuallyReferencingOne, | |
536 'MutuallyReferencingOneComponent' | |
537 )); | |
538 } | |
539 | |
540 /** | |
541 * Test mutually referencing components. | |
542 * | |
543 * @return void | |
544 */ | |
545 function testSomethingReferencingEmailComponent() { | |
546 $Controller =& new ComponentTestController(); | |
547 $Controller->components = array('SomethingWithEmail'); | |
548 $Controller->uses = false; | |
549 $Controller->constructClasses(); | |
550 $Controller->Component->initialize($Controller); | |
551 $Controller->beforeFilter(); | |
552 $Controller->Component->startup($Controller); | |
553 | |
554 $this->assertTrue(is_a( | |
555 $Controller->SomethingWithEmail, | |
556 'SomethingWithEmailComponent' | |
557 )); | |
558 $this->assertTrue(is_a( | |
559 $Controller->SomethingWithEmail->Email, | |
560 'EmailComponent' | |
561 )); | |
562 $this->assertTrue(is_a( | |
563 $Controller->SomethingWithEmail->Email->Controller, | |
564 'ComponentTestController' | |
565 )); | |
566 } | |
567 | |
568 /** | |
569 * Test that SessionComponent doesn't get added if its already in the components array. | |
570 * | |
571 * @return void | |
572 * @access public | |
573 */ | |
574 function testDoubleLoadingOfSessionComponent() { | |
575 if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) { | |
576 return; | |
577 } | |
578 | |
579 $Controller =& new ComponentTestController(); | |
580 $Controller->uses = false; | |
581 $Controller->components = array('Session'); | |
582 $Controller->constructClasses(); | |
583 | |
584 $this->assertEqual($Controller->components, array('Session' => '', 'Orange' => array('colour' => 'blood orange'))); | |
585 } | |
586 | |
587 } |