-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ComponentRegistryTest.php
227 lines (197 loc) · 7.18 KB
/
ComponentRegistryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Controller;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Component\AuthComponent;
use Cake\Controller\Component\CookieComponent;
use Cake\Controller\Controller;
use Cake\Core\Plugin;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\TestSuite\TestCase;
/**
* Extended CookieComponent
*/
class CookieAliasComponent extends CookieComponent
{
}
class ComponentRegistryTest extends TestCase
{
/**
* setUp
*
* @return void
*/
public function setUp()
{
parent::setUp();
$controller = new Controller(new Request(), new Response());
$this->Components = new ComponentRegistry($controller);
}
/**
* tearDown
*
* @return void
*/
public function tearDown()
{
parent::tearDown();
unset($this->Components);
}
/**
* test triggering callbacks on loaded helpers
*
* @return void
*/
public function testLoad()
{
$result = $this->Components->load('Cookie');
$this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $result);
$this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $this->Components->Cookie);
$result = $this->Components->loaded();
$this->assertEquals(['Cookie'], $result, 'loaded() results are wrong.');
$result = $this->Components->load('Cookie');
$this->assertSame($result, $this->Components->Cookie);
}
/**
* Tests loading as an alias
*
* @return void
*/
public function testLoadWithAlias()
{
$result = $this->Components->load('Cookie', ['className' => __NAMESPACE__ . '\CookieAliasComponent', 'somesetting' => true]);
$this->assertInstanceOf(__NAMESPACE__ . '\CookieAliasComponent', $result);
$this->assertInstanceOf(__NAMESPACE__ . '\CookieAliasComponent', $this->Components->Cookie);
$this->assertTrue($this->Components->Cookie->config('somesetting'));
$result = $this->Components->loaded();
$this->assertEquals(['Cookie'], $result, 'loaded() results are wrong.');
$result = $this->Components->load('Cookie');
$this->assertInstanceOf(__NAMESPACE__ . '\CookieAliasComponent', $result);
Plugin::load('TestPlugin');
$result = $this->Components->load('SomeOther', ['className' => 'TestPlugin.Other']);
$this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $result);
$this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $this->Components->SomeOther);
$result = $this->Components->loaded();
$this->assertEquals(['Cookie', 'SomeOther'], $result, 'loaded() results are wrong.');
}
/**
* test load and enable = false
*
* @return void
*/
public function testLoadWithEnableFalse()
{
$mock = $this->getMockBuilder('Cake\Event\EventManager')->getMock();
$mock->expects($this->never())
->method('attach');
$this->Components->getController()->eventManager($mock);
$result = $this->Components->load('Cookie', ['enabled' => false]);
$this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $result);
$this->assertInstanceOf('Cake\Controller\Component\CookieComponent', $this->Components->Cookie);
}
/**
* test missingcomponent exception
*
* @expectedException \Cake\Controller\Exception\MissingComponentException
* @return void
*/
public function testLoadMissingComponent()
{
$this->Components->load('ThisComponentShouldAlwaysBeMissing');
}
/**
* test loading a plugin component.
*
* @return void
*/
public function testLoadPluginComponent()
{
Plugin::load('TestPlugin');
$result = $this->Components->load('TestPlugin.Other');
$this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $result, 'Component class is wrong.');
$this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $this->Components->Other, 'Class is wrong');
}
/**
* Test loading components with aliases and plugins.
*
* @return void
*/
public function testLoadWithAliasAndPlugin()
{
Plugin::load('TestPlugin');
$result = $this->Components->load('AliasedOther', ['className' => 'TestPlugin.Other']);
$this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $result);
$this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $this->Components->AliasedOther);
$result = $this->Components->loaded();
$this->assertEquals(['AliasedOther'], $result, 'loaded() results are wrong.');
}
/**
* test getting the controller out of the collection
*
* @return void
*/
public function testGetController()
{
$result = $this->Components->getController();
$this->assertInstanceOf('Cake\Controller\Controller', $result);
}
/**
* Test reset.
*
* @return void
*/
public function testReset()
{
$eventManager = $this->Components->getController()->eventManager();
$instance = $this->Components->load('Auth');
$this->assertSame(
$instance,
$this->Components->Auth,
'Instance in registry should be the same as previously loaded'
);
$this->assertCount(1, $eventManager->listeners('Controller.startup'));
$this->assertNull($this->Components->reset(), 'No return expected');
$this->assertCount(0, $eventManager->listeners('Controller.startup'));
$this->assertNotSame($instance, $this->Components->load('Auth'));
}
/**
* Test unloading.
*
* @return void
*/
public function testUnload()
{
$eventManager = $this->Components->getController()->eventManager();
$result = $this->Components->load('Auth');
$this->Components->unload('Auth');
$this->assertFalse(isset($this->Components->Auth), 'Should be gone');
$this->assertCount(0, $eventManager->listeners('Controller.startup'));
}
/**
* Test set.
*
* @return void
*/
public function testSet()
{
$eventManager = $this->Components->getController()->eventManager();
$this->assertCount(0, $eventManager->listeners('Controller.startup'));
$auth = new AuthComponent($this->Components);
$this->Components->set('Auth', $auth);
$this->assertTrue(isset($this->Components->Auth), 'Should be gone');
$this->assertCount(1, $eventManager->listeners('Controller.startup'));
}
}