8000 Implements Option/ArgumentInput::complete() for openess · symfony/symfony@091027a · GitHub
[go: up one dir, main page]

Skip to content

Commit 091027a

Browse files
committed
Implements Option/ArgumentInput::complete() for openess
1 parent 373785a commit 091027a

File tree

6 files changed

+87
-43
lines changed

6 files changed

+87
-43
lines changed

src/Symfony/Component/Console/Command/Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,9 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
321321
{
322322
$definition = $this->getDefinition();
323323
if (CompletionInput::TYPE_OPTION_VALUE === $input->getCompletionType() && $definition->hasOption($input->getCompletionName())) {
324-
$suggestions->suggestValues($definition->getOption($input->getCompletionName())->getSuggestedValues($input));
324+
$definition->getOption($input->getCompletionName())->complete($input, $suggestions);
325325
} elseif (CompletionInput::TYPE_ARGUMENT_VALUE === $input->getCompletionType() && $definition->hasArgument($input->getCompletionName())) {
326-
$suggestions->suggestValues($definition->getArgument($input->getCompletionName())->getSuggestedValues($input));
326+
$definition->getArgument($input->getCompletionName())->complete($input, $suggestions);
327327
}
328328
}
329329

src/Symfony/Component/Console/Input/InputArgument.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Input;
1313

1414
use Symfony\Component\Console\Completion\CompletionInput;
15+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1516
use Symfony\Component\Console\Exception\InvalidArgumentException;
1617
use Symfony\Component\Console\Exception\LogicException;
1718

@@ -115,17 +116,25 @@ public function getDefault(): string|bool|int|float|array|null
115116
return $this->default;
116117
}
117118

