From 4453bdb74959821f2168ecaade0ff44bd54a7a56 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 1 Mar 2022 13:09:24 +0100 Subject: [PATCH] [FrameworkBundle] Fix resetting container between tests --- src/Symfony/Bundle/FrameworkBundle/Client.php | 7 +------ .../FrameworkBundle/Test/KernelTestCase.php | 17 ++++++----------- .../FrameworkBundle/Tests/KernelBrowserTest.php | 10 ++++++++++ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Client.php b/src/Symfony/Bundle/FrameworkBundle/Client.php index a27a7141dfba..97b19cce3a49 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Client.php +++ b/src/Symfony/Bundle/FrameworkBundle/Client.php @@ -19,7 +19,6 @@ use Symfony\Component\HttpKernel\HttpKernelBrowser; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile; -use Symfony\Contracts\Service\ResetInterface; /** * Client simulates a browser and makes requests to a Kernel object. @@ -117,12 +116,8 @@ protected function doRequest($request) // avoid shutting down the Kernel if no request has been performed yet // WebTestCase::createClient() boots the Kernel but do not handle a request if ($this->hasPerformedRequest && $this->reboot) { - $container = $this->kernel->getContainer(); + $this->kernel->boot(); $this->kernel->shutdown(); - - if ($container instanceof ResetInterface) { - $container->reset(); - } } else { $this->hasPerformedRequest = true; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php index b87018bbd6b3..a65a17b4c53e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php +++ b/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php @@ -14,7 +14,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpKernel\KernelInterface; -use Symfony\Contracts\Service\ResetInterface; /** * KernelTestCase is the base class for tests needing a Kernel. @@ -39,8 +38,6 @@ abstract class KernelTestCase extends TestCase protected static $booted = false; - private static $kernelContainer; - private function doTearDown() { static::ensureKernelShutdown(); @@ -77,11 +74,12 @@ protected static function bootKernel(array $options = []) { static::ensureKernelShutdown(); - static::$kernel = static::createKernel($options); - static::$kernel->boot(); + $kernel = static::createKernel($options); + $kernel->boot(); + self::$kernel = $kernel; static::$booted = true; - self::$kernelContainer = $container = static::$kernel->getContainer(); + $container = static::$kernel->getContainer(); static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container; return static::$kernel; @@ -132,14 +130,11 @@ protected static function createKernel(array $options = []) protected static function ensureKernelShutdown() { if (null !== static::$kernel) { + static::$kernel->boot(); static::$kernel->shutdown(); static::$booted = false; } - if (self::$kernelContainer instanceof ResetInterface) { - self::$kernelContainer->reset(); - } - - static::$container = self::$kernelContainer = null; + static::$container = null; } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/KernelBrowserTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/KernelBrowserTest.php index 96eaaea7612a..404a239b5128 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/KernelBrowserTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/KernelBrowserTest.php @@ -51,6 +51,16 @@ public function testEnableRebootKernel() $client->request('GET', '/'); } + public function testRequestAfterKernelShutdownAndPerformedRequest() + { + $this->expectNotToPerformAssertions(); + + $client = static::createClient(['test_case' => 'TestServiceContainer']); + $client->request('GET', '/'); + static::ensureKernelShutdown(); + $client->request('GET', '/'); + } + private function getKernelMock() { $mock = $this->getMockBuilder($this->getKernelClass())