Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c94226e

Browse files
[FrameworkBundle] Fix stale container after reboot in KernelTestCase
1 parent 5b5d194 commit c94226e

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

Test/KernelTestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Test;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
1516
use Symfony\Component\DependencyInjection\Container;
1617
use Symfony\Component\DependencyInjection\ContainerInterface;
1718
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1819
use Symfony\Component\Filesystem\Filesystem;
20+
use Symfony\Component\HttpKernel\Kernel;
1921
use Symfony\Component\HttpKernel\KernelInterface;
2022
use Symfony\Contracts\Service\ResetInterface;
2123

@@ -38,6 +40,8 @@ abstract class KernelTestCase extends TestCase
3840

3941
protected static $booted = false;
4042

43+
private static bool $kernelHasBeenRebooted = false;
44+
4145
protected function tearDown(): void
4246
{
4347
static::ensureKernelShutdown();
@@ -88,6 +92,7 @@ protected static function bootKernel(array $options = []): KernelInterface
8892
// reboot a fresh one.
8993
if ($kernel->getContainer()->initialized('cache_warmer')) {
9094
static::ensureKernelShutdown();
95+
self::$kernelHasBeenRebooted = true;
9196

9297
$kernel = static::createKernel($options);
9398
$kernel->boot();
@@ -161,6 +166,20 @@ protected static function ensureKernelShutdown()
161166
static::$kernel->shutdown();
162167
static::$booted = false;
163168

169+
if (self::$kernelHasBeenRebooted) {
170+
self::$kernelHasBeenRebooted = false;
171+
try {
172+
(new \ReflectionProperty(Kernel::class, 'freshCache'))->setValue(null, []);
173+
} catch (\ReflectionException) {
174+
// ignore if the property doesn't exist
175+
}
176+
try {
177+
(new \ReflectionProperty(SelfCheckingResourceChecker::class, 'cache'))->setValue(null, []);
178+
} catch (\ReflectionException) {
179+
// ignore if the property doesn't exist
180+
}
181+
}
182+
164183
if ($container instanceof ResetInterface) {
165184
$container->reset();
166185
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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;
13+
14+
use Symfony\Component\HttpKernel\KernelInterface;
15+
16+
class KernelTestCaseFreshCacheTest extends AbstractWebTestCase
17+
{
18+
private static string $trackedFile;
19+
20+
public static function setUpBeforeClass(): void
21+
{
22+
parent::setUpBeforeClass();
23+
24+
self::$trackedFile = sys_get_temp_dir().'/'.static::getVarDir().'/tracked_file.yaml';
25+
}
26+
27+
public static function tearDownAfterClass(): void
28+
{
29+
parent::tearDownAfterClass();
30+
31+
@unlink(self::$trackedFile);
32+
}
33+
34+
public function testContainerIsRebuiltWhenTrackedFileAppears()
35+
{
36+
@unlink(self::$trackedFile);
37+
38+
// First boot: container is built tracking the file as non-existing
39+
static::bootKernel(['test_case' => 'TestServiceContainer', 'debug' => true]);
40+
41+
$this->assertFalse(static::$kernel->getContainer()->getParameter('tracked_file_existed'));
42+
43+
static::ensureKernelShutdown();
44+
45+
// Create the tracked file between boots
46+
@mkdir(\dirname(self::$trackedFile), 0777, true);
47+
file_put_contents(self::$trackedFile, 'placeholder');
48+
49+
// Reboot: should detect the resource change and rebuild the container
50+
static::bootKernel(['test_case' => 'TestServiceContainer', 'debug' => true]);
51+
52+
$this->assertTrue(static::$kernel->getContainer()->getParameter('tracked_file_existed'));
53+
}
54+
55+
protected static function createKernel(array $options = []): KernelInterface
56+
{
57+
$kernel = parent::createKernel($options);
58+
59+
FileExistenceTrackingKernel::$trackedFile = self::$trackedFile;
60+
61+
return $kernel;
62+
}
63+
64+
protected static function getKernelClass(): string
65+
{
66+
require_once __DIR__.'/app/AppKernel.php';
67+
68+
return FileExistenceTrackingKernel::class;
69+
}
70+
}
71+
72+
class FileExistenceTrackingKernel extends app\AppKernel
73+
{
74+
public static string $trackedFile;
75+
76+
protected function build(\Symfony\Component\DependencyInjection\ContainerBuilder $container): void
77+
{
78+
parent::build($container);
79+
80+
$container->setParameter('tracked_file_existed', $container->fileExists(self::$trackedFile));
81+
}
82+
}

0 commit comments

Comments
 (0)