/
ComponentTest.php
272 lines (238 loc) · 8.64 KB
/
ComponentTest.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @since 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Controller;
use Cake\Controller\Component\CookieComponent;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Core\Configure;
use Cake\Event\EventManager;
use Cake\TestSuite\TestCase;
use TestApp\Controller\Component\AppleComponent;
use TestApp\Controller\Component\BananaComponent;
use TestApp\Controller\Component\ConfiguredComponent;
use TestApp\Controller\Component\OrangeComponent;
use TestApp\Controller\Component\SomethingWithCookieComponent;
use TestApp\Controller\ComponentTestController;
/**
* ComponentTest class
*/
class ComponentTest extends TestCase
{
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
Configure::write('App.namespace', 'TestApp');
}
/**
* test accessing inner components.
*
* @return void
*/
public function testInnerComponentConstruction()
{
$Collection = new ComponentRegistry();
$Component = new AppleComponent($Collection);
$this->assertInstanceOf(OrangeComponent::class, $Component->Orange, 'class is wrong');
}
/**
* test component loading
*
* @return void
*/
public function testNestedComponentLoading()
{
$Collection = new ComponentRegistry();
$Apple = new AppleComponent($Collection);
$this->assertInstanceOf(OrangeComponent::class, $Apple->Orange, 'class is wrong');
$this->assertInstanceOf(BananaComponent::class, $Apple->Orange->Banana, 'class is wrong');
$this->assertTrue(empty($Apple->Session));
$this->assertTrue(empty($Apple->Orange->Session));
}
/**
* test that component components are not enabled in the collection.
*
* @return void
*/
public function testInnerComponentsAreNotEnabled()
{
$mock = $this->getMockBuilder(EventManager::class)->getMock();
$controller = new Controller();
$controller->eventManager($mock);
$mock->expects($this->once())
->method('on')
->with($this->isInstanceOf(AppleComponent::class));
$Collection = new ComponentRegistry($controller);
$Apple = $Collection->load('Apple');
$this->assertInstanceOf(OrangeComponent::class, $Apple->Orange, 'class is wrong');
}
/**
* test a component being used more than once.
*
* @return void
*/
public function testMultipleComponentInitialize()
{
$Collection = new ComponentRegistry();
$Banana = $Collection->load('Banana');
$Orange = $Collection->load('Orange');
$this->assertSame($Banana, $Orange->Banana, 'Should be references');
$Banana->testField = 'OrangeField';
$this->assertSame($Banana->testField, $Orange->Banana->testField, 'References are broken');
}
/**
* Test a duplicate component being loaded more than once with same and differing configurations.
*
* @expectedException \RuntimeException
* @expectedExceptionMessage The "Banana" alias has already been loaded with the following config:
* @return void
*/
public function testDuplicateComponentInitialize()
{
$Collection = new ComponentRegistry();
$Collection->load('Banana', ['property' => ['closure' => function () {
}]]);
$Collection->load('Banana', ['property' => ['closure' => function () {
}]]);
$this->assertInstanceOf(BananaComponent::class, $Collection->Banana, 'class is wrong');
$Collection->load('Banana', ['property' => ['differs']]);
}
/**
* Test mutually referencing components.
*
* @return void
*/
public function testSomethingReferencingCookieComponent()
{
$Controller = new ComponentTestController();
$Controller->loadComponent('SomethingWithCookie');
$Controller->startupProcess();
$this->assertInstanceOf(SomethingWithCookieComponent::class, $Controller->SomethingWithCookie);
$this->assertInstanceOf(CookieComponent::class, $Controller->SomethingWithCookie->Cookie);
}
/**
* Tests __debugInfo
*
* @return void
*/
public function testDebugInfo()
{
$Collection = new ComponentRegistry();
$Component = new AppleComponent($Collection);
$expected = [
'components' => [
'Orange'
],
'implementedEvents' => [
'Controller.startup' => 'startup'
],
'_config' => []
];
$result = $Component->__debugInfo();
$this->assertEquals($expected, $result);
}
/**
* Tests null return for unknown magic properties.
*
* @return void
*/
public function testMagicReturnsNull()
{
$Component = new AppleComponent(new ComponentRegistry());
$this->assertNull($Component->ShouldBeNull);
}
/**
* Tests config via constructor
*
* @return void
*/
public function testConfigViaConstructor()
{
$Component = new ConfiguredComponent(new ComponentRegistry(), ['chicken' => 'soup']);
$this->assertEquals(['chicken' => 'soup'], $Component->configCopy);
$this->assertEquals(['chicken' => 'soup'], $Component->config());
}
/**
* Lazy load a component without events.
*
* @return void
*/
public function testLazyLoading()
{
$Component = new ConfiguredComponent(new ComponentRegistry(), [], ['Apple', 'Banana', 'Orange']);
$this->assertInstanceOf(AppleComponent::class, $Component->Apple, 'class is wrong');
$this->assertInstanceOf(OrangeComponent::class, $Component->Orange, 'class is wrong');
$this->assertInstanceOf(BananaComponent::class, $Component->Banana, 'class is wrong');
}
/**
* Lazy load a component that does not exist.
*
* @expectedException \Cake\Controller\Exception\MissingComponentException
* @expectedExceptionMessage Component class YouHaveNoBananasComponent could not be found.
* @return void
*/
public function testLazyLoadingDoesNotExists()
{
$Component = new ConfiguredComponent(new ComponentRegistry(), [], ['YouHaveNoBananas']);
$bananas = $Component->YouHaveNoBananas;
}
/**
* Lazy loaded components can have config options
*
* @return void
*/
public function testConfiguringInnerComponent()
{
$Component = new ConfiguredComponent(new ComponentRegistry(), [], ['Configured' => ['foo' => 'bar']]);
$this->assertInstanceOf(ConfiguredComponent::class, $Component->Configured, 'class is wrong');
$this->assertNotSame($Component, $Component->Configured, 'Component instance was reused');
$this->assertEquals(['foo' => 'bar', 'enabled' => false], $Component->Configured->config());
}
/**
* Test enabling events for lazy loaded components
*
* @return void
*/
public function testEventsInnerComponent()
{
$eventManager = $this->getMockBuilder(EventManager::class)->getMock();
$eventManager->expects($this->once())
->method('on')
->with($this->isInstanceOf(AppleComponent::class));
$controller = new Controller();
$controller->eventManager($eventManager);
$Collection = new ComponentRegistry($controller);
$Component = new ConfiguredComponent($Collection, [], ['Apple' => ['enabled' => true]]);
$this->assertInstanceOf(AppleComponent::class, $Component->Apple, 'class is wrong');
}
/**
* Disabled events do not register for event listeners.
*
* @return void
*/
public function testNoEventsInnerComponent()
{
$eventManager = $this->getMockBuilder(EventManager::class)->getMock();
$eventManager->expects($this->never())->method('on');
$controller = new Controller();
$controller->eventManager($eventManager);
$Collection = new ComponentRegistry($controller);
$Component = new ConfiguredComponent($Collection, [], ['Apple' => ['enabled' => false]]);
$this->assertInstanceOf(AppleComponent::class, $Component->Apple, 'class is wrong');
}
}