119+
public function hasCompletion(): bool
120+
{
121+
return [] !== $this->suggestedValues;
122+
}
123+
118124
/**
119-
* Returns suggested values for input completion.
125+
* Adds suggestions to $suggestions for the current completion input.
126+
*
127+
* @see Command::complete()
120128
*/
121-
public function getSuggestedValues(CompletionInput $input): array
129+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
122130
{
123131
$values = $this->suggestedValues;
124132
if ($values instanceof \Closure && !\is_array($values = $values($input))) {
125-
throw new LogicException(sprintf('Closure for argument "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
133+
throw new LogicException(sprintf('Closure for argument "%s" must return an array or null. Got "%s".', $this->name, get_debug_type($values)));
134+
}
135+
if ($values) {
136+
$suggestions->suggestValues($values);
126137
}
127-
128-
return $values;
129138
}
130139

131140
/**

src/Symfony/Component/Console/Input/InputOption.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Component\Console\Input;
1313

14+
use Symfony\Component\Console\Command\Command;
1415
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Exception\InvalidArgumentException;
1618
use Symfony\Component\Console\Exception\LogicException;
1719

@@ -201,24 +203,32 @@ public function getDefault(): string|bool|int|float|array|null
201203
}
202204

203205
/**
204-
* Returns suggested values for input completion.
206+
* Returns the description text.
205207
*/
206-
public function getSuggestedValues(CompletionInput $input): array
208+
public function getDescription(): string
207209
{
208-
$values = $this->suggestedValues;
209-
if ($values instanceof \Closure && !\is_array($values = $values($input))) {
210-
throw new LogicException(sprintf('Closure for option "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
211-
}
210+
return $this->description;
211+
}
212212

213-
return $values;
213+
public function hasCompletion(): bool
214+
{
215+
return [] !== $this->suggestedValues;
214216
}
215217

216218
/**
217-
* Returns the description text.
219+
* Adds suggestions to $suggestions for the current completion input.
220+
*
221+
* @see Command::complete()
218222
*/
219-
public function getDescription(): string
223+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
220224
{
221-
return $this->description;
225+
$values = $this->suggestedValues;
226+
if ($values instanceof \Closure && !\is_array($values = $values($input))) {
227+
throw new LogicException(sprintf('Closure for option "%s" must return an array or null. Got "%s".', $this->name, get_debug_type($values)));
228+
}
229+
if ($values) {
230+
$suggestions->suggestValues($values);
231+
}
222232
}
223233

224234
/**

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Attribute\AsCommand;
1818
use Symfony\Component\Console\Command\Command;
1919
use Symfony\Component\Console\Completion\CompletionInput;
20+
use Symfony\Component\Console\Completion\CompletionSuggestions;
2021
use Symfony\Component\Console\Exception\InvalidOptionException;
2122
use Symfony\Component\Console\Helper\FormatterHelper;
2223
use Symfony\Component\Console\Input\InputArgument;
@@ -96,7 +97,7 @@ public function testAddArgumentFull()
9697
$argument = $command->getDefinition()->getArgument('foo');
9798
$this->assertSame('Description', $argument->getDescription());
9899
$this->assertSame('default', $argument->getDefault());
99-
$this->assertSame(['a', 'b'], $argument->getSuggestedValues(new CompletionInput()));
100+
$this->assertTrue($argument->hasCompletion());
100101
}
101102

102103
public function testAddOption()
@@ -115,7 +116,7 @@ public function testAddOptionFull()
115116
$this->assertSame('f', $option->getShortcut());
116117
$this->assertSame('Description', $option->getDescription());
117118
$this->assertSame('default', $option->getDefault());
118-
$this->assertSame(['a', 'b'], $option->getSuggestedValues(new CompletionInput()));
119+
$this->assertTrue($option->hasCompletion());
119120
}
120121

121122
public function testSetHidden()

src/Symfony/Component/Console/Tests/Input/InputArgumentTest.php

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

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
17+
use Symfony\Component\Console\Completion\Suggestion;
1618
use Symfony\Component\Console\Exception\LogicException;
1719
use Symfony\Component\Console\Input\InputArgument;
1820

@@ -98,22 +100,32 @@ public function testSetDefaultWithArrayArgument()
98100
$argument->setDefault('default');
99101
}
100102

101-
public function testGetSuggestedValues()
103+
public function testCompleteArray()
102104
{
103105
$values = ['foo', 'bar'];
104-
$option = new InputArgument('foo', null, '', null, $values);
105-
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
106+
$argument = new InputArgument('foo', null, '', null, $values);
107+
$this->assertTrue($argument->hasCompletion());
108+
$suggestions = new CompletionSuggestions();
109+
$argument->complete(new CompletionInput(), $suggestions);
110+
$this->assertSame($values, array_map(fn (Suggestion $suggestion) => $suggestion->getValue(), $suggestions->getValueSuggestions()));
111+
}
106112

107-
$option = new InputArgument('foo', null, '', null, fn (CompletionInput $input): array => $values);
108-
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
113+
public function testCompleteClosure()
114+
{
115+
$values = ['foo', 'bar'];
116+
$argument = new InputArgument('foo', null, '', null, fn (CompletionInput $input): array => $values);
117+
$this->assertTrue($argument->hasCompletion());
118+
$suggestions = new CompletionSuggestions();
119+
$argument->complete(new CompletionInput(), $suggestions);
120+
$this->assertSame($values, array_map(fn (Suggestion $suggestion) => $suggestion->getValue(), $suggestions->getValueSuggestions()));
109121
}
110122

111-
public function testGetSuggestedValuesClosureReturnIncorrectType()
123+
public function testCompleteClosureReturnIncorrectType()
112124
{
113125
$this->expectException(LogicException::class);
114-
$this->expectExceptionMessage('Closure for argument "foo" must return an array. Got "string".');
126+
$this->expectExceptionMessage('Closure for argument "foo" must return an array or null. Got "string".');
115127

116-
$option = new InputArgument('foo', InputArgument::OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid');
117-
$option->getSuggestedValues(new CompletionInput());
128+
$argument = new InputArgument('foo', InputArgument::OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid');
129+
$argument->complete(new CompletionInput(), new CompletionSuggestions());
118130
}
119131
}

src/Symfony/Component/Console/Tests/Input/InputOptionTest.php

Lines changed: 27 additions & 15 deletions
< 179B td data-grid-cell-id="diff-a101f55f8b360e9d2c5e6a064154132b1d44dc72f906d1f43ff3d65c3be9e0d9-14-14-2" data-line-anchor="diff-a101f55f8b360e9d2c5e6a064154132b1d44dc72f906d1f43ff3d65c3be9e0d9R14" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">
use PHPUnit\Framework\TestCase;
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
1515
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
17+
use Symfony\Component\Console\Completion\Suggestion;
1618
use Symfony\Component\Console\Exception\LogicException;
19+
use Symfony\Component\Console\Input\InputArgument;
1720
use Symfony\Component\Console\Input\InputOption;
1821

1922
class InputOptionTest extends TestCase
@@ -197,31 +200,40 @@ public function testEquals()
197200
$this->assertFalse($option->equals($option2));
198201
}
199202

200-
public function testGetSuggestedValues()
203+
public function testSuggestedValuesErrorIfNoValue()
201204
{
202-
$values = ['foo', 'bar'];
203-
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, $values);
204-
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
205+
$this->expectException(LogicException::class);
206+
$this->expectExceptionMessage('Cannot set suggested values if the option does not accept a value.');
205207

206-
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input): array => $values);
207-
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
208+
new InputOption('foo', null, InputOption::VALUE_NONE, '', null, ['foo']);
208209
}
209210

210-
public function testGetSuggestedValuesClosureReturnIncorrectType()
211+
public function testCompleteArray()
211212
{
212-
$this->expectException(LogicException::class);
213-
$this->expectExceptionMessage('Closure for option "foo" must return an array. Got "string".');
213+
$values = ['foo', 'bar'];
214+
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, $values);
215+
$this->assertTrue($option->hasCompletion());
216+
$suggestions = new CompletionSuggestions();
217+
$option->complete(new CompletionInput(), $suggestions);
218+
$this->assertSame($values, array_map(fn (Suggestion $suggestion) => $suggestion->getValue(), $suggestions->getValueSuggestions()));
219+
}
214220

215-
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid');
216-
$option->getSuggestedValues(new CompletionInput());
221+
public function testCompleteClosure()
222+
{
223+
$values = ['foo', 'bar'];
224+
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input): array => $values);
225+
$this->assertTrue($option->hasCompletion());
226+
$suggestions = new CompletionSuggestions();
227+
$option->complete(new CompletionInput(), $suggestions);
228+
$this->assertSame($values, array_map(fn (Suggestion $suggestion) => $suggestion->getValue(), $suggestions->getValueSuggestions()));
217229
}
218230

219-
public function testSuggestedValuesErrorIfNoValue()
231+
public function testCompleteClosureReturnIncorrectType()
220232
{
221233
$this->expectException(LogicException::class);
222-
$this->expectExceptionMessage('Cannot set suggested values if the option does not accept a value.');
234+
$this->expectExceptionMessage('Closure for option "foo" must return an array or null. Got "string".');
223235

224-
$option = new InputOption('foo', null, InputOption::VALUE_NONE, '', null, ['foo']);
225-
$option->getSuggestedValues(new CompletionInput());
236+
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid');
237+
$option->complete(new CompletionInput(), new CompletionSuggestions());
226238
}
227239
}

0 commit comments

Comments
 (0)
0