pictcode / lib / Cake / Test / Case / Controller / ComponentCollectionTest.php @ 0b1b8047
履歴 | 表示 | アノテート | ダウンロード (5.178 KB)
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* ComponentCollectionTest 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.Controller
|
15 |
* @since CakePHP(tm) v 2.0
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
17 |
*/
|
18 |
|
19 |
App::uses('CakeResponse', 'Network'); |
20 |
App::uses('CookieComponent', 'Controller/Component'); |
21 |
App::uses('SecurityComponent', 'Controller/Component'); |
22 |
App::uses('ComponentCollection', 'Controller'); |
23 |
|
24 |
/**
|
25 |
* Extended CookieComponent
|
26 |
*/
|
27 |
class CookieAliasComponent extends CookieComponent { |
28 |
} |
29 |
|
30 |
class ComponentCollectionTest extends CakeTestCase { |
31 |
|
32 |
/**
|
33 |
* setUp
|
34 |
*
|
35 |
* @return void
|
36 |
*/
|
37 |
public function setUp() { |
38 |
parent::setUp();
|
39 |
$this->Components = new ComponentCollection(); |
40 |
} |
41 |
|
42 |
/**
|
43 |
* tearDown
|
44 |
*
|
45 |
* @return void
|
46 |
*/
|
47 |
public function tearDown() { |
48 |
parent::tearDown();
|
49 |
unset($this->Components); |
50 |
} |
51 |
|
52 |
/**
|
53 |
* test triggering callbacks on loaded helpers
|
54 |
*
|
55 |
* @return void
|
56 |
*/
|
57 |
public function testLoad() { |
58 |
$result = $this->Components->load('Cookie'); |
59 |
$this->assertInstanceOf('CookieComponent', $result); |
60 |
$this->assertInstanceOf('CookieComponent', $this->Components->Cookie); |
61 |
|
62 |
$result = $this->Components->loaded(); |
63 |
$this->assertEquals(array('Cookie'), $result, 'loaded() results are wrong.'); |
64 |
|
65 |
$this->assertTrue($this->Components->enabled('Cookie')); |
66 |
|
67 |
$result = $this->Components->load('Cookie'); |
68 |
$this->assertSame($result, $this->Components->Cookie); |
69 |
} |
70 |
|
71 |
/**
|
72 |
* Tests loading as an alias
|
73 |
*
|
74 |
* @return void
|
75 |
*/
|
76 |
public function testLoadWithAlias() { |
77 |
$result = $this->Components->load('Cookie', array('className' => 'CookieAlias', 'somesetting' => true)); |
78 |
$this->assertInstanceOf('CookieAliasComponent', $result); |
79 |
$this->assertInstanceOf('CookieAliasComponent', $this->Components->Cookie); |
80 |
$this->assertTrue($this->Components->Cookie->settings['somesetting']); |
81 |
|
82 |
$result = $this->Components->loaded(); |
83 |
$this->assertEquals(array('Cookie'), $result, 'loaded() results are wrong.'); |
84 |
|
85 |
$this->assertTrue($this->Components->enabled('Cookie')); |
86 |
|
87 |
$result = $this->Components->load('Cookie'); |
88 |
$this->assertInstanceOf('CookieAliasComponent', $result); |
89 |
|
90 |
App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS))); |
91 |
CakePlugin::load('TestPlugin'); |
92 |
$result = $this->Components->load('SomeOther', array('className' => 'TestPlugin.Other')); |
93 |
$this->assertInstanceOf('OtherComponent', $result); |
94 |
$this->assertInstanceOf('OtherComponent', $this->Components->SomeOther); |
95 |
|
96 |
$result = $this->Components->loaded(); |
97 |
$this->assertEquals(array('Cookie', 'SomeOther'), $result, 'loaded() results are wrong.'); |
98 |
App::build();
|
99 |
CakePlugin::unload();
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* test load and enable = false
|
104 |
*
|
105 |
* @return void
|
106 |
*/
|
107 |
public function testLoadWithEnableFalse() { |
108 |
$result = $this->Components->load('Cookie', array('enabled' => false)); |
109 |
$this->assertInstanceOf('CookieComponent', $result); |
110 |
$this->assertInstanceOf('CookieComponent', $this->Components->Cookie); |
111 |
|
112 |
$this->assertFalse($this->Components->enabled('Cookie'), 'Cookie should be disabled'); |
113 |
} |
114 |
|
115 |
/**
|
116 |
* test missingcomponent exception
|
117 |
*
|
118 |
* @expectedException MissingComponentException
|
119 |
* @return void
|
120 |
*/
|
121 |
public function testLoadMissingComponent() { |
122 |
$this->Components->load('ThisComponentShouldAlwaysBeMissing'); |
123 |
} |
124 |
|
125 |
/**
|
126 |
* test loading a plugin component.
|
127 |
*
|
128 |
* @return void
|
129 |
*/
|
130 |
public function testLoadPluginComponent() { |
131 |
App::build(array( |
132 |
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS), |
133 |
)); |
134 |
CakePlugin::load('TestPlugin'); |
135 |
$result = $this->Components->load('TestPlugin.Other'); |
136 |
$this->assertInstanceOf('OtherComponent', $result, 'Component class is wrong.'); |
137 |
$this->assertInstanceOf('OtherComponent', $this->Components->Other, 'Class is wrong'); |
138 |
App::build();
|
139 |
CakePlugin::unload();
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* test unload()
|
144 |
*
|
145 |
* @return void
|
146 |
*/
|
147 |
public function testUnload() { |
148 |
$this->Components->load('Cookie'); |
149 |
$this->Components->load('Security'); |
150 |
|
151 |
$result = $this->Components->loaded(); |
152 |
$this->assertEquals(array('Cookie', 'Security'), $result, 'loaded components is wrong'); |
153 |
|
154 |
$this->Components->unload('Cookie'); |
155 |
$this->assertFalse(isset($this->Components->Cookie)); |
156 |
$this->assertTrue(isset($this->Components->Security)); |
157 |
|
158 |
$result = $this->Components->loaded(); |
159 |
$this->assertEquals(array('Security'), $result, 'loaded components is wrong'); |
160 |
|
161 |
$result = $this->Components->enabled(); |
162 |
$this->assertEquals(array('Security'), $result, 'enabled components is wrong'); |
163 |
} |
164 |
|
165 |
/**
|
166 |
* test getting the controller out of the collection
|
167 |
*
|
168 |
* @return void
|
169 |
*/
|
170 |
public function testGetController() { |
171 |
$controller = $this->getMock('Controller'); |
172 |
$controller->components = array('Security'); |
173 |
$this->Components->init($controller); |
174 |
$result = $this->Components->getController(); |
175 |
|
176 |
$this->assertSame($controller, $result); |
177 |
} |
178 |
} |