X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fsymfony%2Fhttp-kernel%2FTests%2FKernelTest.php;h=ab0b8cfb6ec6bd35c449e419322f52a2084fc8bf;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hp=501b6240d9d987ec0832f84675b82e59761f112f;hpb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;p=yaffs-website diff --git a/vendor/symfony/http-kernel/Tests/KernelTest.php b/vendor/symfony/http-kernel/Tests/KernelTest.php index 501b6240d..ab0b8cfb6 100644 --- a/vendor/symfony/http-kernel/Tests/KernelTest.php +++ b/vendor/symfony/http-kernel/Tests/KernelTest.php @@ -12,18 +12,31 @@ namespace Symfony\Component\HttpKernel\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Bundle\BundleInterface; use Symfony\Component\HttpKernel\Config\EnvParametersResource; -use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass; +use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter; use Symfony\Component\HttpKernel\HttpKernelInterface; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest; +use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName; +use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest; +use Symfony\Component\HttpKernel\Tests\Fixtures\KernelWithoutBundles; +use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService; class KernelTest extends TestCase { + public static function tearDownAfterClass() + { + $fs = new Filesystem(); + $fs->remove(__DIR__.'/Fixtures/cache'); + } + public function testConstructor() { $env = 'test_env'; @@ -52,6 +65,31 @@ class KernelTest extends TestCase $this->assertNull($clone->getContainer()); } + public function testInitializeContainerClearsOldContainers() + { + $fs = new Filesystem(); + $legacyContainerDir = __DIR__.'/Fixtures/cache/custom/ContainerA123456'; + $fs->mkdir($legacyContainerDir); + touch($legacyContainerDir.'.legacy'); + + $kernel = new CustomProjectDirKernel(); + $kernel->boot(); + + $containerDir = __DIR__.'/Fixtures/cache/custom/'.substr(\get_class($kernel->getContainer()), 0, 16); + $this->assertTrue(unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta')); + $this->assertFileExists($containerDir); + $this->assertFileNotExists($containerDir.'.legacy'); + + $kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass')->setPublic(true); }); + $kernel->boot(); + + $this->assertFileExists($containerDir); + $this->assertFileExists($containerDir.'.legacy'); + + $this->assertFileNotExists($legacyContainerDir); + $this->assertFileNotExists($legacyContainerDir.'.legacy'); + } + public function testBootInitializesBundlesAndContainer() { $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer')); @@ -86,6 +124,9 @@ class KernelTest extends TestCase $this->assertTrue($kernel->isBooted()); } + /** + * @group legacy + */ public function testClassCacheIsLoaded() { $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache')); @@ -106,6 +147,9 @@ class KernelTest extends TestCase $kernel->boot(); } + /** + * @group legacy + */ public function testClassCacheIsNotLoadedWhenKernelIsNotBooted() { $kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache')); @@ -134,7 +178,7 @@ class KernelTest extends TestCase ->method('getLogDir') ->will($this->returnValue(sys_get_temp_dir())); - $reflection = new \ReflectionClass(get_class($kernel)); + $reflection = new \ReflectionClass(\get_class($kernel)); $method = $reflection->getMethod('buildContainer'); $method->setAccessible(true); $method->invoke($kernel); @@ -305,7 +349,7 @@ EOF; // Heredocs are preserved, making the output mixing Unix and Windows line // endings, switching to "\n" everywhere on Windows to avoid failure. - if ('\\' === DIRECTORY_SEPARATOR) { + if ('\\' === \DIRECTORY_SEPARATOR) { $expected = str_replace("\r\n", "\n", $expected); $output = str_replace("\r\n", "\n", $output); } @@ -317,7 +361,7 @@ EOF; { $kernel = new KernelForTest('test', true); - $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures', realpath($kernel->getRootDir())); + $this->assertEquals(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures', realpath($kernel->getRootDir())); } public function testGetName() @@ -395,6 +439,9 @@ EOF; $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt')); } + /** + * @group legacy + */ public function testLocateResourceReturnsTheFirstThatMatchesWithParent() { $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'); @@ -411,6 +458,9 @@ EOF; $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/bar.txt', $kernel->locateResource('@ParentAABundle/bar.txt')); } + /** + * @group legacy + */ public function testLocateResourceReturnsAllMatches() { $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'); @@ -429,6 +479,9 @@ EOF; $kernel->locateResource('@Bundle1Bundle/foo.txt', null, false)); } + /** + * @group legacy + */ public function testLocateResourceReturnsAllMatchesBis() { $kernel = $this->getKernel(array('getBundle')); @@ -477,6 +530,9 @@ EOF; ); } + /** + * @group legacy + */ public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes() { $kernel = $this->getKernel(array('getBundle')); @@ -493,6 +549,9 @@ EOF; ); } + /** + * @group legacy + */ public function testLocateResourceOverrideBundleAndResourcesFolders() { $parent = $this->getBundle(__DIR__.'/Fixtures/BaseBundle', null, 'BaseBundle', 'BaseBundle'); @@ -566,6 +625,9 @@ EOF; ); } + /** + * @group legacy + */ public function testInitializeBundles() { $parent = $this->getBundle(null, null, 'ParentABundle'); @@ -584,6 +646,9 @@ EOF; $this->assertEquals(array($child, $parent), $map['ParentABundle']); } + /** + * @group legacy + */ public function testInitializeBundlesSupportInheritanceCascade() { $grandparent = $this->getBundle(null, null, 'GrandParentBBundle'); @@ -606,6 +671,7 @@ EOF; } /** + * @group legacy * @expectedException \LogicException * @expectedExceptionMessage Bundle "ChildCBundle" extends bundle "FooBar", which is not registered. */ @@ -616,6 +682,9 @@ EOF; $kernel->boot(); } + /** + * @group legacy + */ public function testInitializeBundlesSupportsArbitraryBundleRegistrationOrder() { $grandparent = $this->getBundle(null, null, 'GrandParentCBundle'); @@ -638,6 +707,7 @@ EOF; } /** + * @group legacy * @expectedException \LogicException * @expectedExceptionMessage Bundle "ParentCBundle" is directly extended by two bundles "ChildC2Bundle" and "ChildC1Bundle". */ @@ -652,6 +722,7 @@ EOF; } /** + * @group legacy * @expectedException \LogicException * @expectedExceptionMessage Trying to register two bundles with the same name "DuplicateName" */ @@ -665,6 +736,7 @@ EOF; } /** + * @group legacy * @expectedException \LogicException * @expectedExceptionMessage Bundle "CircularRefBundle" can not extend itself. */ @@ -719,6 +791,14 @@ EOF; $kernel->terminate(Request::create('/'), new Response()); } + public function testKernelWithoutBundles() + { + $kernel = new KernelWithoutBundles('test', true); + $kernel->boot(); + + $this->assertTrue($kernel->getContainer()->getParameter('test_executed')); + } + public function testKernelRootDirNameStartingWithANumber() { $dir = __DIR__.'/Fixtures/123'; @@ -727,6 +807,115 @@ EOF; $this->assertEquals('_123', $kernel->getName()); } + /** + * @group legacy + * @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. + * @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. + */ + public function testSymfonyEnvironmentVariables() + { + $_SERVER['SYMFONY__FOO__BAR'] = 'baz'; + + $kernel = $this->getKernel(); + $method = new \ReflectionMethod($kernel, 'getEnvParameters'); + $method->setAccessible(true); + + $envParameters = $method->invoke($kernel); + $this->assertSame('baz', $envParameters['foo.bar']); + + unset($_SERVER['SYMFONY__FOO__BAR']); + } + + public function testProjectDirExtension() + { + $kernel = new CustomProjectDirKernel(); + $kernel->boot(); + + $this->assertSame('foo', $kernel->getProjectDir()); + $this->assertSame('foo', $kernel->getContainer()->getParameter('kernel.project_dir')); + } + + public function testKernelReset() + { + (new Filesystem())->remove(__DIR__.'/Fixtures/cache'); + + $kernel = new CustomProjectDirKernel(); + $kernel->boot(); + + $containerClass = \get_class($kernel->getContainer()); + $containerFile = (new \ReflectionClass($kernel->getContainer()))->getFileName(); + unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta'); + + $kernel = new CustomProjectDirKernel(); + $kernel->boot(); + + $this->assertInstanceOf($containerClass, $kernel->getContainer()); + $this->assertFileExists($containerFile); + unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta'); + + $kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass')->setPublic(true); }); + $kernel->boot(); + + $this->assertNotInstanceOf($containerClass, $kernel->getContainer()); + $this->assertFileExists($containerFile); + $this->assertFileExists(\dirname($containerFile).'.legacy'); + } + + public function testKernelPass() + { + $kernel = new PassKernel(); + $kernel->boot(); + + $this->assertTrue($kernel->getContainer()->getParameter('test.processed')); + } + + public function testServicesResetter() + { + $httpKernelMock = $this->getMockBuilder(HttpKernelInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $httpKernelMock + ->expects($this->exactly(2)) + ->method('handle'); + + $kernel = new CustomProjectDirKernel(function ($container) { + $container->addCompilerPass(new ResettableServicePass()); + $container->register('one', ResettableService::class) + ->setPublic(true) + ->addTag('kernel.reset', array('method' => 'reset')); + $container->register('services_resetter', ServicesResetter::class)->setPublic(true); + }, $httpKernelMock, 'resetting'); + + ResettableService::$counter = 0; + + $request = new Request(); + + $kernel->handle($request); + $kernel->getContainer()->get('one'); + + $this->assertEquals(0, ResettableService::$counter); + $this->assertFalse($kernel->getContainer()->initialized('services_resetter')); + + $kernel->handle($request); + + $this->assertEquals(1, ResettableService::$counter); + } + + /** + * @group time-sensitive + */ + public function testKernelStartTimeIsResetWhileBootingAlreadyBootedKernel() + { + $kernel = $this->getKernelForTest(array('initializeBundles'), true); + $kernel->boot(); + $preReBoot = $kernel->getStartTime(); + + sleep(3600); //Intentionally large value to detect if ClockMock ever breaks + $kernel->reboot(null); + + $this->assertGreaterThan($preReBoot, $kernel->getStartTime()); + } + /** * Returns a mock for the BundleInterface. * @@ -749,7 +938,7 @@ EOF; $bundle ->expects($this->any()) ->method('getName') - ->will($this->returnValue(null === $bundleName ? get_class($bundle) : $bundleName)) + ->will($this->returnValue(null === $bundleName ? \get_class($bundle) : $bundleName)) ; $bundle @@ -796,10 +985,10 @@ EOF; return $kernel; } - protected function getKernelForTest(array $methods = array()) + protected function getKernelForTest(array $methods = array(), $debug = false) { $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest') - ->setConstructorArgs(array('test', false)) + ->setConstructorArgs(array('test', $debug)) ->setMethods($methods) ->getMock(); $p = new \ReflectionProperty($kernel, 'rootDir'); @@ -823,3 +1012,64 @@ class TestKernel implements HttpKernelInterface { } } + +class CustomProjectDirKernel extends Kernel +{ + private $baseDir; + private $buildContainer; + private $httpKernel; + + public function __construct(\Closure $buildContainer = null, HttpKernelInterface $httpKernel = null, $name = 'custom') + { + parent::__construct($name, true); + + $this->baseDir = 'foo'; + $this->buildContainer = $buildContainer; + $this->httpKernel = $httpKernel; + } + + public function registerBundles() + { + return array(); + } + + public function registerContainerConfiguration(LoaderInterface $loader) + { + } + + public function getProjectDir() + { + return $this->baseDir; + } + + public function getRootDir() + { + return __DIR__.'/Fixtures'; + } + + protected function build(ContainerBuilder $container) + { + if ($build = $this->buildContainer) { + $build($container); + } + } + + protected function getHttpKernel() + { + return $this->httpKernel; + } +} + +class PassKernel extends CustomProjectDirKernel implements CompilerPassInterface +{ + public function __construct() + { + parent::__construct(); + Kernel::__construct('pass', true); + } + + public function process(ContainerBuilder $container) + { + $container->setParameter('test.processed', true); + } +}