13
13
*/
14
14
namespace Cake \Test \TestCase \Controller ;
15
15
16
- use Cake \Controller \Component ;
16
+ use Cake \Controller \Component \ CookieComponent ;
17
17
use Cake \Controller \ComponentRegistry ;
18
18
use Cake \Controller \Controller ;
19
- use Cake \Core \App ;
20
19
use Cake \Core \Configure ;
20
+ use Cake \Event \EventManager ;
21
21
use Cake \TestSuite \TestCase ;
22
- use TestApp \Controller \ComponentTestController ;
23
22
use TestApp \Controller \Component \AppleComponent ;
23
+ use TestApp \Controller \Component \BananaComponent ;
24
+ use TestApp \Controller \Component \ConfiguredComponent ;
25
+ use TestApp \Controller \Component \OrangeComponent ;
26
+ use TestApp \Controller \Component \SomethingWithCookieComponent ;
27
+ use TestApp \Controller \ComponentTestController ;
24
28
25
29
/**
26
30
* ComponentTest class
@@ -37,8 +41,6 @@ public function setUp()
37
41
{
38
42
parent ::setUp ();
39
43
Configure::write ('App.namespace ' , 'TestApp ' );
40
-
41
- $ this ->_pluginPaths = App::path ('Plugin ' );
42
44
}
43
45
44
46
/**
@@ -51,7 +53,7 @@ public function testInnerComponentConstruction()
51
53
$ Collection = new ComponentRegistry ();
52
54
$ Component = new AppleComponent ($ Collection );
53
55
54
- $ this ->assertInstanceOf (' TestApp\Controller\Component\ OrangeComponent' , $ Component ->Orange , 'class is wrong ' );
56
+ $ this ->assertInstanceOf (OrangeComponent::class , $ Component ->Orange , 'class is wrong ' );
55
57
}
56
58
57
59
/**
@@ -64,8 +66,8 @@ public function testNestedComponentLoading()
64
66
$ Collection = new ComponentRegistry ();
65
67
$ Apple = new AppleComponent ($ Collection );
66
68
67
- $ this ->assertInstanceOf (' TestApp\Controller\Component\ OrangeComponent' , $ Apple ->Orange , 'class is wrong ' );
68
- $ this ->assertInstanceOf (' TestApp\Controller\Component\ BananaComponent' , $ Apple ->Orange ->Banana , 'class is wrong ' );
69
+ $ this ->assertInstanceOf (OrangeComponent::class , $ Apple ->Orange , 'class is wrong ' );
70
+ $ this ->assertInstanceOf (BananaComponent::class , $ Apple ->Orange ->Banana , 'class is wrong ' );
69
71
$ this ->assertTrue (empty ($ Apple ->Session ));
70
72
$ this ->assertTrue (empty ($ Apple ->Orange ->Session ));
71
73
}
@@ -77,18 +79,18 @@ public function testNestedComponentLoading()
77
79
*/
78
80
public function testInnerComponentsAreNotEnabled ()
79
81
{
80
- $ mock = $ this ->getMockBuilder (' Cake\Event\ EventManager' )->getMock ();
82
+ $ mock = $ this ->getMockBuilder (EventManager::class )->getMock ();
81
83
$ controller = new Controller ();
82
84
$ controller ->eventManager ($ mock );
83
85
84
86
$ mock ->expects ($ this ->once ())
85
87
->method ('on ' )
86
- ->with ($ this ->isInstanceOf (' TestApp\Controller\Component\ AppleComponent' ));
88
+ ->with ($ this ->isInstanceOf (AppleComponent::class ));
87
89
88
90
$ Collection = new ComponentRegistry ($ controller );
89
91
$ Apple = $ Collection ->load ('Apple ' );
90
92
91
- $ this ->assertInstanceOf (' TestApp\Controller\Component\ OrangeComponent' , $ Apple ->Orange , 'class is wrong ' );
93
+ $ this ->assertInstanceOf (OrangeComponent::class , $ Apple ->Orange , 'class is wrong ' );
92
94
}
93
95
94
96
/**
@@ -111,7 +113,7 @@ public function testMultipleComponentInitialize()
111
113
/**
112
114
* Test a duplicate component being loaded more than once with same and differing configurations.
113
115
*
114
- * @expectedException RuntimeException
116
+ * @expectedException \ RuntimeException
115
117
* @expectedExceptionMessage The "Banana" alias has already been loaded with the following config:
116
118
* @return void
117
119
*/
@@ -123,7 +125,7 @@ public function testDuplicateComponentInitialize()
123
125
$ Collection ->load ('Banana ' , ['property ' => ['closure ' => function () {
124
126
}]]);
125
127
126
- $ this ->assertInstanceOf (' TestApp\Controller\Component\ BananaComponent' , $ Collection ->Banana , 'class is wrong ' );
128
+ $ this ->assertInstanceOf (BananaComponent::class , $ Collection ->Banana , 'class is wrong ' );
127
129
128
130
$ Collection ->load ('Banana ' , ['property ' => ['differs ' ]]);
129
131
}
@@ -139,8 +141,8 @@ public function testSomethingReferencingCookieComponent()
139
141
$ Controller ->loadComponent ('SomethingWithCookie ' );
140
142
$ Controller ->startupProcess ();
141
143
142
- $ this ->assertInstanceOf (' TestApp\Controller\Component\ SomethingWithCookieComponent' , $ Controller ->SomethingWithCookie );
143
- $ this ->assertInstanceOf (' Cake\Controller\Component\ CookieComponent' , $ Controller ->SomethingWithCookie ->Cookie );
144
+ $ this ->assertInstanceOf (SomethingWithCookieComponent::class , $ Controller ->SomethingWithCookie );
145
+ $ this ->assertInstanceOf (CookieComponent::class , $ Controller ->SomethingWithCookie ->Cookie );
144
146
}
145
147
146
148
/**
@@ -165,4 +167,106 @@ public function testDebugInfo()
165
167
$ result = $ Component ->__debugInfo ();
166
168
$ this ->assertEquals ($ expected , $ result );
167
169
}
170
+
171
+ /**
172
+ * Tests null return for unknown magic properties.
173
+ *
174
+ * @return void
175
+ */
176
+ public function testMagicReturnsNull ()
177
+ {
178
+ $ Component = new AppleComponent (new ComponentRegistry ());
179
+ $ this ->assertNull ($ Component ->ShouldBeNull );
180
+ }
181
+
182
+ /**
183
+ * Tests config via constructor
184
+ *
185
+ * @return void
186
+ */
187
+ public function testConfigViaConstructor ()
188
+ {
189
+ $ Component = new ConfiguredComponent (new ComponentRegistry (), ['chicken ' => 'soup ' ]);
190
+ $ this ->assertEquals (['chicken ' => 'soup ' ], $ Component ->configCopy );
191
+ $ this ->assertEquals (['chicken ' => 'soup ' ], $ Component ->config ());
192
+ }
193
+
194
+ /**
195
+ * Lazy load a component without events.
196
+ *
197
+ * @return void
198
+ */
199
+ public function testLazyLoading ()
200
+ {
201
+ $ Component = new ConfiguredComponent (new ComponentRegistry (), [], ['Apple ' , 'Banana ' , 'Orange ' ]);
202
+ $ this ->assertInstanceOf (AppleComponent::class, $ Component ->Apple , 'class is wrong ' );
203
+ $ this ->assertInstanceOf (OrangeComponent::class, $ Component ->Orange , 'class is wrong ' );
204
+ $ this ->assertInstanceOf (BananaComponent::class, $ Component ->Banana , 'class is wrong ' );
205
+ }
206
+
207
+ /**
208
+ * Lazy load a component that does not exist.
209
+ *
210
+ * @expectedException \Cake\Controller\Exception\MissingComponentException
211
+ * @expectedExceptionMessage Component class YouHaveNoBananasComponent could not be found.
212
+ * @return void
213
+ */
214
+ public function testLazyLoadingDoesNotExists ()
215
+ {
216
+ $ Component = new ConfiguredComponent (new ComponentRegistry (), [], ['YouHaveNoBananas ' ]);
217
+ $ bananas = $ Component ->YouHaveNoBananas ;
218
+ }
219
+
220
+ /**
221
+ * Lazy loaded components can have config options
222
+ *
223
+ * @return void
224
+ */
225
+ public function testConfiguringInnerComponent ()
226
+ {
227
+ $ Component = new ConfiguredComponent (new ComponentRegistry (), [], ['Configured ' => ['foo ' => 'bar ' ]]);
228
+ $ this ->assertInstanceOf (ConfiguredComponent::class, $ Component ->Configured , 'class is wrong ' );
229
+ $ this ->assertNotSame ($ Component , $ Component ->Configured , 'Component instance was reused ' );
230
+ $ this ->assertEquals (['foo ' => 'bar ' , 'enabled ' => false ], $ Component ->Configured ->config ());
231
+ }
232
+
233
+ /**
234
+ * Test enabling events for lazy loaded components
235
+ *
236
+ * @return void
237
+ */
238
+ public function testEventsInnerComponent ()
239
+ {
240
+ $ eventManager = $ this ->getMockBuilder (EventManager::class)->getMock ();
241
+ $ eventManager ->expects ($ this ->once ())
242
+ ->method ('on ' )
243
+ ->with ($ this ->isInstanceOf (AppleComponent::class));
244
+
245
+ $ controller = new Controller ();
246
+ $ controller ->eventManager ($ eventManager );
247
+
248
+ $ Collection = new ComponentRegistry ($ controller );
249
+
250
+ $ Component = new ConfiguredComponent ($ Collection , [], ['Apple ' => ['enabled ' => true ]]);
251
+ $ this ->assertInstanceOf (AppleComponent::class, $ Component ->Apple , 'class is wrong ' );
252
+ }
253
+
254
+ /**
255
+ * Disabled events do not register for event listeners.
256
+ *
257
+ * @return void
258
+ */
259
+ public function testNoEventsInnerComponent ()
260
+ {
261
+ $ eventManager = $ this ->getMockBuilder (EventManager::class)->getMock ();
262
+ $ eventManager ->expects ($ this ->never ())->method ('on ' );
263
+
264
+ $ controller = new Controller ();
265
+ $ controller ->eventManager ($ eventManager );
266
+
267
+ $ Collection = new ComponentRegistry ($ controller );
268
+
269
+ $ Component = new ConfiguredComponent ($ Collection , [], ['Apple ' => ['enabled ' => false ]]);
270
+ $ this ->assertInstanceOf (AppleComponent::class, $ Component ->Apple , 'class is wrong ' );
271
+ }
168
272
}
0 commit comments