Skip to content

Commit bfd308f

Browse files
bug #34078 [FrameworkBundle] Don't reset the test container but the real one instead (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [FrameworkBundle] Don't reset the test container but the real one instead | Q | A | ------------- | --- | Branch? | 4.4 for features / 3.4 or 4.3 for bug fixes <!-- see below --> | Bug fix? | yes/no | New feature? | yes/no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | yes/no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | - After #31202 and #32056, the tearDown method keeps throwing deprecation notices about "Getting the container from a non-booted kernel". The reason is that resetting the test-container calls `$kernel->getContainer()` while the kernel has been shut down. This fixes it and a few other glitches found meanwhile. Commits ------- 8e16143 [FrameworkBundle] Dont reset the test container but the real one instead
2 parents 5d82cf3 + 8e16143 commit bfd308f

File tree

9 files changed

+21
-23
lines changed

9 files changed

+21
-23
lines changed

UPGRADE-4.4.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Form
9393
FrameworkBundle
9494
---------------
9595

96-
* Deprecated booting the kernel before running `WebTestCase::createClient()`.
96+
* Deprecated calling `WebTestCase::createClient()` while a kernel has been booted, ensure the kernel is shut down before calling the method
9797
* Deprecated support for `templating` engine in `TemplateController`, use Twig instead
9898
* The `$parser` argument of `ControllerResolver::__construct()` and `DelegatingLoader::__construct()`
9999
has been deprecated.

UPGRADE-5.0.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ Form
222222
FrameworkBundle
223223
---------------
224224

225-
* Dropped support for booting the kernel before running `WebTestCase::createClient()`. `createClient()` will throw an
226-
exception if the kernel was already booted before.
225+
* Calling `WebTestCase::createClient()` while a kernel has been booted now throws an exception, ensure the kernel is shut down before calling the method
227226
* Removed the `framework.templating` option, configure the Twig bundle instead.
228227
* The project dir argument of the constructor of `AssetsInstallCommand` is required.
229228
* Removed support for `bundle:controller:action` syntax to reference controllers. Use `serviceOrFqcn::method`

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ CHANGELOG
1010
* Deprecated the `controller_name_converter` and `resolve_controller_name_subscriber` services
1111
* The `ControllerResolver` and `DelegatingLoader` classes have been marked as `final`
1212
* Added support for configuring chained cache pools
13-
* Deprecated booting the kernel before running `WebTestCase::createClient()`
13+
* Deprecated calling `WebTestCase::createClient()` while a kernel has been booted, ensure the kernel is shut down before calling the method
1414
* Deprecated `routing.loader.service`, use `routing.loader.container` instead.
1515
* Not tagging service route loaders with `routing.route_loader` has been deprecated.
1616
* Overriding the methods `KernelTestCase::tearDown()` and `WebTestCase::tearDown()` without the `void` return-type is deprecated.

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ abstract class KernelTestCase extends TestCase
3737
*/
3838
protected static $container;
3939

40-
protected static $booted;
40+
protected static $booted = false;
41+
42+
private static $kernelContainer;
4143

4244
private function doTearDown()
4345
{
@@ -76,7 +78,7 @@ protected static function bootKernel(array $options = [])
7678
static::$kernel->boot();
7779
static::$booted = true;
7880

79-
$container = static::$kernel->getContainer();
81+
self::$kernelContainer = $container = static::$kernel->getContainer();
8082
static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
8183

8284
return static::$kernel;
@@ -127,17 +129,14 @@ protected static function createKernel(array $options = [])
127129
protected static function ensureKernelShutdown()
128130
{
129131
if (null !== static::$kernel) {
130-
$isBooted = (new \ReflectionClass(static::$kernel))->getProperty('booted');
131-
$isBooted->setAccessible(true);
132-
if ($isBooted->getValue(static::$kernel)) {
133-
$container = static::$kernel->getContainer();
134-
static::$kernel->shutdown();
135-
static::$booted = false;
136-
if ($container instanceof ResetInterface) {
137-
$container->reset();
138-
}
139-
}
132+
static::$kernel->shutdown();
133+
static::$booted = false;
134+
}
135+
136+
if (self::$kernelContainer instanceof ResetInterface) {
137+
self::$kernelContainer->reset();
140138
}
141-
static::$container = null;
139+
140+
static::$container = self::$kernelContainer = null;
142141
}
143142
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function initialized($id): bool
119119
*/
120120
public function reset()
121121
{
122-
$this->getPublicContainer()->reset();
122+
// ignore the call
123123
}
124124

125125
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ private function doTearDown()
4141
*/
4242
protected static function createClient(array $options = [], array $server = [])
4343
{
44-
if (true === static::$booted) {
45-
@trigger_error(sprintf('Booting the kernel before calling %s() is deprecated and will throw in Symfony 5.0, the kernel should only be booted once.', __METHOD__), E_USER_DEPRECATED);
44+
if (static::$booted) {
45+
@trigger_error(sprintf('Calling "%s()" while a kernel has been booted is deprecated since Symfony 4.4 and will throw in 5.0, ensure the kernel is shut down before calling the method.', __METHOD__), E_USER_DEPRECATED);
4646
}
4747

4848
$kernel = static::bootKernel($options);

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/AppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected function getKernelParameters(): array
100100

101101
public function getContainer(): ContainerInterface
102102
{
103-
if (!$this->booted) {
103+
if (!$this->container) {
104104
throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
105105
}
106106

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected function getKernelParameters(): array
102102

103103
public function getContainer(): ContainerInterface
104104
{
105-
if (!$this->booted) {
105+
if (!$this->container) {
106106
throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
107107
}
108108

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public function getProjectDir()
380380
*/
381381
public function getContainer()
382382
{
383-
if (!$this->booted) {
383+
if (!$this->container) {
384384
@trigger_error('Getting the container from a non-booted kernel is deprecated since Symfony 4.4.', E_USER_DEPRECATED);
385385
}
386386

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy