Skip to content

Commit c158532

Browse files
committed
Fix missing kernel.build_dir on cache clear
1 parent fad9e25 commit c158532

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

Command/CacheClearCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7979
$io = new SymfonyStyle($input, $output);
8080

8181
$kernel = $this->getApplication()->getKernel();
82-
$realBuildDir = $kernel->getContainer()->getParameter('kernel.build_dir');
8382
$realCacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir');
83+
$realBuildDir = $kernel->getContainer()->hasParameter('kernel.build_dir') ? $kernel->getContainer()->getParameter('kernel.build_dir') : $realCacheDir;
8484
// the old cache dir name must not be longer than the real one to avoid exceeding
8585
// the maximum length of a directory or file path within it (esp. Windows MAX_PATH)
8686
$oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~');

Tests/Command/CacheClearCommand/CacheClearCommandTest.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Output\NullOutput;
2121
use Symfony\Component\Filesystem\Filesystem;
2222
use Symfony\Component\Finder\Finder;
23+
use Symfony\Component\HttpKernel\KernelInterface;
2324

2425
class CacheClearCommandTest extends TestCase
2526
{
@@ -40,17 +41,18 @@ protected function tearDown(): void
4041
$this->fs->remove($this->kernel->getProjectDir());
4142
}
4243

43-
public function testCacheIsFreshAfterCacheClearedWithWarmup()
44+
/** @dataProvider getKernel */
45+
public function testCacheIsFreshAfterCacheClearedWithWarmup(KernelInterface $kernel)
4446
{
4547
$input = new ArrayInput(['cache:clear']);
46-
$application = new Application($this->kernel);
48+
$application = new Application($kernel);
4749
$application->setCatchExceptions(false);
4850

4951
$application->doRun($input, new NullOutput());
5052

5153
// Ensure that all *.meta files are fresh
5254
$finder = new Finder();
53-
$metaFiles = $finder->files()->in($this->kernel->getCacheDir())->name('*.php.meta');
55+
$metaFiles = $finder->files()->in($kernel->getCacheDir())->name('*.php.meta');
5456
// check that cache is warmed up
5557
$this->assertNotEmpty($metaFiles);
5658
$configCacheFactory = new ConfigCacheFactory(true);
@@ -62,11 +64,11 @@ public function testCacheIsFreshAfterCacheClearedWithWarmup()
6264
}
6365

6466
// check that app kernel file present in meta file of container's cache
65-
$containerClass = $this->kernel->getContainer()->getParameter('kernel.container_class');
67+
$containerClass = $kernel->getContainer()->getParameter('kernel.container_class');
6668
$containerRef = new \ReflectionClass($containerClass);
6769
$containerFile = \dirname($containerRef->getFileName(), 2).'/'.$containerClass.'.php';
6870
$containerMetaFile = $containerFile.'.meta';
69-
$kernelRef = new \ReflectionObject($this->kernel);
71+
$kernelRef = new \ReflectionObject($kernel);
7072
$kernelFile = $kernelRef->getFileName();
7173
/** @var ResourceInterface[] $meta */
7274
$meta = unserialize(file_get_contents($containerMetaFile));
@@ -83,4 +85,21 @@ public function testCacheIsFreshAfterCacheClearedWithWarmup()
8385
$containerFile = str_replace('tes_'.\DIRECTORY_SEPARATOR, 'test'.\DIRECTORY_SEPARATOR, $containerRef->getFileName());
8486
$this->assertMatchesRegularExpression(sprintf('/\'kernel.container_class\'\s*=>\s*\'%s\'/', $containerClass), file_get_contents($containerFile), 'kernel.container_class is properly set on the dumped container');
8587
}
88+
89+
public function getKernel()
90+
{
91+
yield [new TestAppKernel('test', true)];
92+
yield [new NoBuildDirKernel('test', true)];
93+
}
94+
}
95+
96+
class NoBuildDirKernel extends TestAppKernel
97+
{
98+
protected function getKernelParameters()
99+
{
100+
$parameters = parent::getKernelParameters();
101+
unset($parameters['kernel.build_dir']);
102+
103+
return $parameters;
104+
}
86105
}

0 commit comments

Comments
 (0)