Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / http-kernel / Tests / KernelTest.php
index 501b6240d9d987ec0832f84675b82e59761f112f..a16ac37deebce81104629b3f2b7e6a6a3085f316 100644 (file)
 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\HttpKernel\Bundle\BundleInterface;
 use Symfony\Component\HttpKernel\Config\EnvParametersResource;
+use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
+use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
 use Symfony\Component\HttpKernel\Kernel;
 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\Tests\Fixtures\KernelForOverrideName;
+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'));
@@ -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,100 @@ 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);
+    }
+
     /**
      * Returns a mock for the BundleInterface.
      *
@@ -823,3 +997,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);
+    }
+}