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