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';
$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'));
$this->assertTrue($kernel->isBooted());
}
+ /**
+ * @group legacy
+ */
public function testClassCacheIsLoaded()
{
$kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
$kernel->boot();
}
+ /**
+ * @group legacy
+ */
public function testClassCacheIsNotLoadedWhenKernelIsNotBooted()
{
$kernel = $this->getKernel(array('initializeBundles', 'initializeContainer', 'doLoadClassCache'));
$this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
}
+ /**
+ * @group legacy
+ */
public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
{
$parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
$this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/bar.txt', $kernel->locateResource('@ParentAABundle/bar.txt'));
}
+ /**
+ * @group legacy
+ */
public function testLocateResourceReturnsAllMatches()
{
$parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
$kernel->locateResource('@Bundle1Bundle/foo.txt', null, false));
}
+ /**
+ * @group legacy
+ */
public function testLocateResourceReturnsAllMatchesBis()
{
$kernel = $this->getKernel(array('getBundle'));
);
}
+ /**
+ * @group legacy
+ */
public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes()
{
$kernel = $this->getKernel(array('getBundle'));
);
}
+ /**
+ * @group legacy
+ */
public function testLocateResourceOverrideBundleAndResourcesFolders()
{
$parent = $this->getBundle(__DIR__.'/Fixtures/BaseBundle', null, 'BaseBundle', 'BaseBundle');
);
}
+ /**
+ * @group legacy
+ */
public function testInitializeBundles()
{
$parent = $this->getBundle(null, null, 'ParentABundle');
$this->assertEquals(array($child, $parent), $map['ParentABundle']);
}
+ /**
+ * @group legacy
+ */
public function testInitializeBundlesSupportInheritanceCascade()
{
$grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
}
/**
+ * @group legacy
* @expectedException \LogicException
* @expectedExceptionMessage Bundle "ChildCBundle" extends bundle "FooBar", which is not registered.
*/
$kernel->boot();
}
+ /**
+ * @group legacy
+ */
public function testInitializeBundlesSupportsArbitraryBundleRegistrationOrder()
{
$grandparent = $this->getBundle(null, null, 'GrandParentCBundle');
}
/**
+ * @group legacy
* @expectedException \LogicException
* @expectedExceptionMessage Bundle "ParentCBundle" is directly extended by two bundles "ChildC2Bundle" and "ChildC1Bundle".
*/
}
/**
+ * @group legacy
* @expectedException \LogicException
* @expectedExceptionMessage Trying to register two bundles with the same name "DuplicateName"
*/
}
/**
+ * @group legacy
* @expectedException \LogicException
* @expectedExceptionMessage Bundle "CircularRefBundle" can not extend itself.
*/
$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';
$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.
*
{
}
}
+
+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);
+ }
+}