Skip to content

Commit 73f563a

Browse files
authored
v1.6.1: Implements cache:clear command (#2)
* v1.6.1: Implements cache:clear command
1 parent 83015fc commit 73f563a

3 files changed

Lines changed: 104 additions & 2 deletions

File tree

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
"micro/plugin-console": "^1.6",
1515
"psr/cache": "^3.0",
1616
"psr/simple-cache": "^3.0",
17-
"symfony/cache": "^6",
18-
"symfony/var-dumper": "^6.2"
17+
"symfony/cache": "^6"
1918
},
2019
"require-dev": {
2120
"ergebnis/composer-normalize": "^2.29",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Cache\Communication\Cli;
15+
16+
use Micro\Plugin\Cache\Facade\CacheFacadeInterface;
17+
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Input\InputArgument;
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
use Symfony\Component\Console\Style\SymfonyStyle;
22+
23+
class ClearCacheCommand extends Command
24+
{
25+
public function __construct(
26+
private readonly CacheFacadeInterface $cacheFacade
27+
) {
28+
parent::__construct('cache:clear');
29+
}
30+
31+
public function configure(): void
32+
{
33+
$this
34+
->addArgument('pools', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'A list of cache pools or cache pool clearer\'s')
35+
->setHelp(
36+
<<<'EOF'
37+
The <info>%command.name%</info> command clears the given cache pools or cache pool clearer\'s.
38+
39+
%command.full_name% <cache pool or clearer 1> [...<cache pool or clearer N>]
40+
EOF
41+
)
42+
->setDescription('Clear cache pools');
43+
}
44+
45+
public function execute(InputInterface $input, OutputInterface $output)
46+
{
47+
$io = new SymfonyStyle($input, $output);
48+
49+
foreach ($input->getArgument('pools') as $poolName) {
50+
$io->comment(sprintf('Clearing cache pool: <info>%s</info>', $poolName));
51+
$pool = $this->cacheFacade->getCachePsr6($poolName);
52+
$pool->clear();
53+
}
54+
55+
$io->success('Cache was successfully cleared.');
56+
57+
return self::SUCCESS;
58+
}
59+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Cache\Test\Unit\Communication\Cli;
15+
16+
use Micro\Plugin\Cache\Communication\Cli\ClearCacheCommand;
17+
use Micro\Plugin\Cache\Facade\CacheFacadeInterface;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
22+
class ClearCacheCommandTest extends TestCase
23+
{
24+
public function testCommand(): void
25+
{
26+
$cacheFacade = $this->createMock(CacheFacadeInterface::class);
27+
$cmd = new ClearCacheCommand(
28+
$cacheFacade
29+
);
30+
31+
$this->assertIsString($cmd->getDescription());
32+
$this->assertIsString($cmd->getHelp());
33+
34+
$input = $this->createMock(InputInterface::class);
35+
$input
36+
->expects($this->once())
37+
->method('getArgument')
38+
->with('pools')
39+
->willReturn(['test'])
40+
;
41+
$output = $this->createMock(OutputInterface::class);
42+
$this->assertEquals(0, $cmd->run($input, $output));
43+
}
44+
}

0 commit comments

Comments
 (0)