Skip to content

Commit 17b31e8

Browse files
committed
Throw exception when clear failed
1 parent cff1ccb commit 17b31e8

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Command/CachePoolClearCommand.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8989
$clearer->clear($kernel->getContainer()->getParameter('kernel.cache_dir'));
9090
}
9191

92+
$failure = false;
9293
foreach ($pools as $id => $pool) {
9394
$io->comment(sprintf('Clearing cache pool: <info>%s</info>', $id));
9495

9596
if ($pool instanceof CacheItemPoolInterface) {
96-
$pool->clear();
97+
if (!$pool->clear()) {
98+
$io->warning(sprintf('Cache pool "%s" could not be cleared.', $pool));
99+
$failure = true;
100+
}
97101
} else {
98-
$this->poolClearer->clearPool($id);
102+
if (false === $this->poolClearer->clearPool($id)) {
103+
$io->warning(sprintf('Cache pool "%s" could not be cleared.', $pool));
104+
$failure = true;
105+
}
99106
}
100107
}
101108

109+
if ($failure) {
110+
return 1;
111+
}
112+
102113
$io->success('Cache was successfully cleared.');
103114

104115
return 0;

Tests/Functional/CachePoolClearCommandTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Command\CachePoolClearCommand;
1515
use Symfony\Bundle\FrameworkBundle\Console\Application;
16+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
1617
use Symfony\Component\Console\Tester\CommandTester;
18+
use Symfony\Component\Finder\SplFileInfo;
1719

1820
/**
1921
* @group functional
@@ -73,6 +75,35 @@ public function testClearUnexistingPool()
7375
->execute(['pools' => ['unknown_pool']], ['decorated' => false]);
7476
}
7577

78+
public function testClearFailed()
79+
{
80+
$tester = $this->createCommandTester();
81+
/** @var FilesystemAdapter $pool */
82+
$pool = static::$container->get('cache.public_pool');
83+
$item = $pool->getItem('foo');
84+
$item->set('baz');
85+
$pool->save($item);
86+
$r = new \ReflectionObject($pool);
87+
$p = $r->getProperty('directory');
88+
$p->setAccessible(true);
89+
$poolDir = $p->getValue($pool);
90+
91+
/** @var SplFileInfo $entry */
92+
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($poolDir)) as $entry) {
93+
// converts files into dir to make adapter fail
94+
if ($entry->isFile()) {
95+
unlink($entry->getPathname());
96+
mkdir($entry->getPathname());
97+
}
98+
}
99+
100+
$tester->execute(['pools' => ['cache.public_pool']]);
101+
102+
$this->assertSame(1, $tester->getStatusCode(), 'cache:pool:clear exits with 1 in case of error');
103+
$this->assertStringNotContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
104+
$this->assertStringContainsString('[WARNING] Cache pool "cache.public_pool" could not be cleared.', $tester->getDisplay());
105+
}
106+
76107
private function createCommandTester()
77108
{
78109
$application = new Application(static::$kernel);

0 commit comments

Comments
 (0)