Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09ec24d

Browse files
bug #45595 [FrameworkBundle] Fix resetting container between tests (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [FrameworkBundle] Fix resetting container between tests | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #40965, fix #45580 | License | MIT | Doc PR | - Replaces #45479 and #45581, related to #34078. Calling `boot()` on an already booted kernel does reset the container, so we don't need to care anymore about the state of kernel. #45580 is fixed by removing `private static $kernelContainer`, which can be out of sync with `static::$kernel->getContainer()` since `KernelBrowser` creates new containers when shutting down the kernel between requests. Commits ------- 4453bdb [FrameworkBundle] Fix resetting container between tests
2 parents f432349 + 4453bdb commit 09ec24d

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/Symfony/Bundle/FrameworkBundle/Client.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\Component\HttpKernel\HttpKernelBrowser;
2020
use Symfony\Component\HttpKernel\KernelInterface;
2121
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
22-
use Symfony\Contracts\Service\ResetInterface;
2322

2423
/**
2524
* Client simulates a browser and makes requests to a Kernel object.
@@ -117,12 +116,8 @@ protected function doRequest($request)
117116
// avoid shutting down the Kernel if no request has been performed yet
118117
// WebTestCase::createClient() boots the Kernel but do not handle a request
119118
if ($this->hasPerformedRequest && $this->reboot) {
120-
$container = $this->kernel->getContainer();
119+
$this->kernel->boot();
121120
$this->kernel->shutdown();
122-
123-
if ($container instanceof ResetInterface) {
124-
$container->reset();
125-
}
126121
} else {
127122
$this->hasPerformedRequest = true;
128123
}

src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\ContainerInterface;
1616
use Symfony\Component\HttpKernel\KernelInterface;
17-
use Symfony\Contracts\Service\ResetInterface;
1817

1918
/**
2019
* KernelTestCase is the base class for tests needing a Kernel.
@@ -39,8 +38,6 @@ abstract class KernelTestCase extends TestCase
3938

4039
protected static $booted = false;
4140

42-
private static $kernelContainer;
43-
4441
private function doTearDown()
4542
{
4643
static::ensureKernelShutdown();
@@ -77,11 +74,12 @@ protected static function bootKernel(array $options = [])
7774
{
7875
static::ensureKernelShutdown();
7976

80-
static::$kernel = static::createKernel($options);
81-
static::$kernel->boot();
77+
$kernel = static::createKernel($options);
78+
$kernel->boot();
79+
self::$kernel = $kernel;
8280
static::$booted = true;
8381

84-
self::$kernelContainer = $container = static::$kernel->getContainer();
82+
$container = static::$kernel->getContainer();
8583
static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
8684

8785
return static::$kernel;
@@ -132,14 +130,11 @@ protected static function createKernel(array $options = [])
132130
protected static function ensureKernelShutdown()
133131
{
134132
if (null !== static::$kernel) {
133+
static::$kernel->boot();
135134
static::$kernel->shutdown();
136135
static::$booted = false;
137136
}
138137

139-
if (self::$kernelContainer instanceof ResetInterface) {
140-
self::$kernelContainer->reset();
141-
}
142-
143-
static::$container = self::$kernelContainer = null;
138+
static::$container = null;
144139
}
145140
}

src/Symfony/Bundle/FrameworkBundle/Tests/KernelBrowserTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ public function testEnableRebootKernel()
5151
$client->request('GET', '/');
5252
}
5353

54+
public function testRequestAfterKernelShutdownAndPerformedRequest()
55+
{
56+
$this->expectNotToPerformAssertions();
57+
58+
$client = static::createClient(['test_case' => 'TestServiceContainer']);
59+
$client->request('GET', '/');
60+
static::ensureKernelShutdown();
61+
$client->request('GET', '/');
62+
}
63+
5464
private function getKernelMock()
5565
{
5666
$mock = $this->getMockBuilder($this->getKernelClass())

0 commit comments

Comments
 (0)