Skip to content

Commit 7a2b34e

Browse files
committed
[FrameworkBundle] WebTestCase KernelBrowser::getContainer null return type
1 parent 32389f8 commit 7a2b34e

File tree

9 files changed

+58
-7
lines changed

9 files changed

+58
-7
lines changed

UPGRADE-4.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ HttpKernel
112112
* Implementing the `BundleInterface` without implementing the `getPublicDir()` method is deprecated.
113113
This method will be added to the interface in 5.0.
114114
* The `DebugHandlersListener` class has been marked as `final`
115+
* Getting the container from a non-booted kernel is deprecated.
115116

116117
Lock
117118
----

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ HttpFoundation
284284
* The `FileinfoMimeTypeGuesser` class has been removed,
285285
use `Symfony\Component\Mime\FileinfoMimeTypeGuesser` instead.
286286
* `ApacheRequest` has been removed, use the `Request` class instead.
287+
* Getting the container from a non-booted kernel is not possible anymore.
287288

288289
HttpKernel
289290
----------

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,15 @@ protected static function createKernel(array $options = [])
127127
protected static function ensureKernelShutdown()
128128
{
129129
if (null !== static::$kernel) {
130-
$container = static::$kernel->getContainer();
131-
static::$kernel->shutdown();
132-
static::$booted = false;
133-
if ($container instanceof ResetInterface) {
134-
$container->reset();
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+
}
135139
}
136140
}
137141
static::$container = null;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,13 @@ protected function getKernelParameters()
9696

9797
return $parameters;
9898
}
99+
100+
public function getContainer()
101+
{
102+
if (false === $this->booted) {
103+
throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
104+
}
105+
106+
return parent::getContainer();
107+
}
99108
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,13 @@ protected function getKernelParameters()
9898

9999
return $parameters;
100100
}
101+
102+
public function getContainer()
103+
{
104+
if (false === $this->booted) {
105+
throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
106+
}
107+
108+
return parent::getContainer();
109+
}
101110
}

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Implementing the `BundleInterface` without implementing the `getPublicDir()` method is deprecated.
88
This method will be added to the interface in 5.0.
99
* The `DebugHandlersListener` class has been marked as `final`
10+
* Getting the container from a non-booted kernel is deprecated.
1011

1112
4.3.0
1213
-----

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ public function getProjectDir()
361361
*/
362362
public function getContainer()
363363
{
364+
if (null === $this->container) {
365+
@trigger_error('Getting the container from a non-booted kernel is deprecated since Symfony 4.4.', E_USER_DEPRECATED);
366+
}
367+
364368
return $this->container;
365369
}
366370

src/Symfony/Component/HttpKernel/KernelInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function getRootDir();
133133
/**
134134
* Gets the current container.
135135
*
136-
* @return ContainerInterface|null A ContainerInterface instance or null when the Kernel is shutdown
136+
* @return ContainerInterface A ContainerInterface instance
137137
*/
138138
public function getContainer();
139139

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ public function testConstructor()
4646
$this->assertEquals($debug, $kernel->isDebug());
4747
$this->assertFalse($kernel->isBooted());
4848
$this->assertLessThanOrEqual(microtime(true), $kernel->getStartTime());
49+
}
50+
51+
/**
52+
* @group legacy
53+
* @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.4.
54+
*/
55+
public function testGetContainerForANonBootedKernel()
56+
{
57+
$kernel = new KernelForTest('test_env', true);
58+
59+
$this->assertFalse($kernel->isBooted());
4960
$this->assertNull($kernel->getContainer());
5061
}
5162

@@ -61,7 +72,6 @@ public function testClone()
6172
$this->assertEquals($debug, $clone->isDebug());
6273
$this->assertFalse($clone->isBooted());
6374
$this->assertLessThanOrEqual(microtime(true), $clone->getStartTime());
64-
$this->assertNull($clone->getContainer());
6575
}
6676

6777
public function testClassNameValidityGetter()
@@ -474,6 +484,18 @@ public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
474484
$kernel->terminate(Request::create('/'), new Response());
475485
}
476486

487+
/**
488+
* @group legacy
489+
* @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.4.
490+
*/
491+
public function testDeprecatedNullKernel()
492+
{
493+
$kernel = $this->getKernel();
494+
$kernel->shutdown();
495+
496+
$this->assertNull($kernel->getContainer());
497+
}
498+
477499
public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
478500
{
479501
// does not implement TerminableInterface

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