Skip to content

Commit fbe5b7e

Browse files
committed
Add completion to commands secrets:set and fix secrets:remove
1 parent 2e6c7e1 commit fbe5b7e

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

Command/SecretsRemoveCommand.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
8989
return;
9090
}
9191

92-
$vault = $input->getOption('local') ? $this->localVault : $this->vault;
9392
$vaultKeys = array_keys($this->vault->list(false));
94-
$suggestions->suggestValues(array_intersect($vaultKeys, array_keys($vault->list(false))));
93+
if ($input->getOption('local')) {
94+
if (null === $this->localVault) {
95+
return;
96+
}
97+
$vaultKeys = array_intersect($vaultKeys, array_keys($this->localVault->list(false)));
98+
}
99+
100+
$suggestions->suggestValues($vaultKeys);
95101
}
96102
}

Command/SecretsSetCommand.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Secrets\AbstractVault;
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\Input\InputArgument;
1719
use Symfony\Component\Console\Input\InputInterface;
1820
use Symfony\Component\Console\Input\InputOption;
@@ -137,4 +139,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
137139

138140
return 0;
139141
}
142+
143+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
144+
{
145+
if ($input->mustSuggestArgumentValuesFor('name')) {
146+
$suggestions->suggestValues(array_keys($this->vault->list(false)));
147+
}
148+
}
140149
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Bundle\FrameworkBundle\Command\SecretsRemoveCommand;
7+
use Symfony\Bundle\FrameworkBundle\Secrets\AbstractVault;
8+
use Symfony\Component\Console\Tester\CommandCompletionTester;
9+
10+
class SecretsRemoveCommandTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider provideCompletionSuggestions
14+
*/
15+
public function testComplete(bool $withLocalVault, array $input, array $expectedSuggestions)
16+
{
17+
$vault = $this->createMock(AbstractVault::class);
18+
$vault->method('list')->willReturn(['SECRET' => null, 'OTHER_SECRET' => null]);
19+
if ($withLocalVault) {
20+
$localVault = $this->createMock(AbstractVault::class);
21+
$localVault->method('list')->willReturn(['SECRET' => null]);
22+
} else {
23+
$localVault = null;
24+
}
25+
$command = new SecretsRemoveCommand($vault, $localVault);
26+
$tester = new CommandCompletionTester($command);
27+
$suggestions = $tester->complete($input);
28+
$this->assertSame($expectedSuggestions, $suggestions);
29+
}
30+
31+
public function provideCompletionSuggestions()
32+
{
33+
yield 'name' => [true, [''], ['SECRET', 'OTHER_SECRET']];
34+
yield '--local name (with local vault)' => [true, ['--local', ''], ['SECRET']];
35+
yield '--local name (without local vault)' => [false, ['--local', ''], []];
36+
}
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Bundle\FrameworkBundle\Command\SecretsSetCommand;
7+
use Symfony\Bundle\FrameworkBundle\Secrets\AbstractVault;
8+
use Symfony\Component\Console\Tester\CommandCompletionTester;
9+
10+
class SecretsSetCommandTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider provideCompletionSuggestions
14+
*/
15+
public function testComplete(array $input, array $expectedSuggestions)
16+
{
17+
$vault = $this->createMock(AbstractVault::class);
18+
$vault->method('list')->willReturn(['SECRET' => null, 'OTHER_SECRET' => null]);
19+
$localVault = $this->createMock(AbstractVault::class);
20+
$command = new SecretsSetCommand($vault, $localVault);
21+
$tester = new CommandCompletionTester($command);
22+
$suggestions = $tester->complete($input);
23+
$this->assertSame($expectedSuggestions, $suggestions);
24+
}
25+
26+
public function provideCompletionSuggestions()
27+
{
28+
yield 'name' => [[''], ['SECRET', 'OTHER_SECRET']];
29+
yield '--local name (with local vault)' => [['--local', ''], ['SECRET', 'OTHER_SECRET']];
30+
}
31+
}

0 commit comments

Comments
 (0)