14
14
use Symfony \Component \HttpKernel \Kernel ;
15
15
use Symfony \Component \HttpKernel \Bundle \BundleInterface ;
16
16
use Symfony \Component \DependencyInjection \Loader \LoaderInterface ;
17
+ use Symfony \Component \HttpKernel \HttpKernelInterface ;
18
+ use Symfony \Component \HttpFoundation \Request ;
17
19
18
20
class KernelTest extends \PHPUnit_Framework_TestCase
21
+ {
22
+ public function testConstructor ()
23
+ {
24
+ $ env = 'test_env ' ;
25
+ $ debug = true ;
26
+ $ kernel = new KernelForTest ($ env , $ debug );
27
+
28
+ $ this ->assertEquals ($ env , $ kernel ->getEnvironment ());
29
+ $ this ->assertEquals ($ debug , $ kernel ->isDebug ());
30
+ $ this ->assertFalse ($ kernel ->isBooted ());
31
+ $ this ->assertLessThanOrEqual (microtime (true ), $ kernel ->getStartTime ());
32
+ $ this ->assertNull ($ kernel ->getContainer ());
33
+ }
34
+
35
+ public function testClone ()
36
+ {
37
+ $ env = 'test_env ' ;
38
+ $ debug = true ;
39
+ $ kernel = new KernelForTest ($ env , $ debug );
40
+
41
+ $ clone = clone $ kernel ;
42
+
43
+ $ this ->assertEquals ($ env , $ clone ->getEnvironment ());
44
+ $ this ->assertEquals ($ debug , $ clone ->isDebug ());
45
+ $ this ->assertFalse ($ clone ->isBooted ());
46
+ $ this ->assertLessThanOrEqual (microtime (true ), $ clone ->getStartTime ());
47
+ $ this ->assertNull ($ clone ->getContainer ());
48
+ }
49
+
50
+ public function testBootInitializesBundlesAndContainer ()
51
+ {
52
+ $ kernel = $ this ->getMockBuilder ('Symfony\Tests\Component\HttpKernel\KernelForTest ' )
53
+ ->disableOriginalConstructor ()
54
+ ->setMethods (array ('initializeBundles ' , 'initializeContainer ' , 'getBundles ' ))
55
+ ->getMock ();
56
+ $ kernel ->expects ($ this ->once ())
57
+ ->method ('initializeBundles ' );
58
+ $ kernel ->expects ($ this ->once ())
59
+ ->method ('initializeContainer ' );
60
+ $ kernel ->expects ($ this ->once ())
61
+ ->method ('getBundles ' )
62
+ ->will ($ this ->returnValue (array ()));
63
+
64
+ $ kernel ->boot ();
65
+ }
66
+
67
+ public function testBootSetsTheContainerToTheBundles ()
68
+ {
69
+ $ bundle = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Bundle\Bundle ' )
70
+ ->disableOriginalConstructor ()
71
+ ->getMock ();
72
+ $ bundle ->expects ($ this ->once ())
73
+ ->method ('setContainer ' );
74
+
75
+ $ kernel = $ this ->getMockBuilder ('Symfony\Tests\Component\HttpKernel\KernelForTest ' )
76
+ ->disableOriginalConstructor ()
77
+ ->setMethods (array ('initializeBundles ' , 'initializeContainer ' , 'getBundles ' ))
78
+ ->getMock ();
79
+ $ kernel ->expects ($ this ->once ())
80
+ ->method ('getBundles ' )
81
+ ->will ($ this ->returnValue (array ($ bundle )));
82
+
83
+ $ kernel ->boot ();
84
+ }
85
+
86
+ public function testBootSetsTheBootedFlagToTrue ()
87
+ {
88
+ $ kernel = $ this ->getMockBuilder ('Symfony\Tests\Component\HttpKernel\KernelForTest ' )
89
+ ->disableOriginalConstructor ()
90
+ ->setMethods (array ('initializeBundles ' , 'initializeContainer ' , 'getBundles ' ))
91
+ ->getMock ();
92
+ $ kernel ->expects ($ this ->once ())
93
+ ->method ('getBundles ' )
94
+ ->will ($ this ->returnValue (array ()));
95
+
96
+ $ kernel ->boot ();
97
+
98
+ $ this ->assertTrue ($ kernel ->isBooted ());
99
+ }
100
+
101
+ public function testShutdownCallsShutdownOnAllBundles ()
102
+ {
103
+ $ bundle = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Bundle\Bundle ' )
104
+ ->disableOriginalConstructor ()
105
+ ->getMock ();
106
+ $ bundle ->expects ($ this ->once ())
107
+ ->method ('shutdown ' );
108
+
109
+ $ kernel = $ this ->getMockBuilder ('Symfony\Tests\Component\HttpKernel\KernelForTest ' )
110
+ ->disableOriginalConstructor ()
111
+ ->setMethods (array ('getBundles ' ))
112
+ ->getMock ();
113
+ $ kernel ->expects ($ this ->once ())
114
+ ->method ('getBundles ' )
115
+ ->will ($ this ->returnValue (array ($ bundle )));
116
+
117
+ $ kernel ->shutdown ();
118
+ }
119
+
120
+ public function testShutdownGivesNullContainerToAllBundles ()
121
+ {
122
+ $ bundle = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Bundle\Bundle ' )
123
+ ->disableOriginalConstructor ()
124
+ ->getMock ();
125
+ $ bundle ->expects ($ this ->once ())
126
+ ->method ('setContainer ' )
127
+ ->with (null );
128
+
129
+ $ kernel = $ this ->getMockBuilder ('Symfony\Tests\Component\HttpKernel\KernelForTest ' )
130
+ ->disableOriginalConstructor ()
131
+ ->setMethods (array ('getBundles ' ))
132
+ ->getMock ();
133
+ $ kernel ->expects ($ this ->once ())
134
+ ->method ('getBundles ' )
135
+ ->will ($ this ->returnValue (array ($ bundle )));
136
+
137
+ $ kernel ->shutdown ();
138
+ }
139
+
140
+ public function testHandleCallsHandleOnHttpKernel ()
141
+ {
142
+ $ type = HttpKernelInterface::MASTER_REQUEST ;
143
+ $ catch = true ;
144
+ $ request = new Request ();
145
+
146
+ $ httpKernelMock = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\HttpKernel ' )
147
+ ->disableOriginalConstructor ()
148
+ ->getMock ();
149
+ $ httpKernelMock
150
+ ->expects ($ this ->once ())
151
+ ->method ('handle ' )
152
+ ->with ($ request , $ type , $ catch );
153
+
154
+ $ kernel = $ this ->getMockBuilder ('Symfony\Tests\Component\HttpKernel\KernelForTest ' )
155
+ ->disableOriginalConstructor ()
156
+ ->setMethods (array ('getHttpKernel ' ))
157
+ ->getMock ();
158
+
159
+ $ kernel ->expects ($ this ->once ())
160
+ ->method ('getHttpKernel ' )
161
+ ->will ($ this ->returnValue ($ httpKernelMock ));
162
+
163
+ $ kernel ->handle ($ request , $ type , $ catch );
164
+ }
165
+
166
+ public function testStripComments ()
167
+ {
168
+ if (!function_exists ('token_get_all ' )) {
169
+ $ this ->markTestSkipped ();
170
+ return ;
171
+ }
172
+ $ source = <<<EOF
173
+ <?php
174
+
175
+ /**
176
+ * some class comments to strip
177
+ */
178
+ class TestClass
19
179
{
180
+ /**
181
+ * some method comments to strip
182
+ */
183
+ public function doStuff()
184
+ {
185
+ // inline comment
186
+ }
187
+ }
188
+ EOF ;
189
+ $ expected = <<<EOF
190
+ <?php
191
+ class TestClass
192
+ {
193
+ public function doStuff()
194
+ {
195
+ }
196
+ }
197
+ EOF ;
198
+
199
+ $ this ->assertEquals ($ expected , Kernel::stripComments ($ source ));
200
+ }
201
+
20
202
/**
21
203
* @expectedException \InvalidArgumentException
22
204
*/
@@ -241,14 +423,14 @@ public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWith
241
423
{
242
424
$ fooBundle = $ this ->getBundle (null , null , 'FooBundle ' , 'DuplicateName ' );
243
425
$ barBundle = $ this ->getBundle (null , null , 'BarBundle ' , 'DuplicateName ' );
244
-
426
+
245
427
$ kernel = $ this ->getKernel ();
246
428
$ kernel
247
429
->expects ($ this ->once ())
248
430
->method ('registerBundles ' )
249
431
->will ($ this ->returnValue (array ($ fooBundle , $ barBundle )))
250
432
;
251
- $ kernel ->initializeBundles ();
433
+ $ kernel ->initializeBundles ();
252
434
}
253
435
254
436
protected function getBundle ($ dir = null , $ parent = null , $ className = null , $ bundleName = null )
@@ -276,13 +458,13 @@ protected function getBundle($dir = null, $parent = null, $className = null, $bu
276
458
->method ('getPath ' )
277
459
->will ($ this ->returnValue (strtr ($ dir , '\\' , '/ ' )))
278
460
;
279
-
461
+
280
462
$ bundle
281
463
->expects ($ this ->any ())
282
464
->method ('getParent ' )
283
465
->will ($ this ->returnValue ($ parent ))
284
466
;
285
-
467
+
286
468
return $ bundle ;
287
469
}
288
470
@@ -333,9 +515,14 @@ public function initializeBundles()
333
515
{
334
516
parent ::initializeBundles ();
335
517
}
518
+
519
+ public function isBooted ()
520
+ {
521
+ return $ this ->booted ;
522
+ }
336
523
}
337
524
338
525
abstract class BundleForTest implements BundleInterface
339
526
{
340
527
// We can not extend Symfony\Component\HttpKernel\Bundle\Bundle as we want to mock getName() which is final
341
- }
528
+ }
0 commit comments