8000 bug #58240 [FrameworkBundle] Fix service reset between tests (HypeMC) · symfony/symfony@b2201c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b2201c3

Browse files
bug #58240 [FrameworkBundle] Fix service reset between tests (HypeMC)
This PR was merged into the 5.4 branch. Discussion ---------- [FrameworkBundle] Fix service reset between tests | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT Currently, not all services are reset between tests. If a service uses the kernel.reset tag but does not implement the ResetInterface, it never gets reset. Commits ------- fb1ae1a [FrameworkBundle] Fix service reset between tests
2 parents e104cd2 + fb1ae1a commit b2201c3

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ protected static function ensureKernelShutdown()
160160
static::$kernel->shutdown();
161161
static::$booted = false;
162162

163+
if ($container->has('services_resetter')) {
164+
// Instantiate the service because Container::reset() only resets services that have been used
165+
$container->get('services_resetter');
166+
}
167+
163168
if ($container instanceof ResetInterface) {
164169
$container->reset();
165170
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer;
13+
14+
class ResettableService
15+
{
16+
private $count = 0;
17+
18+
public function myCustomName(): void
19+
{
20+
++$this->count;
21+
}
22+
23+
public function getCount(): int
24+
{
25+
return $this->count;
26+
}
27+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/KernelTestCaseTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService;
1616
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService;
1717
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PublicService;
18+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\ResettableService;
1819
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\UnusedPrivateService;
1920
use Symfony\Component\DependencyInjection\ContainerInterface;
2021

@@ -41,4 +42,14 @@ public function testThatPrivateServicesAreAvailableIfTestConfigIsEnabled()
4142
$this->assertTrue($container->has('private_service'));
4243
$this->assertFalse($container->has(UnusedPrivateService::class));
4344
}
45+
46+
public function testServicesAreResetOnEnsureKernelShutdown()
47+
{
48+
static::bootKernel(['test_case' => 'TestServiceContainer']);
49+
50+
$resettableService = static::getContainer()->get(ResettableService::class);
51+
52+
self::ensureKernelShutdown();
53+
self::assertSame(1, $resettableService->getCount());
54+
}
4455
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ services:
1313
arguments:
1414
- '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService'
1515
- '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService'
16+
17+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\ResettableService:
18+
public: true
19+
tags:
20+
- kernel.reset: { method: 'myCustomName' }

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"ext-xml": "*",
2121
"symfony/cache": "^5.2|^6.0",
2222
"symfony/config": "^5.3|^6.0",
23-
"symfony/dependency-injection": "^5.4.5|^6.0.5",
23+
"symfony/dependency-injection": "^5.4.44|^6.0.5",
2424
"symfony/deprecation-contracts": "^2.1|^3",
2525
"symfony/event-dispatcher": "^5.1|^6.0",
2626
"symfony/error-handler": "^4.4.1|^5.0.1|^6.0",

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
299299
public function reset()
300300
{
301301
$services = $this->services + $this->privates;
302-
$this->services = $this->factories = $this->privates = [];
303302

304303
foreach ($services as $service) {
305304
try {
@@ -310,6 +309,8 @@ public function reset()
310309
continue;
311310
}
312311
}
312+
313+
$this->services = $this->factories = $this->privates = [];
313314
}
314315

315316
/**

0 commit comments

Comments
 (0)
0