feature #31202 [FrameworkBundle] WebTestCase KernelBrowser::getContainer null return type (Simperfit)
This PR was merged into the 4.4 branch.
Discussion
----------
[FrameworkBundle] WebTestCase KernelBrowser::getContainer null return type
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | no
| New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks? | no <!-- see https://symfony.com/bc -->
| Deprecations? | yes <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass? | yes <!-- please add some, will be required by reviewers -->
| Fixed tickets | #25920 <!-- #-prefixed issue number(s), if any -->
| License | MIT
| Doc PR | <!-- required for new features -->
<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
- Bug fixes must be submitted against the lowest branch where they apply
(lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the master branch.
-->
As @stof suggested in the #25920 I started deprecating the behaviour of returning null when the container is non booted / null.
If this is not wanted we should close both the PR and the issue ;).
Commits
-------
e169e1a4d5
[FrameworkBundle] WebTestCase KernelBrowser::getContainer null return type
This commit is contained in:
commit
979be2958d
@ -148,6 +148,7 @@ HttpKernel
|
||||
fallback directories. Resources like service definitions are usually loaded relative to the
|
||||
current directory or with a glob pattern. The fallback directories have never been advocated
|
||||
so you likely do not use those in any app based on the SF Standard or Flex edition.
|
||||
* Getting the container from a non-booted kernel is deprecated
|
||||
|
||||
Lock
|
||||
----
|
||||
|
@ -288,6 +288,7 @@ HttpFoundation
|
||||
use `Symfony\Component\Mime\FileinfoMimeTypeGuesser` instead.
|
||||
* `ApacheRequest` has been removed, use the `Request` class instead.
|
||||
* The third argument of the `HeaderBag::get()` method has been removed, use method `all()` instead.
|
||||
* Getting the container from a non-booted kernel is not possible anymore.
|
||||
|
||||
HttpKernel
|
||||
----------
|
||||
|
@ -127,6 +127,9 @@ abstract class KernelTestCase extends TestCase
|
||||
protected static function ensureKernelShutdown()
|
||||
{
|
||||
if (null !== static::$kernel) {
|
||||
$isBooted = (new \ReflectionClass(static::$kernel))->getProperty('booted');
|
||||
$isBooted->setAccessible(true);
|
||||
if ($isBooted->getValue(static::$kernel)) {
|
||||
$container = static::$kernel->getContainer();
|
||||
static::$kernel->shutdown();
|
||||
static::$booted = false;
|
||||
@ -134,6 +137,7 @@ abstract class KernelTestCase extends TestCase
|
||||
$container->reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
static::$container = null;
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\app;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
|
||||
@ -96,4 +97,13 @@ class AppKernel extends Kernel
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
public function getContainer(): ContainerInterface
|
||||
{
|
||||
if (!$this->booted) {
|
||||
throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
|
||||
}
|
||||
|
||||
return parent::getContainer();
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\app;
|
||||
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
|
||||
@ -98,4 +99,13 @@ class AppKernel extends Kernel
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
public function getContainer(): ContainerInterface
|
||||
{
|
||||
if (!$this->booted) {
|
||||
throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
|
||||
}
|
||||
|
||||
return parent::getContainer();
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ CHANGELOG
|
||||
so you likely do not use those in any app based on the SF Standard or Flex edition.
|
||||
* Marked all dispatched event classes as `@final`
|
||||
* Added `ErrorController` to enable the preview and error rendering mechanism
|
||||
* Getting the container from a non-booted kernel is deprecated.
|
||||
|
||||
4.3.0
|
||||
-----
|
||||
|
@ -380,6 +380,10 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
|
||||
*/
|
||||
public function getContainer()
|
||||
{
|
||||
if (!$this->booted) {
|
||||
@trigger_error('Getting the container from a non-booted kernel is deprecated since Symfony 4.4.', E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
return $this->container;
|
||||
}
|
||||
|
||||
|
@ -124,7 +124,7 @@ interface KernelInterface extends HttpKernelInterface
|
||||
/**
|
||||
* Gets the current container.
|
||||
*
|
||||
* @return ContainerInterface|null A ContainerInterface instance or null when the Kernel is shutdown
|
||||
* @return ContainerInterface
|
||||
*/
|
||||
public function getContainer();
|
||||
|
||||
|
@ -46,6 +46,17 @@ class KernelTest extends TestCase
|
||||
$this->assertEquals($debug, $kernel->isDebug());
|
||||
$this->assertFalse($kernel->isBooted());
|
||||
$this->assertLessThanOrEqual(microtime(true), $kernel->getStartTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.4.
|
||||
*/
|
||||
public function testGetContainerForANonBootedKernel()
|
||||
{
|
||||
$kernel = new KernelForTest('test_env', true);
|
||||
|
||||
$this->assertFalse($kernel->isBooted());
|
||||
$this->assertNull($kernel->getContainer());
|
||||
}
|
||||
|
||||
@ -61,7 +72,6 @@ class KernelTest extends TestCase
|
||||
$this->assertEquals($debug, $clone->isDebug());
|
||||
$this->assertFalse($clone->isBooted());
|
||||
$this->assertLessThanOrEqual(microtime(true), $clone->getStartTime());
|
||||
$this->assertNull($clone->getContainer());
|
||||
}
|
||||
|
||||
public function testClassNameValidityGetter()
|
||||
@ -486,6 +496,18 @@ EOF;
|
||||
$kernel->terminate(Request::create('/'), new Response());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.4.
|
||||
*/
|
||||
public function testDeprecatedNullKernel()
|
||||
{
|
||||
$kernel = $this->getKernel();
|
||||
$kernel->shutdown();
|
||||
|
||||
$this->assertNull($kernel->getContainer());
|
||||
}
|
||||
|
||||
public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
|
||||
{
|
||||
// does not implement TerminableInterface
|
||||
|
Reference in New Issue
Block a user