dad52401daf398558d62aa54b7b9689605d7f55d
[yaffs-website] / vendor / symfony / http-kernel / Tests / KernelTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\HttpKernel\Bundle\BundleInterface;
17 use Symfony\Component\HttpKernel\Config\EnvParametersResource;
18 use Symfony\Component\HttpKernel\Kernel;
19 use Symfony\Component\HttpKernel\HttpKernelInterface;
20 use Symfony\Component\HttpFoundation\Request;
21 use Symfony\Component\HttpFoundation\Response;
22 use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
23 use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName;
24 use Symfony\Component\HttpKernel\Tests\Fixtures\FooBarBundle;
25
26 class KernelTest extends TestCase
27 {
28     public function testConstructor()
29     {
30         $env = 'test_env';
31         $debug = true;
32         $kernel = new KernelForTest($env, $debug);
33
34         $this->assertEquals($env, $kernel->getEnvironment());
35         $this->assertEquals($debug, $kernel->isDebug());
36         $this->assertFalse($kernel->isBooted());
37         $this->assertLessThanOrEqual(microtime(true), $kernel->getStartTime());
38         $this->assertNull($kernel->getContainer());
39     }
40
41     public function testClone()
42     {
43         $env = 'test_env';
44         $debug = true;
45         $kernel = new KernelForTest($env, $debug);
46
47         $clone = clone $kernel;
48
49         $this->assertEquals($env, $clone->getEnvironment());
50         $this->assertEquals($debug, $clone->isDebug());
51         $this->assertFalse($clone->isBooted());
52         $this->assertLessThanOrEqual(microtime(true), $clone->getStartTime());
53         $this->assertNull($clone->getContainer());
54     }
55
56     public function testBootInitializesBundlesAndContainer()
57     {
58         $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer'));
59         $kernel->expects($this->once())
60             ->method('initializeBundles');
61         $kernel->expects($this->once())
62             ->method('initializeContainer');
63
64         $kernel->boot();
65     }
66
67     public function testBootSetsTheContainerToTheBundles()
68     {
69         $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
70         $bundle->expects($this->once())
71             ->method('setContainer');
72
73         $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'getBundles'));
74         $kernel->expects($this->once())
75             ->method('getBundles')
76             ->will($this->returnValue(array($bundle)));
77
78         $kernel->boot();
79     }
80
81     public function testBootSetsTheBootedFlagToTrue()
82     {
83         // use test kernel to access isBooted()
84         $kernel = $this->getKernelForTest(array('initializeBundles', 'initializeContainer'));
85         $kernel->boot();
86
87         $this->assertTrue($kernel->isBooted());
88     }
89
90     public function testClassCacheIsLoaded()
91     {
92         $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
93         $kernel->loadClassCache('name', '.extension');
94         $kernel->expects($this->once())
95             ->method('doLoadClassCache')
96             ->with('name', '.extension');
97
98         $kernel->boot();
99     }
100
101     public function testClassCacheIsNotLoadedByDefault()
102     {
103         $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
104         $kernel->expects($this->never())
105             ->method('doLoadClassCache');
106
107         $kernel->boot();
108     }
109
110     public function testClassCacheIsNotLoadedWhenKernelIsNotBooted()
111     {
112         $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
113         $kernel->loadClassCache();
114         $kernel->expects($this->never())
115             ->method('doLoadClassCache');
116     }
117
118     public function testEnvParametersResourceIsAdded()
119     {
120         $container = new ContainerBuilder();
121         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
122             ->disableOriginalConstructor()
123             ->setMethods(array('getContainerBuilder', 'prepareContainer', 'getCacheDir', 'getLogDir'))
124             ->getMock();
125         $kernel->expects($this->any())
126             ->method('getContainerBuilder')
127             ->will($this->returnValue($container));
128         $kernel->expects($this->any())
129             ->method('prepareContainer')
130             ->will($this->returnValue(null));
131         $kernel->expects($this->any())
132             ->method('getCacheDir')
133             ->will($this->returnValue(sys_get_temp_dir()));
134         $kernel->expects($this->any())
135             ->method('getLogDir')
136             ->will($this->returnValue(sys_get_temp_dir()));
137
138         $reflection = new \ReflectionClass(get_class($kernel));
139         $method = $reflection->getMethod('buildContainer');
140         $method->setAccessible(true);
141         $method->invoke($kernel);
142
143         $found = false;
144         foreach ($container->getResources() as $resource) {
145             if ($resource instanceof EnvParametersResource) {
146                 $found = true;
147                 break;
148             }
149         }
150
151         $this->assertTrue($found);
152     }
153
154     public function testBootKernelSeveralTimesOnlyInitializesBundlesOnce()
155     {
156         $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer'));
157         $kernel->expects($this->once())
158             ->method('initializeBundles');
159
160         $kernel->boot();
161         $kernel->boot();
162     }
163
164     public function testShutdownCallsShutdownOnAllBundles()
165     {
166         $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
167         $bundle->expects($this->once())
168             ->method('shutdown');
169
170         $kernel = $this->getKernel(array(), array($bundle));
171
172         $kernel->boot();
173         $kernel->shutdown();
174     }
175
176     public function testShutdownGivesNullContainerToAllBundles()
177     {
178         $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
179         $bundle->expects($this->at(3))
180             ->method('setContainer')
181             ->with(null);
182
183         $kernel = $this->getKernel(array('getBundles'));
184         $kernel->expects($this->any())
185             ->method('getBundles')
186             ->will($this->returnValue(array($bundle)));
187
188         $kernel->boot();
189         $kernel->shutdown();
190     }
191
192     public function testHandleCallsHandleOnHttpKernel()
193     {
194         $type = HttpKernelInterface::MASTER_REQUEST;
195         $catch = true;
196         $request = new Request();
197
198         $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
199             ->disableOriginalConstructor()
200             ->getMock();
201         $httpKernelMock
202             ->expects($this->once())
203             ->method('handle')
204             ->with($request, $type, $catch);
205
206         $kernel = $this->getKernel(array('getHttpKernel'));
207         $kernel->expects($this->once())
208             ->method('getHttpKernel')
209             ->will($this->returnValue($httpKernelMock));
210
211         $kernel->handle($request, $type, $catch);
212     }
213
214     public function testHandleBootsTheKernel()
215     {
216         $type = HttpKernelInterface::MASTER_REQUEST;
217         $catch = true;
218         $request = new Request();
219
220         $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
221             ->disableOriginalConstructor()
222             ->getMock();
223
224         $kernel = $this->getKernel(array('getHttpKernel', 'boot'));
225         $kernel->expects($this->once())
226             ->method('getHttpKernel')
227             ->will($this->returnValue($httpKernelMock));
228
229         $kernel->expects($this->once())
230             ->method('boot');
231
232         $kernel->handle($request, $type, $catch);
233     }
234
235     public function testStripComments()
236     {
237         $source = <<<'EOF'
238 <?php
239
240 $string = 'string should not be   modified';
241
242 $string = 'string should not be
243
244 modified';
245
246
247 $heredoc = <<<HD
248
249
250 Heredoc should not be   modified {$a[1+$b]}
251
252
253 HD;
254
255 $nowdoc = <<<'ND'
256
257
258 Nowdoc should not be   modified
259
260
261 ND;
262
263 /**
264  * some class comments to strip
265  */
266 class TestClass
267 {
268     /**
269      * some method comments to strip
270      */
271     public function doStuff()
272     {
273         // inline comment
274     }
275 }
276 EOF;
277         $expected = <<<'EOF'
278 <?php
279 $string = 'string should not be   modified';
280 $string = 'string should not be
281
282 modified';
283 $heredoc = <<<HD
284
285
286 Heredoc should not be   modified {$a[1+$b]}
287
288
289 HD;
290 $nowdoc = <<<'ND'
291
292
293 Nowdoc should not be   modified
294
295
296 ND;
297 class TestClass
298 {
299     public function doStuff()
300     {
301         }
302 }
303 EOF;
304
305         $output = Kernel::stripComments($source);
306
307         // Heredocs are preserved, making the output mixing Unix and Windows line
308         // endings, switching to "\n" everywhere on Windows to avoid failure.
309         if ('\\' === DIRECTORY_SEPARATOR) {
310             $expected = str_replace("\r\n", "\n", $expected);
311             $output = str_replace("\r\n", "\n", $output);
312         }
313
314         $this->assertEquals($expected, $output);
315     }
316
317     /**
318      * @group legacy
319      */
320     public function testLegacyIsClassInActiveBundleFalse()
321     {
322         $kernel = $this->getKernelMockForIsClassInActiveBundleTest();
323
324         $this->assertFalse($kernel->isClassInActiveBundle('Not\In\Active\Bundle'));
325     }
326
327     /**
328      * @group legacy
329      */
330     public function testLegacyIsClassInActiveBundleFalseNoNamespace()
331     {
332         $kernel = $this->getKernelMockForIsClassInActiveBundleTest();
333
334         $this->assertFalse($kernel->isClassInActiveBundle('NotNamespacedClass'));
335     }
336
337     /**
338      * @group legacy
339      */
340     public function testLegacyIsClassInActiveBundleTrue()
341     {
342         $kernel = $this->getKernelMockForIsClassInActiveBundleTest();
343
344         $this->assertTrue($kernel->isClassInActiveBundle(__NAMESPACE__.'\Fixtures\FooBarBundle\SomeClass'));
345     }
346
347     protected function getKernelMockForIsClassInActiveBundleTest()
348     {
349         $bundle = new FooBarBundle();
350
351         $kernel = $this->getKernel(array('getBundles'));
352         $kernel->expects($this->once())
353             ->method('getBundles')
354             ->will($this->returnValue(array($bundle)));
355
356         return $kernel;
357     }
358
359     public function testGetRootDir()
360     {
361         $kernel = new KernelForTest('test', true);
362
363         $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures', realpath($kernel->getRootDir()));
364     }
365
366     public function testGetName()
367     {
368         $kernel = new KernelForTest('test', true);
369
370         $this->assertEquals('Fixtures', $kernel->getName());
371     }
372
373     public function testOverrideGetName()
374     {
375         $kernel = new KernelForOverrideName('test', true);
376
377         $this->assertEquals('overridden', $kernel->getName());
378     }
379
380     public function testSerialize()
381     {
382         $env = 'test_env';
383         $debug = true;
384         $kernel = new KernelForTest($env, $debug);
385
386         $expected = serialize(array($env, $debug));
387         $this->assertEquals($expected, $kernel->serialize());
388     }
389
390     /**
391      * @expectedException \InvalidArgumentException
392      */
393     public function testLocateResourceThrowsExceptionWhenNameIsNotValid()
394     {
395         $this->getKernel()->locateResource('Foo');
396     }
397
398     /**
399      * @expectedException \RuntimeException
400      */
401     public function testLocateResourceThrowsExceptionWhenNameIsUnsafe()
402     {
403         $this->getKernel()->locateResource('@FooBundle/../bar');
404     }
405
406     /**
407      * @expectedException \InvalidArgumentException
408      */
409     public function testLocateResourceThrowsExceptionWhenBundleDoesNotExist()
410     {
411         $this->getKernel()->locateResource('@FooBundle/config/routing.xml');
412     }
413
414     /**
415      * @expectedException \InvalidArgumentException
416      */
417     public function testLocateResourceThrowsExceptionWhenResourceDoesNotExist()
418     {
419         $kernel = $this->getKernel(array('getBundle'));
420         $kernel
421             ->expects($this->once())
422             ->method('getBundle')
423             ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
424         ;
425
426         $kernel->locateResource('@Bundle1Bundle/config/routing.xml');
427     }
428
429     public function testLocateResourceReturnsTheFirstThatMatches()
430     {
431         $kernel = $this->getKernel(array('getBundle'));
432         $kernel
433             ->expects($this->once())
434             ->method('getBundle')
435             ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
436         ;
437
438         $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
439     }
440
441     public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
442     {
443         $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
444         $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
445
446         $kernel = $this->getKernel(array('getBundle'));
447         $kernel
448             ->expects($this->exactly(2))
449             ->method('getBundle')
450             ->will($this->returnValue(array($child, $parent)))
451         ;
452
453         $this->assertEquals(__DIR__.'/Fixtures/Bundle2Bundle/foo.txt', $kernel->locateResource('@ParentAABundle/foo.txt'));
454         $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/bar.txt', $kernel->locateResource('@ParentAABundle/bar.txt'));
455     }
456
457     public function testLocateResourceReturnsAllMatches()
458     {
459         $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
460         $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
461
462         $kernel = $this->getKernel(array('getBundle'));
463         $kernel
464             ->expects($this->once())
465             ->method('getBundle')
466             ->will($this->returnValue(array($child, $parent)))
467         ;
468
469         $this->assertEquals(array(
470             __DIR__.'/Fixtures/Bundle2Bundle/foo.txt',
471             __DIR__.'/Fixtures/Bundle1Bundle/foo.txt', ),
472             $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false));
473     }
474
475     public function testLocateResourceReturnsAllMatchesBis()
476     {
477         $kernel = $this->getKernel(array('getBundle'));
478         $kernel
479             ->expects($this->once())
480             ->method('getBundle')
481             ->will($this->returnValue(array(
482                 $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'),
483                 $this->getBundle(__DIR__.'/Foobar'),
484             )))
485         ;
486
487         $this->assertEquals(
488             array(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt'),
489             $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false)
490         );
491     }
492
493     public function testLocateResourceIgnoresDirOnNonResource()
494     {
495         $kernel = $this->getKernel(array('getBundle'));
496         $kernel
497             ->expects($this->once())
498             ->method('getBundle')
499             ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
500         ;
501
502         $this->assertEquals(
503             __DIR__.'/Fixtures/Bundle1Bundle/foo.txt',
504             $kernel->locateResource('@Bundle1Bundle/foo.txt', __DIR__.'/Fixtures')
505         );
506     }
507
508     public function testLocateResourceReturnsTheDirOneForResources()
509     {
510         $kernel = $this->getKernel(array('getBundle'));
511         $kernel
512             ->expects($this->once())
513             ->method('getBundle')
514             ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle'))))
515         ;
516
517         $this->assertEquals(
518             __DIR__.'/Fixtures/Resources/FooBundle/foo.txt',
519             $kernel->locateResource('@FooBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
520         );
521     }
522
523     public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes()
524     {
525         $kernel = $this->getKernel(array('getBundle'));
526         $kernel
527             ->expects($this->once())
528             ->method('getBundle')
529             ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))))
530         ;
531
532         $this->assertEquals(array(
533             __DIR__.'/Fixtures/Resources/Bundle1Bundle/foo.txt',
534             __DIR__.'/Fixtures/Bundle1Bundle/Resources/foo.txt', ),
535             $kernel->locateResource('@Bundle1Bundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
536         );
537     }
538
539     public function testLocateResourceOverrideBundleAndResourcesFolders()
540     {
541         $parent = $this->getBundle(__DIR__.'/Fixtures/BaseBundle', null, 'BaseBundle', 'BaseBundle');
542         $child = $this->getBundle(__DIR__.'/Fixtures/ChildBundle', 'ParentBundle', 'ChildBundle', 'ChildBundle');
543
544         $kernel = $this->getKernel(array('getBundle'));
545         $kernel
546             ->expects($this->exactly(4))
547             ->method('getBundle')
548             ->will($this->returnValue(array($child, $parent)))
549         ;
550
551         $this->assertEquals(array(
552             __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
553             __DIR__.'/Fixtures/ChildBundle/Resources/foo.txt',
554             __DIR__.'/Fixtures/BaseBundle/Resources/foo.txt',
555             ),
556             $kernel->locateResource('@BaseBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
557         );
558
559         $this->assertEquals(
560             __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
561             $kernel->locateResource('@BaseBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
562         );
563
564         try {
565             $kernel->locateResource('@BaseBundle/Resources/hide.txt', __DIR__.'/Fixtures/Resources', false);
566             $this->fail('Hidden resources should raise an exception when returning an array of matching paths');
567         } catch (\RuntimeException $e) {
568         }
569
570         try {
571             $kernel->locateResource('@BaseBundle/Resources/hide.txt', __DIR__.'/Fixtures/Resources', true);
572             $this->fail('Hidden resources should raise an exception when returning the first matching path');
573         } catch (\RuntimeException $e) {
574         }
575     }
576
577     public function testLocateResourceOnDirectories()
578     {
579         $kernel = $this->getKernel(array('getBundle'));
580         $kernel
581             ->expects($this->exactly(2))
582             ->method('getBundle')
583             ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle'))))
584         ;
585
586         $this->assertEquals(
587             __DIR__.'/Fixtures/Resources/FooBundle/',
588             $kernel->locateResource('@FooBundle/Resources/', __DIR__.'/Fixtures/Resources')
589         );
590         $this->assertEquals(
591             __DIR__.'/Fixtures/Resources/FooBundle',
592             $kernel->locateResource('@FooBundle/Resources', __DIR__.'/Fixtures/Resources')
593         );
594
595         $kernel = $this->getKernel(array('getBundle'));
596         $kernel
597             ->expects($this->exactly(2))
598             ->method('getBundle')
599             ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))))
600         ;
601
602         $this->assertEquals(
603             __DIR__.'/Fixtures/Bundle1Bundle/Resources/',
604             $kernel->locateResource('@Bundle1Bundle/Resources/')
605         );
606         $this->assertEquals(
607             __DIR__.'/Fixtures/Bundle1Bundle/Resources',
608             $kernel->locateResource('@Bundle1Bundle/Resources')
609         );
610     }
611
612     public function testInitializeBundles()
613     {
614         $parent = $this->getBundle(null, null, 'ParentABundle');
615         $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
616
617         // use test kernel so we can access getBundleMap()
618         $kernel = $this->getKernelForTest(array('registerBundles'));
619         $kernel
620             ->expects($this->once())
621             ->method('registerBundles')
622             ->will($this->returnValue(array($parent, $child)))
623         ;
624         $kernel->boot();
625
626         $map = $kernel->getBundleMap();
627         $this->assertEquals(array($child, $parent), $map['ParentABundle']);
628     }
629
630     public function testInitializeBundlesSupportInheritanceCascade()
631     {
632         $grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
633         $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
634         $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
635
636         // use test kernel so we can access getBundleMap()
637         $kernel = $this->getKernelForTest(array('registerBundles'));
638         $kernel
639             ->expects($this->once())
640             ->method('registerBundles')
641             ->will($this->returnValue(array($grandparent, $parent, $child)))
642         ;
643         $kernel->boot();
644
645         $map = $kernel->getBundleMap();
646         $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentBBundle']);
647         $this->assertEquals(array($child, $parent), $map['ParentBBundle']);
648         $this->assertEquals(array($child), $map['ChildBBundle']);
649     }
650
651     /**
652      * @expectedException \LogicException
653      * @expectedExceptionMessage Bundle "ChildCBundle" extends bundle "FooBar", which is not registered.
654      */
655     public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists()
656     {
657         $child = $this->getBundle(null, 'FooBar', 'ChildCBundle');
658         $kernel = $this->getKernel(array(), array($child));
659         $kernel->boot();
660     }
661
662     public function testInitializeBundlesSupportsArbitraryBundleRegistrationOrder()
663     {
664         $grandparent = $this->getBundle(null, null, 'GrandParentCBundle');
665         $parent = $this->getBundle(null, 'GrandParentCBundle', 'ParentCBundle');
666         $child = $this->getBundle(null, 'ParentCBundle', 'ChildCBundle');
667
668         // use test kernel so we can access getBundleMap()
669         $kernel = $this->getKernelForTest(array('registerBundles'));
670         $kernel
671             ->expects($this->once())
672             ->method('registerBundles')
673             ->will($this->returnValue(array($parent, $grandparent, $child)))
674         ;
675         $kernel->boot();
676
677         $map = $kernel->getBundleMap();
678         $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentCBundle']);
679         $this->assertEquals(array($child, $parent), $map['ParentCBundle']);
680         $this->assertEquals(array($child), $map['ChildCBundle']);
681     }
682
683     /**
684      * @expectedException \LogicException
685      * @expectedExceptionMessage Bundle "ParentCBundle" is directly extended by two bundles "ChildC2Bundle" and "ChildC1Bundle".
686      */
687     public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
688     {
689         $parent = $this->getBundle(null, null, 'ParentCBundle');
690         $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
691         $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
692
693         $kernel = $this->getKernel(array(), array($parent, $child1, $child2));
694         $kernel->boot();
695     }
696
697     /**
698      * @expectedException \LogicException
699      * @expectedExceptionMessage Trying to register two bundles with the same name "DuplicateName"
700      */
701     public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
702     {
703         $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
704         $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
705
706         $kernel = $this->getKernel(array(), array($fooBundle, $barBundle));
707         $kernel->boot();
708     }
709
710     /**
711      * @expectedException \LogicException
712      * @expectedExceptionMessage Bundle "CircularRefBundle" can not extend itself.
713      */
714     public function testInitializeBundleThrowsExceptionWhenABundleExtendsItself()
715     {
716         $circularRef = $this->getBundle(null, 'CircularRefBundle', 'CircularRefBundle');
717
718         $kernel = $this->getKernel(array(), array($circularRef));
719         $kernel->boot();
720     }
721
722     public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
723     {
724         $kernel = $this->getKernel(array('getHttpKernel'));
725         $kernel->expects($this->never())
726             ->method('getHttpKernel');
727
728         $kernel->terminate(Request::create('/'), new Response());
729     }
730
731     public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
732     {
733         // does not implement TerminableInterface
734         $httpKernel = new TestKernel();
735
736         $kernel = $this->getKernel(array('getHttpKernel'));
737         $kernel->expects($this->once())
738             ->method('getHttpKernel')
739             ->willReturn($httpKernel);
740
741         $kernel->boot();
742         $kernel->terminate(Request::create('/'), new Response());
743
744         $this->assertFalse($httpKernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');
745
746         // implements TerminableInterface
747         $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
748             ->disableOriginalConstructor()
749             ->setMethods(array('terminate'))
750             ->getMock();
751
752         $httpKernelMock
753             ->expects($this->once())
754             ->method('terminate');
755
756         $kernel = $this->getKernel(array('getHttpKernel'));
757         $kernel->expects($this->exactly(2))
758             ->method('getHttpKernel')
759             ->will($this->returnValue($httpKernelMock));
760
761         $kernel->boot();
762         $kernel->terminate(Request::create('/'), new Response());
763     }
764
765     public function testKernelRootDirNameStartingWithANumber()
766     {
767         $dir = __DIR__.'/Fixtures/123';
768         require_once $dir.'/Kernel123.php';
769         $kernel = new \Symfony\Component\HttpKernel\Tests\Fixtures\_123\Kernel123('dev', true);
770         $this->assertEquals('_123', $kernel->getName());
771     }
772
773     /**
774      * Returns a mock for the BundleInterface.
775      *
776      * @return BundleInterface
777      */
778     protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
779     {
780         $bundle = $this
781             ->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')
782             ->setMethods(array('getPath', 'getParent', 'getName'))
783             ->disableOriginalConstructor()
784         ;
785
786         if ($className) {
787             $bundle->setMockClassName($className);
788         }
789
790         $bundle = $bundle->getMockForAbstractClass();
791
792         $bundle
793             ->expects($this->any())
794             ->method('getName')
795             ->will($this->returnValue(null === $bundleName ? get_class($bundle) : $bundleName))
796         ;
797
798         $bundle
799             ->expects($this->any())
800             ->method('getPath')
801             ->will($this->returnValue($dir))
802         ;
803
804         $bundle
805             ->expects($this->any())
806             ->method('getParent')
807             ->will($this->returnValue($parent))
808         ;
809
810         return $bundle;
811     }
812
813     /**
814      * Returns a mock for the abstract kernel.
815      *
816      * @param array $methods Additional methods to mock (besides the abstract ones)
817      * @param array $bundles Bundles to register
818      *
819      * @return Kernel
820      */
821     protected function getKernel(array $methods = array(), array $bundles = array())
822     {
823         $methods[] = 'registerBundles';
824
825         $kernel = $this
826             ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
827             ->setMethods($methods)
828             ->setConstructorArgs(array('test', false))
829             ->getMockForAbstractClass()
830         ;
831         $kernel->expects($this->any())
832             ->method('registerBundles')
833             ->will($this->returnValue($bundles))
834         ;
835         $p = new \ReflectionProperty($kernel, 'rootDir');
836         $p->setAccessible(true);
837         $p->setValue($kernel, __DIR__.'/Fixtures');
838
839         return $kernel;
840     }
841
842     protected function getKernelForTest(array $methods = array())
843     {
844         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
845             ->setConstructorArgs(array('test', false))
846             ->setMethods($methods)
847             ->getMock();
848         $p = new \ReflectionProperty($kernel, 'rootDir');
849         $p->setAccessible(true);
850         $p->setValue($kernel, __DIR__.'/Fixtures');
851
852         return $kernel;
853     }
854 }
855
856 class TestKernel implements HttpKernelInterface
857 {
858     public $terminateCalled = false;
859
860     public function terminate()
861     {
862         $this->terminateCalled = true;
863     }
864
865     public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
866     {
867     }
868 }