4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Loader\LoaderInterface;
16 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\Filesystem\Filesystem;
19 use Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\HttpFoundation\Response;
21 use Symfony\Component\HttpKernel\Bundle\BundleInterface;
22 use Symfony\Component\HttpKernel\Config\EnvParametersResource;
23 use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
24 use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
25 use Symfony\Component\HttpKernel\HttpKernelInterface;
26 use Symfony\Component\HttpKernel\Kernel;
27 use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName;
28 use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
29 use Symfony\Component\HttpKernel\Tests\Fixtures\KernelWithoutBundles;
30 use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;
32 class KernelTest extends TestCase
34 public static function tearDownAfterClass()
36 $fs = new Filesystem();
37 $fs->remove(__DIR__.'/Fixtures/cache');
40 public function testConstructor()
44 $kernel = new KernelForTest($env, $debug);
46 $this->assertEquals($env, $kernel->getEnvironment());
47 $this->assertEquals($debug, $kernel->isDebug());
48 $this->assertFalse($kernel->isBooted());
49 $this->assertLessThanOrEqual(microtime(true), $kernel->getStartTime());
50 $this->assertNull($kernel->getContainer());
53 public function testClone()
57 $kernel = new KernelForTest($env, $debug);
59 $clone = clone $kernel;
61 $this->assertEquals($env, $clone->getEnvironment());
62 $this->assertEquals($debug, $clone->isDebug());
63 $this->assertFalse($clone->isBooted());
64 $this->assertLessThanOrEqual(microtime(true), $clone->getStartTime());
65 $this->assertNull($clone->getContainer());
68 public function testInitializeContainerClearsOldContainers()
70 $fs = new Filesystem();
71 $legacyContainerDir = __DIR__.'/Fixtures/cache/custom/ContainerA123456';
72 $fs->mkdir($legacyContainerDir);
73 touch($legacyContainerDir.'.legacy');
75 $kernel = new CustomProjectDirKernel();
78 $containerDir = __DIR__.'/Fixtures/cache/custom/'.substr(\get_class($kernel->getContainer()), 0, 16);
79 $this->assertTrue(unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta'));
80 $this->assertFileExists($containerDir);
81 $this->assertFileNotExists($containerDir.'.legacy');
83 $kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass')->setPublic(true); });
86 $this->assertFileExists($containerDir);
87 $this->assertFileExists($containerDir.'.legacy');
89 $this->assertFileNotExists($legacyContainerDir);
90 $this->assertFileNotExists($legacyContainerDir.'.legacy');
93 public function testBootInitializesBundlesAndContainer()
95 $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer'));
96 $kernel->expects($this->once())
97 ->method('initializeBundles');
98 $kernel->expects($this->once())
99 ->method('initializeContainer');
104 public function testBootSetsTheContainerToTheBundles()
106 $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
107 $bundle->expects($this->once())
108 ->method('setContainer');
110 $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'getBundles'));
111 $kernel->expects($this->once())
112 ->method('getBundles')
113 ->will($this->returnValue(array($bundle)));
118 public function testBootSetsTheBootedFlagToTrue()
120 // use test kernel to access isBooted()
121 $kernel = $this->getKernelForTest(array('initializeBundles', 'initializeContainer'));
124 $this->assertTrue($kernel->isBooted());
130 public function testClassCacheIsLoaded()
132 $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
133 $kernel->loadClassCache('name', '.extension');
134 $kernel->expects($this->once())
135 ->method('doLoadClassCache')
136 ->with('name', '.extension');
141 public function testClassCacheIsNotLoadedByDefault()
143 $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
144 $kernel->expects($this->never())
145 ->method('doLoadClassCache');
153 public function testClassCacheIsNotLoadedWhenKernelIsNotBooted()
155 $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
156 $kernel->loadClassCache();
157 $kernel->expects($this->never())
158 ->method('doLoadClassCache');
161 public function testEnvParametersResourceIsAdded()
163 $container = new ContainerBuilder();
164 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
165 ->disableOriginalConstructor()
166 ->setMethods(array('getContainerBuilder', 'prepareContainer', 'getCacheDir', 'getLogDir'))
168 $kernel->expects($this->any())
169 ->method('getContainerBuilder')
170 ->will($this->returnValue($container));
171 $kernel->expects($this->any())
172 ->method('prepareContainer')
173 ->will($this->returnValue(null));
174 $kernel->expects($this->any())
175 ->method('getCacheDir')
176 ->will($this->returnValue(sys_get_temp_dir()));
177 $kernel->expects($this->any())
178 ->method('getLogDir')
179 ->will($this->returnValue(sys_get_temp_dir()));
181 $reflection = new \ReflectionClass(\get_class($kernel));
182 $method = $reflection->getMethod('buildContainer');
183 $method->setAccessible(true);
184 $method->invoke($kernel);
187 foreach ($container->getResources() as $resource) {
188 if ($resource instanceof EnvParametersResource) {
194 $this->assertTrue($found);
197 public function testBootKernelSeveralTimesOnlyInitializesBundlesOnce()
199 $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer'));
200 $kernel->expects($this->once())
201 ->method('initializeBundles');
207 public function testShutdownCallsShutdownOnAllBundles()
209 $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
210 $bundle->expects($this->once())
211 ->method('shutdown');
213 $kernel = $this->getKernel(array(), array($bundle));
219 public function testShutdownGivesNullContainerToAllBundles()
221 $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')->getMock();
222 $bundle->expects($this->at(3))
223 ->method('setContainer')
226 $kernel = $this->getKernel(array('getBundles'));
227 $kernel->expects($this->any())
228 ->method('getBundles')
229 ->will($this->returnValue(array($bundle)));
235 public function testHandleCallsHandleOnHttpKernel()
237 $type = HttpKernelInterface::MASTER_REQUEST;
239 $request = new Request();
241 $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
242 ->disableOriginalConstructor()
245 ->expects($this->once())
247 ->with($request, $type, $catch);
249 $kernel = $this->getKernel(array('getHttpKernel'));
250 $kernel->expects($this->once())
251 ->method('getHttpKernel')
252 ->will($this->returnValue($httpKernelMock));
254 $kernel->handle($request, $type, $catch);
257 public function testHandleBootsTheKernel()
259 $type = HttpKernelInterface::MASTER_REQUEST;
261 $request = new Request();
263 $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
264 ->disableOriginalConstructor()
267 $kernel = $this->getKernel(array('getHttpKernel', 'boot'));
268 $kernel->expects($this->once())
269 ->method('getHttpKernel')
270 ->will($this->returnValue($httpKernelMock));
272 $kernel->expects($this->once())
275 $kernel->handle($request, $type, $catch);
278 public function testStripComments()
283 $string = 'string should not be modified';
285 $string = 'string should not be
293 Heredoc should not be modified {$a[1+$b]}
301 Nowdoc should not be modified
307 * some class comments to strip
312 * some method comments to strip
314 public function doStuff()
322 $string = 'string should not be modified';
323 $string = 'string should not be
329 Heredoc should not be modified {$a[1+$b]}
336 Nowdoc should not be modified
342 public function doStuff()
348 $output = Kernel::stripComments($source);
350 // Heredocs are preserved, making the output mixing Unix and Windows line
351 // endings, switching to "\n" everywhere on Windows to avoid failure.
352 if ('\\' === \DIRECTORY_SEPARATOR) {
353 $expected = str_replace("\r\n", "\n", $expected);
354 $output = str_replace("\r\n", "\n", $output);
357 $this->assertEquals($expected, $output);
360 public function testGetRootDir()
362 $kernel = new KernelForTest('test', true);
364 $this->assertEquals(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures', realpath($kernel->getRootDir()));
367 public function testGetName()
369 $kernel = new KernelForTest('test', true);
371 $this->assertEquals('Fixtures', $kernel->getName());
374 public function testOverrideGetName()
376 $kernel = new KernelForOverrideName('test', true);
378 $this->assertEquals('overridden', $kernel->getName());
381 public function testSerialize()
385 $kernel = new KernelForTest($env, $debug);
387 $expected = serialize(array($env, $debug));
388 $this->assertEquals($expected, $kernel->serialize());
392 * @expectedException \InvalidArgumentException
394 public function testLocateResourceThrowsExceptionWhenNameIsNotValid()
396 $this->getKernel()->locateResource('Foo');
400 * @expectedException \RuntimeException
402 public function testLocateResourceThrowsExceptionWhenNameIsUnsafe()
404 $this->getKernel()->locateResource('@FooBundle/../bar');
408 * @expectedException \InvalidArgumentException
410 public function testLocateResourceThrowsExceptionWhenBundleDoesNotExist()
412 $this->getKernel()->locateResource('@FooBundle/config/routing.xml');
416 * @expectedException \InvalidArgumentException
418 public function testLocateResourceThrowsExceptionWhenResourceDoesNotExist()
420 $kernel = $this->getKernel(array('getBundle'));
422 ->expects($this->once())
423 ->method('getBundle')
424 ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
427 $kernel->locateResource('@Bundle1Bundle/config/routing.xml');
430 public function testLocateResourceReturnsTheFirstThatMatches()
432 $kernel = $this->getKernel(array('getBundle'));
434 ->expects($this->once())
435 ->method('getBundle')
436 ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
439 $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
445 public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
447 $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
448 $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
450 $kernel = $this->getKernel(array('getBundle'));
452 ->expects($this->exactly(2))
453 ->method('getBundle')
454 ->will($this->returnValue(array($child, $parent)))
457 $this->assertEquals(__DIR__.'/Fixtures/Bundle2Bundle/foo.txt', $kernel->locateResource('@ParentAABundle/foo.txt'));
458 $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/bar.txt', $kernel->locateResource('@ParentAABundle/bar.txt'));
464 public function testLocateResourceReturnsAllMatches()
466 $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
467 $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
469 $kernel = $this->getKernel(array('getBundle'));
471 ->expects($this->once())
472 ->method('getBundle')
473 ->will($this->returnValue(array($child, $parent)))
476 $this->assertEquals(array(
477 __DIR__.'/Fixtures/Bundle2Bundle/foo.txt',
478 __DIR__.'/Fixtures/Bundle1Bundle/foo.txt', ),
479 $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false));
485 public function testLocateResourceReturnsAllMatchesBis()
487 $kernel = $this->getKernel(array('getBundle'));
489 ->expects($this->once())
490 ->method('getBundle')
491 ->will($this->returnValue(array(
492 $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'),
493 $this->getBundle(__DIR__.'/Foobar'),
498 array(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt'),
499 $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false)
503 public function testLocateResourceIgnoresDirOnNonResource()
505 $kernel = $this->getKernel(array('getBundle'));
507 ->expects($this->once())
508 ->method('getBundle')
509 ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
513 __DIR__.'/Fixtures/Bundle1Bundle/foo.txt',
514 $kernel->locateResource('@Bundle1Bundle/foo.txt', __DIR__.'/Fixtures')
518 public function testLocateResourceReturnsTheDirOneForResources()
520 $kernel = $this->getKernel(array('getBundle'));
522 ->expects($this->once())
523 ->method('getBundle')
524 ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle'))))
528 __DIR__.'/Fixtures/Resources/FooBundle/foo.txt',
529 $kernel->locateResource('@FooBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
536 public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes()
538 $kernel = $this->getKernel(array('getBundle'));
540 ->expects($this->once())
541 ->method('getBundle')
542 ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))))
545 $this->assertEquals(array(
546 __DIR__.'/Fixtures/Resources/Bundle1Bundle/foo.txt',
547 __DIR__.'/Fixtures/Bundle1Bundle/Resources/foo.txt', ),
548 $kernel->locateResource('@Bundle1Bundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
555 public function testLocateResourceOverrideBundleAndResourcesFolders()
557 $parent = $this->getBundle(__DIR__.'/Fixtures/BaseBundle', null, 'BaseBundle', 'BaseBundle');
558 $child = $this->getBundle(__DIR__.'/Fixtures/ChildBundle', 'ParentBundle', 'ChildBundle', 'ChildBundle');
560 $kernel = $this->getKernel(array('getBundle'));
562 ->expects($this->exactly(4))
563 ->method('getBundle')
564 ->will($this->returnValue(array($child, $parent)))
567 $this->assertEquals(array(
568 __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
569 __DIR__.'/Fixtures/ChildBundle/Resources/foo.txt',
570 __DIR__.'/Fixtures/BaseBundle/Resources/foo.txt',
572 $kernel->locateResource('@BaseBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
576 __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
577 $kernel->locateResource('@BaseBundle/Resources/foo.txt', __DIR__.'/Fixtures/Resources')
581 $kernel->locateResource('@BaseBundle/Resources/hide.txt', __DIR__.'/Fixtures/Resources', false);
582 $this->fail('Hidden resources should raise an exception when returning an array of matching paths');
583 } catch (\RuntimeException $e) {
587 $kernel->locateResource('@BaseBundle/Resources/hide.txt', __DIR__.'/Fixtures/Resources', true);
588 $this->fail('Hidden resources should raise an exception when returning the first matching path');
589 } catch (\RuntimeException $e) {
593 public function testLocateResourceOnDirectories()
595 $kernel = $this->getKernel(array('getBundle'));
597 ->expects($this->exactly(2))
598 ->method('getBundle')
599 ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/FooBundle', null, null, 'FooBundle'))))
603 __DIR__.'/Fixtures/Resources/FooBundle/',
604 $kernel->locateResource('@FooBundle/Resources/', __DIR__.'/Fixtures/Resources')
607 __DIR__.'/Fixtures/Resources/FooBundle',
608 $kernel->locateResource('@FooBundle/Resources', __DIR__.'/Fixtures/Resources')
611 $kernel = $this->getKernel(array('getBundle'));
613 ->expects($this->exactly(2))
614 ->method('getBundle')
615 ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1Bundle'))))
619 __DIR__.'/Fixtures/Bundle1Bundle/Resources/',
620 $kernel->locateResource('@Bundle1Bundle/Resources/')
623 __DIR__.'/Fixtures/Bundle1Bundle/Resources',
624 $kernel->locateResource('@Bundle1Bundle/Resources')
631 public function testInitializeBundles()
633 $parent = $this->getBundle(null, null, 'ParentABundle');
634 $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
636 // use test kernel so we can access getBundleMap()
637 $kernel = $this->getKernelForTest(array('registerBundles'));
639 ->expects($this->once())
640 ->method('registerBundles')
641 ->will($this->returnValue(array($parent, $child)))
645 $map = $kernel->getBundleMap();
646 $this->assertEquals(array($child, $parent), $map['ParentABundle']);
652 public function testInitializeBundlesSupportInheritanceCascade()
654 $grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
655 $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
656 $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
658 // use test kernel so we can access getBundleMap()
659 $kernel = $this->getKernelForTest(array('registerBundles'));
661 ->expects($this->once())
662 ->method('registerBundles')
663 ->will($this->returnValue(array($grandparent, $parent, $child)))
667 $map = $kernel->getBundleMap();
668 $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentBBundle']);
669 $this->assertEquals(array($child, $parent), $map['ParentBBundle']);
670 $this->assertEquals(array($child), $map['ChildBBundle']);
675 * @expectedException \LogicException
676 * @expectedExceptionMessage Bundle "ChildCBundle" extends bundle "FooBar", which is not registered.
678 public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists()
680 $child = $this->getBundle(null, 'FooBar', 'ChildCBundle');
681 $kernel = $this->getKernel(array(), array($child));
688 public function testInitializeBundlesSupportsArbitraryBundleRegistrationOrder()
690 $grandparent = $this->getBundle(null, null, 'GrandParentCBundle');
691 $parent = $this->getBundle(null, 'GrandParentCBundle', 'ParentCBundle');
692 $child = $this->getBundle(null, 'ParentCBundle', 'ChildCBundle');
694 // use test kernel so we can access getBundleMap()
695 $kernel = $this->getKernelForTest(array('registerBundles'));
697 ->expects($this->once())
698 ->method('registerBundles')
699 ->will($this->returnValue(array($parent, $grandparent, $child)))
703 $map = $kernel->getBundleMap();
704 $this->assertEquals(array($child, $parent, $grandparent), $map['GrandParentCBundle']);
705 $this->assertEquals(array($child, $parent), $map['ParentCBundle']);
706 $this->assertEquals(array($child), $map['ChildCBundle']);
711 * @expectedException \LogicException
712 * @expectedExceptionMessage Bundle "ParentCBundle" is directly extended by two bundles "ChildC2Bundle" and "ChildC1Bundle".
714 public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
716 $parent = $this->getBundle(null, null, 'ParentCBundle');
717 $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
718 $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
720 $kernel = $this->getKernel(array(), array($parent, $child1, $child2));
726 * @expectedException \LogicException
727 * @expectedExceptionMessage Trying to register two bundles with the same name "DuplicateName"
729 public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
731 $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
732 $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
734 $kernel = $this->getKernel(array(), array($fooBundle, $barBundle));
740 * @expectedException \LogicException
741 * @expectedExceptionMessage Bundle "CircularRefBundle" can not extend itself.
743 public function testInitializeBundleThrowsExceptionWhenABundleExtendsItself()
745 $circularRef = $this->getBundle(null, 'CircularRefBundle', 'CircularRefBundle');
747 $kernel = $this->getKernel(array(), array($circularRef));
751 public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
753 $kernel = $this->getKernel(array('getHttpKernel'));
754 $kernel->expects($this->never())
755 ->method('getHttpKernel');
757 $kernel->terminate(Request::create('/'), new Response());
760 public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
762 // does not implement TerminableInterface
763 $httpKernel = new TestKernel();
765 $kernel = $this->getKernel(array('getHttpKernel'));
766 $kernel->expects($this->once())
767 ->method('getHttpKernel')
768 ->willReturn($httpKernel);
771 $kernel->terminate(Request::create('/'), new Response());
773 $this->assertFalse($httpKernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');
775 // implements TerminableInterface
776 $httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
777 ->disableOriginalConstructor()
778 ->setMethods(array('terminate'))
782 ->expects($this->once())
783 ->method('terminate');
785 $kernel = $this->getKernel(array('getHttpKernel'));
786 $kernel->expects($this->exactly(2))
787 ->method('getHttpKernel')
788 ->will($this->returnValue($httpKernelMock));
791 $kernel->terminate(Request::create('/'), new Response());
794 public function testKernelWithoutBundles()
796 $kernel = new KernelWithoutBundles('test', true);
799 $this->assertTrue($kernel->getContainer()->getParameter('test_executed'));
802 public function testKernelRootDirNameStartingWithANumber()
804 $dir = __DIR__.'/Fixtures/123';
805 require_once $dir.'/Kernel123.php';
806 $kernel = new \Symfony\Component\HttpKernel\Tests\Fixtures\_123\Kernel123('dev', true);
807 $this->assertEquals('_123', $kernel->getName());
812 * @expectedDeprecation The "Symfony\Component\HttpKernel\Kernel::getEnvParameters()" method is deprecated as of 3.3 and will be removed in 4.0. Use the %cenv()%c syntax to get the value of any environment variable from configuration files instead.
813 * @expectedDeprecation The support of special environment variables that start with SYMFONY__ (such as "SYMFONY__FOO__BAR") is deprecated as of 3.3 and will be removed in 4.0. Use the %cenv()%c syntax instead to get the value of environment variables in configuration files.
815 public function testSymfonyEnvironmentVariables()
817 $_SERVER['SYMFONY__FOO__BAR'] = 'baz';
819 $kernel = $this->getKernel();
820 $method = new \ReflectionMethod($kernel, 'getEnvParameters');
821 $method->setAccessible(true);
823 $envParameters = $method->invoke($kernel);
824 $this->assertSame('baz', $envParameters['foo.bar']);
826 unset($_SERVER['SYMFONY__FOO__BAR']);
829 public function testProjectDirExtension()
831 $kernel = new CustomProjectDirKernel();
834 $this->assertSame('foo', $kernel->getProjectDir());
835 $this->assertSame('foo', $kernel->getContainer()->getParameter('kernel.project_dir'));
838 public function testKernelReset()
840 (new Filesystem())->remove(__DIR__.'/Fixtures/cache');
842 $kernel = new CustomProjectDirKernel();
845 $containerClass = \get_class($kernel->getContainer());
846 $containerFile = (new \ReflectionClass($kernel->getContainer()))->getFileName();
847 unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta');
849 $kernel = new CustomProjectDirKernel();
852 $this->assertInstanceOf($containerClass, $kernel->getContainer());
853 $this->assertFileExists($containerFile);
854 unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta');
856 $kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass')->setPublic(true); });
859 $this->assertNotInstanceOf($containerClass, $kernel->getContainer());
860 $this->assertFileExists($containerFile);
861 $this->assertFileExists(\dirname($containerFile).'.legacy');
864 public function testKernelPass()
866 $kernel = new PassKernel();
869 $this->assertTrue($kernel->getContainer()->getParameter('test.processed'));
872 public function testServicesResetter()
874 $httpKernelMock = $this->getMockBuilder(HttpKernelInterface::class)
875 ->disableOriginalConstructor()
878 ->expects($this->exactly(2))
881 $kernel = new CustomProjectDirKernel(function ($container) {
882 $container->addCompilerPass(new ResettableServicePass());
883 $container->register('one', ResettableService::class)
885 ->addTag('kernel.reset', array('method' => 'reset'));
886 $container->register('services_resetter', ServicesResetter::class)->setPublic(true);
887 }, $httpKernelMock, 'resetting');
889 ResettableService::$counter = 0;
891 $request = new Request();
893 $kernel->handle($request);
894 $kernel->getContainer()->get('one');
896 $this->assertEquals(0, ResettableService::$counter);
897 $this->assertFalse($kernel->getContainer()->initialized('services_resetter'));
899 $kernel->handle($request);
901 $this->assertEquals(1, ResettableService::$counter);
905 * @group time-sensitive
907 public function testKernelStartTimeIsResetWhileBootingAlreadyBootedKernel()
909 $kernel = $this->getKernelForTest(array('initializeBundles'), true);
911 $preReBoot = $kernel->getStartTime();
913 sleep(3600); //Intentionally large value to detect if ClockMock ever breaks
914 $kernel->reboot(null);
916 $this->assertGreaterThan($preReBoot, $kernel->getStartTime());
920 * Returns a mock for the BundleInterface.
922 * @return BundleInterface
924 protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
927 ->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')
928 ->setMethods(array('getPath', 'getParent', 'getName'))
929 ->disableOriginalConstructor()
933 $bundle->setMockClassName($className);
936 $bundle = $bundle->getMockForAbstractClass();
939 ->expects($this->any())
941 ->will($this->returnValue(null === $bundleName ? \get_class($bundle) : $bundleName))
945 ->expects($this->any())
947 ->will($this->returnValue($dir))
951 ->expects($this->any())
952 ->method('getParent')
953 ->will($this->returnValue($parent))
960 * Returns a mock for the abstract kernel.
962 * @param array $methods Additional methods to mock (besides the abstract ones)
963 * @param array $bundles Bundles to register
967 protected function getKernel(array $methods = array(), array $bundles = array())
969 $methods[] = 'registerBundles';
972 ->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
973 ->setMethods($methods)
974 ->setConstructorArgs(array('test', false))
975 ->getMockForAbstractClass()
977 $kernel->expects($this->any())
978 ->method('registerBundles')
979 ->will($this->returnValue($bundles))
981 $p = new \ReflectionProperty($kernel, 'rootDir');
982 $p->setAccessible(true);
983 $p->setValue($kernel, __DIR__.'/Fixtures');
988 protected function getKernelForTest(array $methods = array(), $debug = false)
990 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
991 ->setConstructorArgs(array('test', $debug))
992 ->setMethods($methods)
994 $p = new \ReflectionProperty($kernel, 'rootDir');
995 $p->setAccessible(true);
996 $p->setValue($kernel, __DIR__.'/Fixtures');
1002 class TestKernel implements HttpKernelInterface
1004 public $terminateCalled = false;
1006 public function terminate()
1008 $this->terminateCalled = true;
1011 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
1016 class CustomProjectDirKernel extends Kernel
1019 private $buildContainer;
1020 private $httpKernel;
1022 public function __construct(\Closure $buildContainer = null, HttpKernelInterface $httpKernel = null, $name = 'custom')
1024 parent::__construct($name, true);
1026 $this->baseDir = 'foo';
1027 $this->buildContainer = $buildContainer;
1028 $this->httpKernel = $httpKernel;
1031 public function registerBundles()
1036 public function registerContainerConfiguration(LoaderInterface $loader)
1040 public function getProjectDir()
1042 return $this->baseDir;
1045 public function getRootDir()
1047 return __DIR__.'/Fixtures';
1050 protected function build(ContainerBuilder $container)
1052 if ($build = $this->buildContainer) {
1057 protected function getHttpKernel()
1059 return $this->httpKernel;
1063 class PassKernel extends CustomProjectDirKernel implements CompilerPassInterface
1065 public function __construct()
1067 parent::__construct();
1068 Kernel::__construct('pass', true);
1071 public function process(ContainerBuilder $container)
1073 $container->setParameter('test.processed', true);