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:
Fabien Potencier 2019-09-27 12:15:03 +02:00
commit 979be2958d
9 changed files with 87 additions and 34 deletions

View File

@ -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
----

View File

@ -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
----------

View File

@ -127,11 +127,15 @@ abstract class KernelTestCase extends TestCase
protected static function ensureKernelShutdown()
{
if (null !== static::$kernel) {
$container = static::$kernel->getContainer();
static::$kernel->shutdown();
static::$booted = false;
if ($container instanceof ResetInterface) {
$container->reset();
$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;
if ($container instanceof ResetInterface) {
$container->reset();
}
}
}
static::$container = null;

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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
-----

View File

@ -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;
}

View File

@ -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();

View File

@ -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