-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Add command to delete an item from a cache pool #26223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer; | ||
|
||
/** | ||
* Delete an item from a cachhe pool. | ||
* | ||
* @author Pierre du Plessis <pdples@gmail.com> | ||
*/ | ||
final class CachePoolDeleteCommand extends Command | ||
{ | ||
protected static $defaultName = 'cache:pool:delete'; | ||
|
||
private $poolClearer; | ||
|
||
public function __construct(Psr6CacheClearer $poolClearer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really want to inject the cache clearer to then use it to fetch a pool? Shouldn't we rather inject the pools directly? |
||
{ | ||
parent::__construct(); | ||
|
||
$this->poolClearer = $poolClearer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setDefinition(array( | ||
new InputArgument('pool', InputArgument::REQUIRED, 'The cache pool to delete an item from'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
new InputArgument('key', InputArgument::REQUIRED, 'The cache key to delete from the pool'), | ||
)) | ||
->setDescription('Deletes an item from a cache pool') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> deletes an item from a given cache pool. | ||
|
||
%command.full_name% <pool> <key> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$pool = $input->getArgument('pool'); | ||
$key = $input->getArgument('key'); | ||
$cachePool = $this->poolClearer->getPool($pool); | ||
|
||
if (!$cachePool->hasItem($key)) { | ||
$io->warning(sprintf('Cache item "%s" does not exist in cache pool "%s".', $key, $pool)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be a notice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, forgot about this. Thanks |
||
|
||
return; | ||
} | ||
|
||
if (!$cachePool->deleteItem($key)) { | ||
throw new \Exception(sprintf('Cache item "%s" could not be deleted.', $key)); | ||
} | ||
|
||
$io->success(sprintf('Cache item "%s" was successfully deleted.', $key)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest to log only in verbose mode (same above) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hum, maybe not actually :) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Command; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Bundle\FrameworkBundle\Command\CachePoolDeleteCommand; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
class CachePoolDeleteCommandTest extends TestCase | ||
{ | ||
private $cachePool; | ||
|
||
protected function setUp() | ||
{ | ||
$this->cachePool = $this->getMockBuilder(CacheItemPoolInterface::class) | ||
->getMock(); | ||
} | ||
|
||
public function testCommandWithValidKey() | ||
{ | ||
$this->cachePool->expects($this->once()) | ||
->method('hasItem') | ||
->with('bar') | ||
->willReturn(true); | ||
|
||
$this->cachePool->expects($this->once()) | ||
->method('deleteItem') | ||
->with('bar') | ||
->willReturn(true); | ||
|
||
$tester = $this->getCommandTester($this->getKernel()); | ||
$tester->execute(array('pool' => 'foo', 'key' => 'bar')); | ||
|
||
$this->assertContains('[OK] Cache item "bar" was successfully deleted.', $tester->getDisplay()); | ||
} | ||
|
||
public function testCommandWithInValidKey() | ||
{ | ||
$this->cachePool->expects($this->once()) | ||
->method('hasItem') | ||
->with('bar') | ||
->willReturn(false); | ||
|
||
$this->cachePool->expects($this->never()) | ||
->method('deleteItem') | ||
->with('bar'); | ||
|
||
$tester = $this->getCommandTester($this->getKernel()); | ||
$tester->execute(array('pool' => 'foo', 'key' => 'bar')); | ||
|
||
$this->assertContains('[WARNING] Cache item "bar" does not exist in cache pool "foo".', $tester->getDisplay()); | ||
} | ||
|
||
public function testCommandDeleteFailed() | ||
{ | ||
$this->cachePool->expects($this->once()) | ||
->method('hasItem') | ||
->with('bar') | ||
->willReturn(true); | ||
|
||
$this->cachePool->expects($this->once()) | ||
->method('deleteItem') | ||
->with('bar') | ||
->willReturn(false); | ||
|
||
if (method_exists($this, 'expectExceptionMessage')) { | ||
$this->expectExceptionMessage('Cache item "bar" could not be deleted.'); | ||
} else { | ||
$this->setExpectedException('Exception', 'Cache item "bar" could not be deleted.'); | ||
} | ||
|
||
$tester = $this->getCommandTester($this->getKernel()); | ||
$tester->execute(array('pool' => 'foo', 'key' => 'bar')); | ||
} | ||
|
||
/** | ||
* @return \PHPUnit_Framework_MockObject_MockObject|KernelInterface | ||
*/ | ||
private function getKernel() | ||
{ | ||
$container = $this | ||
->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface') | ||
->getMock(); | ||
|
||
$kernel = $this | ||
->getMockBuilder(KernelInterface::class) | ||
->getMock(); | ||
|
||
$kernel | ||
->expects($this->any()) | ||
->method('getContainer') | ||
->willReturn($container); | ||
|
||
$kernel | ||
->expects($this->once()) | ||
->method('getBundles') | ||
->willReturn(array()); | ||
|
||
return $kernel; | ||
} | ||
|
||
private function getCommandTester(KernelInterface $kernel): CommandTester | ||
{ | ||
$application = new Application($kernel); | ||
$application->add(new CachePoolDeleteCommand(new Psr6CacheClearer(array('foo' => $this->cachePool)))); | ||
|
||
return new CommandTester($application->find('cache:pool:delete')); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this can only delete an item as specified in descriptions (
key
is required), wouldn'tCachePooltemDeleteCommand
be more accurate? Also, there's a tyop in the comment above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This matches the name of the command, and is good enough IMHO as nothing else than an item can be deleted.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about renaming both.
Not everyone may know that though and can have different expectations. But as long as it's documented well, it's ok, I guess.