Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26a049e

Browse files
committed
Add command to delete an item from a cache pool
1 parent 3aa59b6 commit 26a049e

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Command;
13+
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\Console\Style\SymfonyStyle;
19+
20+
/**
21+
* Delete an item from a cachhe pool.
22+
*
23+
* @author Pierre du Plessis <[email protected]>
24+
*/
25+
final class CachePoolDeleteCommand extends Command
26+
{
27+
protected static $defaultName = 'cache:pool:delete';
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function configure()
33+
{
34+
$this
35+
->setDefinition(array(
36+
new InputArgument('pool', InputArgument::REQUIRED, 'The cache pool to delete an item from'),
37+
new InputArgument('key', InputArgument::REQUIRED, 'The cache key to delete from the pool'),
38+
))
39+
->setDescription('Deletes an item from a cache pool')
40+
->setHelp(<<<'EOF'
41+
The <info>%command.name%</info> deletes an item from a given cache pool.
42+
43+
%command.full_name% <pool> <key>
44+
EOF
45+
)
46+
;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
protected function execute(InputInterface $input, OutputInterface $output)
53+
{
54+
$io = new SymfonyStyle($input, $output);
55+
$pool = $input->getArgument('pool');
56+
$key = $input->getArgument('key');
57+
$cachePool = $this->getApplication()->getKernel()->getContainer()->get($pool);
58+
59+
if (!$cachePool->hasItem($key)) {
60+
return $io->warning(sprintf('Cache item "%s" does not exist in cache pool "%s"', $key, $pool));
61+
}
62+
63+
if (!$cachePool->deleteItem($key)) {
64+
return $io->error(sprintf('Cache item "%s" could not be deleted', $key));
65+
}
66+
67+
$io->success(sprintf('Cache item "%s" was successfully deleted.', $key));
68+
}
69+
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
<tag name="console.command" command="cache:pool:prune" />
3939
</service>
4040

41+
<service id="console.command.cache_pool_delete" class="Symfony\Bundle\FrameworkBundle\Command\CachePoolDeleteCommand">
42+
<tag name="console.command" command="cache:pool:delete" />
43+
</service>
44+
4145
<service id="console.command.cache_warmup" class="Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand">
4246
<argument type="service" id="cache_warmer" />
4347
<tag name="console.command" command="cache:warmup" />
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
13+
14+
use Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Bundle\FrameworkBundle\Command\CachePoolDeleteCommand;
16+
use Symfony\Bundle\FrameworkBundle\Console\Application;
17+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
18+
use Symfony\Component\Console\Tester\CommandTester;
19+
use Symfony\Component\HttpKernel\KernelInterface;
20+
21+
class CachePoolDeleteCommandTest extends TestCase
22+
{
23+
private $cachePool;
24+
25+
protected function setUp()
26+
{
27+
$this->cachePool = $this->getMockBuilder(CacheItemPoolInterface::class)
28+
->getMock();
29+
}
30+
31+
public function testCommandWithValidKey()
32+
{
33+
$this->cachePool->expects($this->once())
34+
->method('hasItem')
35+
->with('bar')
36+
->willReturn(true);
37+
38+
$this->cachePool->expects($this->once())
39+
->method('deleteItem')
40+
->with('bar')
41+
->willReturn(true);
42+
43+
$tester = $this->getCommandTester($this->getKernel());
44+
$tester->execute(array('pool' => 'foo', 'key' => 'bar'));
45+
46+
$this->assertContains('[OK] Cache item "bar" was successfully deleted.', $tester->getDisplay());
47+
}
48+
49+
public function testCommandWithInValidKey()
50+
{
51+
$this->cachePool->expects($this->once())
52+
->method('hasItem')
53+
->with('bar')
54+
->willReturn(false);
55+
56+
$this->cachePool->expects($this->never())
57+
->method('deleteItem')
58+
->with('bar');
59+
60+
$tester = $this->getCommandTester($this->getKernel());
61+
$tester->execute(array('pool' => 'foo', 'key' => 'bar'));
62+
63+
$this->assertContains('[WARNING] Cache item "bar" does not exist in cache pool "foo"', $tester->getDisplay());
64+
}
65+
66+
public function testCommandDeleteFailed()
67+
{
68+
$this->cachePool->expects($this->once())
69+
->method('hasItem')
70+
->with('bar')
71+
->willReturn(true);
72+
73+
$this->cachePool->expects($this->once())
74+
->method('deleteItem')
75+
->with('bar')
76+
->willReturn(false);
77+
78+
$tester = $this->getCommandTester($this->getKernel());
79+
$tester->execute(array('pool' => 'foo', 'key' => 'bar'));
80+
81+
$this->assertContains('[ERROR] Cache item "bar" could not be deleted', $tester->getDisplay());
82+
}
83+
84+
/**
85+
* @return \PHPUnit_Framework_MockObject_MockObject|KernelInterface
86+
*/
87+
private function getKernel()
88+
{
89+
$container = $this
90+
->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')
91+
->getMock();
92+
93+
$kernel = $this
94+
->getMockBuilder(KernelInterface::class)
95+
->getMock();
96+
97+
$kernel
98+
->expects($this->any())
99+
->method('getContainer')
100+
->willReturn($container);
101+
102+
$kernel
103+
->expects($this->once())
104+
->method('getBundles')
105+
->willReturn(array());
106+
107+
$container->expects($this->once())
108+
->method('get')
109+
->with('foo')
110+
->willReturn($this->cachePool);
111+
112+
return $kernel;
113+
}
114+
115+
private function getCommandTester(KernelInterface $kernel): CommandTester
116+
{
117+
$application = new Application($kernel);
118+
$application->add(new CachePoolDeleteCommand());
119+
120+
return new CommandTester($application->find('cache:pool:delete'));
121+
}
122+
}

0 commit comments

Comments
 (0)