Skip to content

Commit 0d4336b

Browse files
committed
feature #43621 Add completion for cache:pool:clear and cache:pool:delete commands (andyexeter)
This PR was merged into the 5.4 branch. Discussion ---------- Add completion for cache:pool:clear and cache:pool:delete commands | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | #43594 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | No <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry --> This PR adds completion to `cache:pool:clear` and `cache:pool:delete` commands. Commits ------- fd399aaa16 Add completion for cache:pool:clear and cache:pool:delete commands
2 parents fbe5b7e + bdf28dd commit 0d4336b

File tree

5 files changed

+124
-2
lines changed

5 files changed

+124
-2
lines changed

Command/CachePoolClearCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Psr\Cache\CacheItemPoolInterface;
1515
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Completion\CompletionInput;
17+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1618
use Symfony\Component\Console\Exception\InvalidArgumentException;
1719
use Symfony\Component\Console\Input\InputArgument;
1820
use Symfony\Component\Console\Input\InputInterface;
@@ -31,12 +33,14 @@ final class CachePoolClearCommand extends Command
3133
protected static $defaultDescription = 'Clear cache pools';
3234

3335
private $poolClearer;
36+
private $poolNames;
3437

35-
public function __construct(Psr6CacheClearer $poolClearer)
38+
public function __construct(Psr6CacheClearer $poolClearer, array $poolNames = null)
3639
{
3740
parent::__construct();
3841

3942
$this->poolClearer = $poolClearer;
43+
$this->poolNames = $poolNames;
4044
}
4145

4246
/**
@@ -114,4 +118,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
114118

115119
return 0;
116120
}
121+
122+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
123+
{
124+
if (\is_array($this->poolNames) && $input->mustSuggestArgumentValuesFor('pools')) {
125+
$suggestions->suggestValues($this->poolNames);
126+
}
127+
}
117128
}

Command/CachePoolDeleteCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Input\InputArgument;
1618
use Symfony\Component\Console\Input\InputInterface;
1719
use Symfony\Component\Console\Output\OutputInterface;
@@ -29,12 +31,14 @@ final class CachePoolDeleteCommand extends Command
2931
protected static $defaultDescription = 'Delete an item from a cache pool';
3032

3133
private $poolClearer;
34+
private $poolNames;
3235

33-
public function __construct(Psr6CacheClearer $poolClearer)
36+
public function __construct(Psr6CacheClearer $poolClearer, array $poolNames = null)
3437
{
3538
parent::__construct();
3639

3740
$this->poolClearer = $poolClearer;
41+
$this->poolNames = $poolNames;
3842
}
3943

4044
/**
@@ -81,4 +85,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8185

8286
return 0;
8387
}
88+
89+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
90+
{
91+
if (\is_array($this->poolNames) && $input->mustSuggestArgumentValuesFor('pool')) {
92+
$suggestions->suggestValues($this->poolNames);
93+
}
94+
}
8495
}

Resources/config/console.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
->set('console.command.cache_pool_clear', CachePoolClearCommand::class)
8585
->args([
8686
service('cache.global_clearer'),
87+
null,
8788
])
8889
->tag('console.command')
8990

@@ -96,6 +97,7 @@
9697
->set('console.command.cache_pool_delete', CachePoolDeleteCommand::class)
9798
->args([
9899
service('cache.global_clearer'),
100+
null,
99101
])
100102
->tag('console.command')
101103

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 PHPUnit\Framework\MockObject\MockObject;
15+
use Psr\Cache\CacheItemPoolInterface;
16+
use Symfony\Bundle\FrameworkBundle\Command\CachePoolClearCommand;
17+
use Symfony\Bundle\FrameworkBundle\Console\Application;
18+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
19+
use Symfony\Component\Console\Tester\CommandCompletionTester;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
21+
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
22+
use Symfony\Component\HttpKernel\KernelInterface;
23+
24+
class CachePoolClearCommandTest extends TestCase
25+
{
26+
private $cachePool;
27+
28+
protected function setUp(): void
29+
{
30+
$this->cachePool = $this->createMock(CacheItemPoolInterface::class);
31+
}
32+
33+
/**
34+
* @dataProvider provideCompletionSuggestions
35+
*/
36+
public function testComplete(array $input, array $expectedSuggestions)
37+
{
38+
$application = new Application($this->getKernel());
39+
$application->add(new CachePoolClearCommand(new Psr6CacheClearer(['foo' => $this->cachePool]), ['foo']));
40+
$tester = new CommandCompletionTester($application->get('cache:pool:clear'));
41+
42+
$suggestions = $tester->complete($input);
43+
44+
$this->assertSame($expectedSuggestions, $suggestions);
45+
}
46+
47+
public function provideCompletionSuggestions()
48+
{
49+
yield 'pool_name' => [
50+
['f'],
51+
['foo'],
52+
];
53+
}
54+
55+
/**
56+
* @return MockObject&KernelInterface
57+
*/
58+
private function getKernel(): KernelInterface
59+
{
60+
$container = $this->createMock(ContainerInterface::class);
61+
62+
$kernel = $this->createMock(KernelInterface::class);
63+
$kernel
64+
->expects($this->any())
65+
->method('getContainer')
66+
->willReturn($container);
67+
68+
$kernel
69+
->expects($this->once())
70+
->method('getBundles')
71+
->willReturn([]);
72+
73+
return $kernel;
74+
}
75+
}

Tests/Command/CachePoolDeleteCommandTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bundle\FrameworkBundle\Command\CachePoolDeleteCommand;
1717
use Symfony\Bundle\FrameworkBundle\Console\Application;
1818
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
19+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1920
use Symfony\Component\Console\Tester\CommandTester;
2021
use Symfony\Component\DependencyInjection\ContainerInterface;
2122
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
@@ -83,6 +84,28 @@ public function testCommandDeleteFailed()
8384
$tester->execute(['pool' => 'foo', 'key' => 'bar']);
8485
}
8586

87+
/**
88+
* @dataProvider provideCompletionSuggestions
89+
*/
90+
public function testComplete(array $input, array $expectedSuggestions)
91+
{
92+
$application = new Application($this->getKernel());
93+
$application->add(new CachePoolDeleteCommand(new Psr6CacheClearer(['foo' => $this->cachePool]), ['foo']));
94+
$tester = new CommandCompletionTester($application->get('cache:pool:delete'));
95+
96+
$suggestions = $tester->complete($input);
97+
98+
$this->assertSame($expectedSuggestions, $suggestions);
99+
}
100+
101+
public function provideCompletionSuggestions()
102+
{
103+
yield 'pool_name' => [
104+
['f'],
105+
['foo'],
106+
];
107+
}
108+
86109
/**
87110
* @return MockObject&KernelInterface
88111
*/

0 commit comments

Comments
 (0)