Skip to content

Commit a522f61

Browse files
committed
[FrameworkBundle] WebTestCase KernelBrowser::getContainer null return type
1 parent 519ba3c commit a522f61

File tree

7 files changed

+51
-5
lines changed

7 files changed

+51
-5
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,14 @@ protected static function createKernel(array $options = [])
124124
protected static function ensureKernelShutdown()
125125
{
126126
if (null !== static::$kernel) {
127-
$container = static::$kernel->getContainer();
128-
static::$kernel->shutdown();
129-
if ($container instanceof ResetInterface) {
130-
$container->reset();
127+
$isBooted = (new \ReflectionClass(static::$kernel))->getProperty('booted');
128+
$isBooted->setAccessible(true);
129+
if ($isBooted->getValue(static::$kernel)) {
130+
$container = static::$kernel->getContainer();
131+
static::$kernel->shutdown();
132+
if ($container instanceof ResetInterface) {
133+
$container->reset();
134+
}
131135
}
132136
}
133137
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,12 @@ 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+
return parent::getContainer();
108+
}
101109
}

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CHANGELOG
2525
* renamed `PostResponseEvent` to `TerminateEvent`
2626
* added `HttpClientKernel` for handling requests with an `HttpClientInterface` instance
2727
* added `trace_header` and `trace_level` configuration options to `HttpCache`
28+
* Getting the container from a non-booted kernel is deprecated.
2829

2930
4.2.0
3031
-----

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.3 and will throw an exceptions in Symfony 5.0.', 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 or null when the Kernel is shutdown
137137
*/
138138
public function getContainer();
139139

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public static function tearDownAfterClass()
3636
$fs->remove(__DIR__.'/Fixtures/var');
3737
}
3838

39+
/**
40+
* @group legacy
41+
* @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.3 and will throw an exceptions in Symfony 5.0.
42+
*/
3943
public function testConstructor()
4044
{
4145
$env = 'test_env';
@@ -49,6 +53,10 @@ public function testConstructor()
4953
$this->assertNull($kernel->getContainer());
5054
}
5155

56+
/**
57+
* @group legacy
58+
* @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.3 and will throw an exceptions in Symfony 5.0.
59+
*/
5260
public function testClone()
5361
{
5462
$env = 'test_env';
@@ -486,6 +494,18 @@ public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
486494
$kernel->terminate(Request::create('/'), new Response());
487495
}
488496

497+
/**
498+
* @group legacy
499+
* @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.3 and will throw an exceptions in Symfony 5.0.
500+
*/
501+
public function testDeprecatedNullKernel()
502+
{
503+
$kernel = $this->getKernel();
504+
$kernel->shutdown();
505+
506+
$this->assertNull($kernel->getContainer());
507+
}
508+
489509
public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
490510
{
491511
// 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