/
CompleteCommandTest.php
151 lines (127 loc) · 5.64 KB
/
CompleteCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?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\Component\Console\Tests\Command;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\CompleteCommand;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Completion\Output\BashCompletionOutput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
class CompleteCommandTest extends TestCase
{
private $command;
private $application;
private $tester;
protected function setUp(): void
{
$this->command = new CompleteCommand();
$this->application = new Application();
$this->application->add(new CompleteCommandTest_HelloCommand());
$this->command->setApplication($this->application);
$this->tester = new CommandTester($this->command);
}
public function testRequiredShellOption()
{
$this->expectExceptionMessage('The "--shell" option must be set.');
$this->execute([]);
}
public function testUnsupportedShellOption()
{
$this->expectExceptionMessage('Shell completion is not supported for your shell: "unsupported" (supported: "bash").');
$this->execute(['--shell' => 'unsupported']);
}
public function testAdditionalShellSupport()
{
$this->command = new CompleteCommand(['supported' => BashCompletionOutput::class]);
$this->command->setApplication($this->application);
$this->tester = new CommandTester($this->command);
$this->execute(['--shell' => 'supported', '--current' => '1', '--input' => ['bin/console']]);
// verify that the default set of shells is still supported
$this->execute(['--shell' => 'bash', '--current' => '1', '--input' => ['bin/console']]);
$this->assertTrue(true);
}
/**
* @dataProvider provideInputAndCurrentOptionValues
*/
public function testInputAndCurrentOptionValidation(array $input, ?string $exceptionMessage)
{
if ($exceptionMessage) {
$this->expectExceptionMessage($exceptionMessage);
}
$this->execute($input + ['--shell' => 'bash']);
if (!$exceptionMessage) {
$this->tester->assertCommandIsSuccessful();
}
}
public function provideInputAndCurrentOptionValues()
{
yield [[], 'The "--current" option must be set and it must be an integer'];
yield [['--current' => 'a'], 'The "--current" option must be set and it must be an integer'];
yield [['--current' => '1', '--input' => ['bin/console']], null];
yield [['--current' => '2', '--input' => ['bin/console']], 'Current index is invalid, it must be the number of input tokens or one more.'];
yield [['--current' => '1', '--input' => ['bin/console', 'cache:clear']], null];
yield [['--current' => '2', '--input' => ['bin/console', 'cache:clear']], null];
}
/**
* @dataProvider provideCompleteCommandNameInputs
*/
public function testCompleteCommandName(array $input, array $suggestions)
{
$this->execute(['--current' => '1', '--input' => $input]);
$this->assertEquals(implode("\n", $suggestions).\PHP_EOL, $this->tester->getDisplay());
}
public function provideCompleteCommandNameInputs()
{
yield 'empty' => [['bin/console'], ['help', 'list', 'completion', 'hello', 'ahoy']];
yield 'partial' => [['bin/console', 'he'], ['help', 'list', 'completion', 'hello', 'ahoy']];
yield 'complete-shortcut-name' => [['bin/console', 'hell'], ['hello', 'ahoy']];
yield 'complete-aliases' => [['bin/console', 'ah'], ['hello', 'ahoy']];
}
/**
* @dataProvider provideCompleteCommandInputDefinitionInputs
*/
public function testCompleteCommandInputDefinition(array $input, array $suggestions)
{
$this->execute(['--current' => '2', '--input' => $input]);
$this->assertEquals(implode("\n", $suggestions).\PHP_EOL, $this->tester->getDisplay());
}
public function provideCompleteCommandInputDefinitionInputs()
{
yield 'definition' => [['bin/console', 'hello', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-ansi', '--no-interaction']];
yield 'custom' => [['bin/console', 'hello'], ['Fabien', 'Robin', 'Wouter']];
yield 'definition-aliased' => [['bin/console', 'ahoy', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-ansi', '--no-interaction']];
yield 'custom-aliased' => [['bin/console', 'ahoy'], ['Fabien', 'Robin', 'Wouter']];
}
private function execute(array $input)
{
// run in verbose mode to assert exceptions
$this->tester->execute($input ? ($input + ['--shell' => 'bash']) : $input, ['verbosity' => OutputInterface::VERBOSITY_DEBUG]);
}
}
class CompleteCommandTest_HelloCommand extends Command
{
public function configure(): void
{
$this->setName('hello')
->setAliases(['ahoy'])
->addArgument('name', InputArgument::REQUIRED)
;
}
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('name')) {
$suggestions->suggestValues(['Fabien', 'Robin', 'Wouter']);
}
}
}