Skip to content

Commit

Permalink
bug #32056 [DI] deprecate booting the kernel twices (Simperfit)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[DI] deprecate booting the kernel twices

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #31233   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | ? <!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained 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 branch 4.4.
 - Legacy code removals go to the master branch.
-->

This adds a check to see if the kernel has been booted twices in a single test, and throw a deprecation

Commits
-------

905bec4 [DI] throw an exception when the kernel has been booted twices
  • Loading branch information
fabpot committed Jul 8, 2019
2 parents ea0da05 + 905bec4 commit 2d04e20
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Deprecated the `controller_name_converter` and `resolve_controller_name_subscriber` services
* The `ControllerResolver` and `DelegatingLoader` classes have been marked as `final`
* Added support for configuring chained cache pools
* Deprecated booting the kernel before running `WebTestCase::createClient()`

4.3.0
-----
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ abstract class KernelTestCase extends TestCase
*/
protected static $container;

protected static $booted;

protected function doTearDown(): void
{
static::ensureKernelShutdown();
Expand Down Expand Up @@ -72,6 +74,7 @@ protected static function bootKernel(array $options = [])

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;
Expand Down Expand Up @@ -126,6 +129,7 @@ protected static function ensureKernelShutdown()
if (null !== static::$kernel) {
$container = static::$kernel->getContainer();
static::$kernel->shutdown();
static::$booted = false;
if ($container instanceof ResetInterface) {
$container->reset();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ protected function doTearDown(): void
*/
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function testTwoClients($config, $insulate)
$client1->insulate();
}

$this->ensureKernelShutdown();

// start second client
$client2 = $this->createClient(['test_case' => 'Session', 'root_config' => $config]);
if ($insulate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWith
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');
Expand All @@ -70,8 +73,17 @@ public function testSecurityConfigurationForSingleIPAddress($config)
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');
Expand All @@ -91,9 +103,11 @@ public function testSecurityConfigurationForExpression($config)
{
$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], []);

Expand Down

0 comments on commit 2d04e20

Please sign in to comment.