Skip to content

Commit 13c969f

Browse files
committed
Throw exception when clear failed
1 parent c5140c2 commit 13c969f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/CachePoolClearCommand.php

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

92+
$failures = [];
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+
$failures[] = $id;
99+
}
97100
} else {
98-
$this->poolClearer->clearPool($id);
101+
if (false === $this->poolClearer->clearPool($id)) {
102+
$failures[] = $id;
103+
}
99104
}
100105
}
101106

107+
if ($failures) {
108+
throw new \Exception(sprintf('Cache pool "%s" could not be cleared.', implode('", "', $failures)));
109+
}
110+
102111
$io->success('Cache was successfully cleared.');
103112

104113
return 0;

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolClearCommandTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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;
1718

1819
/**
@@ -73,6 +74,32 @@ public function testClearUnexistingPool()
7374
->execute(['pools' => ['unknown_pool']], ['decorated' => false]);
7475
}
7576

77+
public function testClearFailed()
78+
{
79+
$tester = $this->createCommandTester();
80+
/** @var FilesystemAdapter $pool */
81+
$pool = static::$container->get('cache.public_pool');
82+
$item = $pool->getItem('foo');
83+
$item->set('baz');
84+
$pool->save($item);
85+
$r = new \ReflectionObject($pool);
86+
$p = $r->getProperty('directory');
87+
$p->setAccessible(true);
88+
$poolDir = $p->getValue($pool);
89+
90+
foreach (glob($poolDir.'*/*') as $dir) {
91+
chmod($dir, 0555);
92+
}
93+
try {
94+
$this->expectExceptionMessage('Cache pool "cache.public_pool" could not be cleared.');
95+
$tester->execute(['pools' => ['cache.public_pool']]);
96+
} finally {
97+
foreach (glob($poolDir.'*/*') as $dir) {
98+
chmod($dir, 0777);
99+
}
100+
}
101+
}
102+
76103
private function createCommandTester()
77104
{
78105
$application = new Application(static::$kernel);

0 commit comments

Comments
 (0)