|
| 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