diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php index 4181ccd8fd30f..5affec0ad8159 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php @@ -79,8 +79,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io = new SymfonyStyle($input, $output); $kernel = $this->getApplication()->getKernel(); - $realBuildDir = $kernel->getContainer()->getParameter('kernel.build_dir'); $realCacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir'); + $realBuildDir = $kernel->getContainer()->hasParameter('kernel.build_dir') ? $kernel->getContainer()->getParameter('kernel.build_dir') : $realCacheDir; // the old cache dir name must not be longer than the real one to avoid exceeding // the maximum length of a directory or file path within it (esp. Windows MAX_PATH) $oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php index 4b557011a9d26..48dcb3626c40e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php @@ -20,6 +20,7 @@ use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; +use Symfony\Component\HttpKernel\KernelInterface; class CacheClearCommandTest extends TestCase { @@ -40,17 +41,18 @@ protected function tearDown(): void $this->fs->remove($this->kernel->getProjectDir()); } - public function testCacheIsFreshAfterCacheClearedWithWarmup() + /** @dataProvider getKernel */ + public function testCacheIsFreshAfterCacheClearedWithWarmup(KernelInterface $kernel) { $input = new ArrayInput(['cache:clear']); - $application = new Application($this->kernel); + $application = new Application($kernel); $application->setCatchExceptions(false); $application->doRun($input, new NullOutput()); // Ensure that all *.meta files are fresh $finder = new Finder(); - $metaFiles = $finder->files()->in($this->kernel->getCacheDir())->name('*.php.meta'); + $metaFiles = $finder->files()->in($kernel->getCacheDir())->name('*.php.meta'); // check that cache is warmed up $this->assertNotEmpty($metaFiles); $configCacheFactory = new ConfigCacheFactory(true); @@ -62,11 +64,11 @@ public function testCacheIsFreshAfterCacheClearedWithWarmup() } // check that app kernel file present in meta file of container's cache - $containerClass = $this->kernel->getContainer()->getParameter('kernel.container_class'); + $containerClass = $kernel->getContainer()->getParameter('kernel.container_class'); $containerRef = new \ReflectionClass($containerClass); $containerFile = \dirname($containerRef->getFileName(), 2).'/'.$containerClass.'.php'; $containerMetaFile = $containerFile.'.meta'; - $kernelRef = new \ReflectionObject($this->kernel); + $kernelRef = new \ReflectionObject($kernel); $kernelFile = $kernelRef->getFileName(); /** @var ResourceInterface[] $meta */ $meta = unserialize(file_get_contents($containerMetaFile)); @@ -83,4 +85,21 @@ public function testCacheIsFreshAfterCacheClearedWithWarmup() $containerFile = str_replace('tes_'.\DIRECTORY_SEPARATOR, 'test'.\DIRECTORY_SEPARATOR, $containerRef->getFileName()); $this->assertMatchesRegularExpression(sprintf('/\'kernel.container_class\'\s*=>\s*\'%s\'/', $containerClass), file_get_contents($containerFile), 'kernel.container_class is properly set on the dumped container'); } + + public function getKernel() + { + yield [new TestAppKernel('test', true)]; + yield [new NoBuildDirKernel('test', true)]; + } +} + +class NoBuildDirKernel extends TestAppKernel +{ + protected function getKernelParameters() + { + $parameters = parent::getKernelParameters(); + unset($parameters['kernel.build_dir']); + + return $parameters; + } }