Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9ba2943

Browse filesBrowse files
ornicarfabpot
authored andcommitted
[HttpKernel] Add unit tests for Kernel. Also slightly modify Kernel to make it more testable.
1 parent c251a36 commit 9ba2943
Copy full SHA for 9ba2943

File tree

Expand file treeCollapse file tree

2 files changed

+208
-11
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+208
-11
lines changed

‎src/Symfony/Component/HttpKernel/Kernel.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Kernel.php
+16-6Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function boot()
9797
// init container
9898
$this->initializeContainer();
9999

100-
foreach ($this->bundles as $bundle) {
100+
foreach ($this->getBundles() as $bundle) {
101101
$bundle->setContainer($this->container);
102102
$bundle->boot();
103103
}
@@ -114,7 +114,7 @@ public function shutdown()
114114
{
115115
$this->booted = false;
116116

117-
foreach ($this->bundles as $bundle) {
117+
foreach ($this->getBundles() as $bundle) {
118118
$bundle->shutdown();
119119
$bundle->setContainer(null);
120120
}
@@ -131,7 +131,17 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
131131
$this->boot();
132132
}
133133

134-
return $this->container->get('http_kernel')->handle($request, $type, $catch);
134+
return $this->getHttpKernel()->handle($request, $type, $catch);
135+
}
136+
137+
/**
138+
* Gets a http kernel from the container
139+
*
140+
* @return HttpKernel
141+
*/
142+
protected function getHttpKernel()
143+
{
144+
return $this->container->get('http_kernel');
135145
}
136146

137147
/**
@@ -343,7 +353,7 @@ protected function initializeBundles()
343353
$this->bundles = array();
344354
$topMostBundles = array();
345355
$directChildren = array();
346-
356+
347357
foreach ($this->registerBundles() as $bundle) {
348358
$name = $bundle->getName();
349359
if (isset($this->bundles[$name])) {
@@ -358,7 +368,7 @@ protected function initializeBundles()
358368
$directChildren[$parentName] = $name;
359369
} else {
360370
$topMostBundles[$name] = $bundle;
361-
}
371+
}
362372
}
363373

364374
// look for orphans
@@ -377,7 +387,7 @@ protected function initializeBundles()
377387
array_unshift($bundleMap, $this->bundles[$name]);
378388
$hierarchy[] = $name;
379389
}
380-
390+
381391
foreach ($hierarchy as $bundle) {
382392
$this->bundleMap[$bundle] = $bundleMap;
383393
array_pop($bundleMap);

‎tests/Symfony/Tests/Component/HttpKernel/KernelTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Component/HttpKernel/KernelTest.php
+192-5Lines changed: 192 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,191 @@
1414
use Symfony\Component\HttpKernel\Kernel;
1515
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
1616
use Symfony\Component\DependencyInjection\Loader\LoaderInterface;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
18+
use Symfony\Component\HttpFoundation\Request;
1719

1820
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
19179
{
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+
20202
/**
21203
* @expectedException \InvalidArgumentException
22204
*/
@@ -241,14 +423,14 @@ public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWith
241423
{
242424
$fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
243425
$barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
244-
426+
245427
$kernel = $this->getKernel();
246428
$kernel
247429
->expects($this->once())
248430
->method('registerBundles')
249431
->will($this->returnValue(array($fooBundle, $barBundle)))
250432
;
251-
$kernel->initializeBundles();
433+
$kernel->initializeBundles();
252434
}
253435

254436
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
276458
->method('getPath')
277459
->will($this->returnValue(strtr($dir, '\\', '/')))
278460
;
279-
461+
280462
$bundle
281463
->expects($this->any())
282464
->method('getParent')
283465
->will($this->returnValue($parent))
284466
;
285-
467+
286468
return $bundle;
287469
}
288470

@@ -333,9 +515,14 @@ public function initializeBundles()
333515
{
334516
parent::initializeBundles();
335517
}
518+
519+
public function isBooted()
520+
{
521+
return $this->booted;
522+
}
336523
}
337524

338525
abstract class BundleForTest implements BundleInterface
339526
{
340527
// We can not extend Symfony\Component\HttpKernel\Bundle\Bundle as we want to mock getName() which is final
341-
}
528+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.