[DI] throw an exception when the kernel has been booted twices

This commit is contained in:
Amrouche Hamza 2019-06-15 06:44:27 +02:00
parent 9f6cbaa6b9
commit 905bec4577
No known key found for this signature in database
GPG Key ID: E45A3DA456145BC1
5 changed files with 25 additions and 0 deletions

View File

@ -8,6 +8,7 @@ CHANGELOG
* Deprecated the `$parser` argument of `ControllerResolver::__construct()` and `DelegatingLoader::__construct()`
* Deprecated the `controller_name_converter` and `resolve_controller_name_subscriber` services
* The `ControllerResolver` and `DelegatingLoader` classes have been marked as `final`
* Deprecated booting the kernel before running `WebTestCase::createClient()`
4.3.0
-----

View File

@ -37,6 +37,8 @@ abstract class KernelTestCase extends TestCase
*/
protected static $container;
protected static $booted;
protected function doTearDown(): void
{
static::ensureKernelShutdown();
@ -72,6 +74,7 @@ abstract class KernelTestCase extends TestCase
static::$kernel = static::createKernel($options);
static::$kernel->boot();
static::$booted = true;
$container = static::$kernel->getContainer();
static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
@ -126,6 +129,7 @@ abstract class KernelTestCase extends TestCase
if (null !== static::$kernel) {
$container = static::$kernel->getContainer();
static::$kernel->shutdown();
static::$booted = false;
if ($container instanceof ResetInterface) {
$container->reset();
}

View File

@ -39,6 +39,10 @@ abstract class WebTestCase extends KernelTestCase
*/
protected static function createClient(array $options = [], array $server = [])
{
if (true === static::$booted) {
@trigger_error(sprintf('Booting the kernel before calling %s::%s is deprecated and will throw in Symfony 5.0, the kernel should only be booted once.', __CLASS__, __METHOD__), E_USER_DEPRECATED);
}
$kernel = static::bootKernel($options);
try {

View File

@ -83,6 +83,8 @@ class SessionTest extends WebTestCase
$client1->insulate();
}
$this->ensureKernelShutdown();
// start second client
$client2 = $this->createClient(['test_case' => 'Session', 'root_config' => $config]);
if ($insulate) {

View File

@ -58,6 +58,9 @@ class SecurityRoutingIntegrationTest extends WebTestCase
public function testSecurityConfigurationForSingleIPAddress($config)
{
$allowedClient = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => $config], ['REMOTE_ADDR' => '10.10.10.10']);
$this->ensureKernelShutdown();
$barredClient = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => $config], ['REMOTE_ADDR' => '10.10.20.10']);
$this->assertAllowed($allowedClient, '/secured-by-one-ip');
@ -70,8 +73,17 @@ class SecurityRoutingIntegrationTest extends WebTestCase
public function testSecurityConfigurationForMultipleIPAddresses($config)
{
$allowedClientA = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => $config], ['REMOTE_ADDR' => '1.1.1.1']);
$this->ensureKernelShutdown();
$allowedClientB = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => $config], ['REMOTE_ADDR' => '2.2.2.2']);
$this->ensureKernelShutdown();
$allowedClientC = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => $config], ['REMOTE_ADDR' => '203.0.113.0']);
$this->ensureKernelShutdown();
$barredClient = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => $config], ['REMOTE_ADDR' => '192.168.1.1']);
$this->assertAllowed($allowedClientA, '/secured-by-two-ips');
@ -91,9 +103,11 @@ class SecurityRoutingIntegrationTest extends WebTestCase
{
$allowedClient = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => $config], ['HTTP_USER_AGENT' => 'Firefox 1.0']);
$this->assertAllowed($allowedClient, '/protected-via-expression');
$this->ensureKernelShutdown();
$barredClient = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => $config], []);
$this->assertRestricted($barredClient, '/protected-via-expression');
$this->ensureKernelShutdown();
$allowedClient = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => $config], []);