[FrameworkBundle] WebTestCase KernelBrowser::getContainer null return type

This commit is contained in:
Amrouche Hamza 2019-04-23 08:04:30 +02:00 committed by Nicolas Grekas
parent b8294398cc
commit e169e1a4d5
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,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;
}
}

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