10000 Add tests · symfony/symfony@4074097 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4074097

Browse files
committed
Add tests
1 parent 0e31423 commit 4074097

File tree

9 files changed

+121
-29
lines changed

9 files changed

+121
-29
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHANGELOG
66

77
* Add method `__toString()` to `InputInterface`
88
* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead
9-
* Add completion values for arguments and options in input definition
9+
* Add suggested values for arguments and options in input definition, for input completion
1010

1111
6.0
1212
---

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,18 @@ public function getNativeDefinition(): InputDefinition
435435
*
436436
* @param $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
437437
* @param $default The default value (for InputArgument::OPTIONAL mode only)
438-
* @param array|Closure(CompletionInput):array $suggestedValues The values used for input completion
438+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
439439
*
440440
* @throws InvalidArgumentException When argument mode is not valid
441441
*
442442
* @return $this
443443
*/
444-
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = null): static
444+
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null, /*array|\Closure $suggestedValues = null*/): static
445445
{
446+
$suggestedValues = 5 <= \func_num_args() ? func_get_arg(4) : [];
447+
if (!\is_array($suggestedValues) && !$suggestedValues instanceof \Closure) {
448+
throw new \TypeError(sprintf('Argument 5 passed to "%s()" must be array or \Closure, "%s" given.', __METHOD__, get_debug_type($suggestedValues)));
449+
}
446450
$this->definition->addArgument(new InputArgument($name, $mode, $description, $default, $suggestedValues));
447451
$this->fullDefinition?->addArgument(new InputArgument($name, $mode, $description, $default, $suggestedValues));
448452

@@ -455,14 +459,18 @@ public function addArgument(string $name, int $mode = null, string $description
455459
* @param $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
456460
* @param $mode The option mode: One of the InputOption::VALUE_* constants
457461
* @param $default The default value (must be null for InputOption::VALUE_NONE)
458-
* @param array|Closure(CompletionInput):array $suggestedValues The values used for input completion
462+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
459463
*
460464
* @throws InvalidArgumentException If option mode is invalid or incompatible
461465
*
462466
* @return $this
463467
*/
464-
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = null): static
468+
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null, /*array|\Closure $suggestedValues = []*/): static
465469
{
470+
$suggestedValues = 6 <= \func_num_args() ? func_get_arg(5) : [];
471+
if (!\is_array($suggestedValues) && !$suggestedValues instanceof \Closure) {
472+
throw new \TypeError(sprintf('Argument 5 passed to "%s()" must be array or \Closure, "%s" given.', __METHOD__, get_debug_type($suggestedValues)));
473+
}
466474
$this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default, $suggestedValues));
467475
$this->fullDefinition?->addOption(new InputOption($name, $shortcut, $mode, $description, $default, $suggestedValues));
468476

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function configure()
7575
<info>eval "$(${fullCommand} completion bash)"</>
7676
EOH
7777
)
78-
->addArgument('shell', InputArgument::OPTIONAL, 'The shell type (e.g. "bash"), the value of the "$SHELL" env var will be used if this is not given', null, $this->getSupportedShells())
78+
->addArgument('shell', InputArgument::OPTIONAL, 'The shell type (e.g. "bash"), the value of the "$SHELL" env var will be used if this is not given', null, fn () => $this->getSupportedShells())
7979
->addOption('debug', null, InputOption::VALUE_NONE, 'Tail the completion debug log')
8080
;
8181
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,27 @@ public function getNativeDefinition(): InputDefinition
108108
return $this->getCommand()->getNativeDefinition();
109109
}
110110

111-
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = null): static
111+
/**
112+
* {@inheritDoc}
113+
*
114+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
115+
*/
116+
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null, /*array|\Closure $suggestedValues = []*/): static
112117
{
118+
$suggestedValues = 5 <= \func_num_args() ? func_get_arg(4) : [];
113119
$this->getCommand()->addArgument($name, $mode, $description, $default, $suggestedValues);
114120

115121
return $this;
116122
}
117123

118-
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = null): static
124+
/**
125+
* {@inheritDoc}
126+
*
127+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
128+
*/
129+
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null, /*array|\Closure $suggestedValues = []*/): static
119130
{
131+
$suggestedValues = 6 <= \func_num_args() ? func_get_arg(5) : [];
120132
$this->getCommand()->addOption($name, $shortcut, $mode, $description, $default, $suggestedValues);
121133

122134
return $this;

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ class InputArgument
2929
private string $name;
3030
private int $mode;
3131
private string|int|bool|array|null|float $default;
32-
private array|\Closure|null $suggestedValues;
32+
private array|\Closure $suggestedValues;
3333
private string $description;
3434

3535
/**
36-
* @param string $name The argument name
37-
* @param int|null $mode The argument mode: self::REQUIRED or self::OPTIONAL
38-
* @param string $description A description text
39-
* @param string|bool|int|float|array|null $default The default value (for self::OPTIONAL mode only)
40-
* @param array|Closure(CompletionInput):array $suggestedValues The values used for input completion
36+
* @param string $name The argument name
37+
* @param int|null $mode The argument mode: self::REQUIRED or self::OPTIONAL
38+
* @param string $description A description text
39+
* @param string|bool|int|float|array|null $default The default value (for self::OPTIONAL mode only)
40+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
4141
*
4242
* @throws InvalidArgumentException When argument mode is not valid
4343
*/
44-
public function __construct(string $name, int $mode = null, string $description = '', string|bool|int|float|array $default = null, \Closure|array $suggestedValues = null)
44+
public function __construct(string $name, int $mode = null, string $description = '', string|bool|int|float|array $default = null, \Closure|array $suggestedValues = [])
4545
{
4646
if (null === $mode) {
4747
$mode = self::OPTIONAL;
@@ -122,10 +122,10 @@ public function getSuggestedValues(CompletionInput $input): array
122122
{
123123
$values = $this->suggestedValues;
124124
if ($values instanceof \Closure && !\is_array($values = $values($input))) {
125-
throw new LogicException(sprintf('Closure for "%s" "%s" must return an array. Got "%s".', $input->getCompletionType(), $input->getCompletionName(), get_debug_type($values)));
125+
throw new LogicException(sprintf('Closure for argument "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
126126
}
127127

128-
return $values ?? [];
128+
return $values;
129129
}
130130

131131
/**

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ class InputOption
5151
private string|array|null $shortcut;
5252
private int $mode;
5353
private string|int|bool|array|null|float $default;
54-
private array|\Closure|null $suggestedValues;
54+
private array|\Closure $suggestedValues;
5555
private string $description;
5656

5757
/**
58-
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
59-
* @param int|null $mode The option mode: One of the VALUE_* constants
60-
* @param string|bool|int|float|array|null $default The default value (must be null for self::VALUE_NONE)
61-
* @param array|Closure(CompletionInput):array $suggestedValues The values used for input completion
58+
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
59+
* @param int|null $mode The option mode: One of the VALUE_* constants
60+
* @param string|bool|int|float|array|null $default The default value (must be null for self::VALUE_NONE)
61+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
6262
*
6363
* @throws InvalidArgumentException If option mode is invalid or incompatible
6464
*/
65-
public function __construct(string $name, string|array $shortcut = null, int $mode = null, string $description = '', string|bool|int|float|array $default = null, array|\Closure $suggestedValues = null)
65+
public function __construct(string $name, string|array $shortcut = null, int $mode = null, string $description = '', string|bool|int|float|array $default = null, array|\Closure $suggestedValues = [])
6666
{
6767
if (str_starts_with($name, '--')) {
6868
$name = substr($name, 2);
@@ -101,10 +101,9 @@ public function __construct(string $name, string|array $shortcut = null, int $mo
101101
$this->description = $description;
102102
$this->suggestedValues = $suggestedValues;
103103

104-
if (null !== $this->suggestedValues && !$this->acceptValue()) {
105-
throw new LogicException('Suggested values cannot be set if the option does not accept a value.');
104+
if ([] !== $this->suggestedValues && !$this->acceptValue()) {
105+
throw new LogicException('Cannot set suggested values if the option does not accept a value.');
106106
}
107-
108107
if ($this->isArray() && !$this->acceptValue()) {
109108
throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
110109
}
@@ -207,11 +206,11 @@ public function getDefault(): string|bool|int|float|array|null
207206
public function getSuggestedValues(CompletionInput $input): array
208207
{
209208
$values = $this->suggestedValues;
210-
if ($values instanceof \Closure && \is_array($values = $values($input))) {
211-
throw new LogicException(sprintf('Closure for "%s" "%s" must return an array. Got "%s".', $input->getCompletionType(), $input->getCompletionName(), get_debug_type($values)));
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)));
212211
}
213212

214-
return $values ?? [];
213+
return $values;
215214
}
216215

217216
/**

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Application;
1717
use Symfony\Component\Console\Attribute\AsCommand;
1818
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Completion\CompletionInput;
1920
use Symfony\Component\Console\Exception\InvalidOptionException;
2021
use Symfony\Component\Console\Helper\FormatterHelper;
2122
use Symfony\Component\Console\Input\InputArgument;
@@ -88,6 +89,16 @@ public function testAddArgument()
8889
$this->assertTrue($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command');
8990
}
9091

92+
public function testAddArgumentFull()
93+
{
94+
$command = new \TestCommand();
95+
$command->addArgument('foo', InputArgument::OPTIONAL, 'Description', 'default', ['a', 'b']);
96+
$argument = $command->getDefinition()->getArgument('foo');
97+
$this->assertSame('Description', $argument->getDescription());
98+
$this->assertSame('default', $argument->getDefault());
99+
$this->assertSame(['a', 'b'], $argument->getSuggestedValues(new CompletionInput()));
100+
}
101+
91102
public function testAddOption()
92103
{
93104
$command = new \TestCommand();
@@ -96,6 +107,17 @@ public function testAddOption()
96107
$this->assertTrue($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command');
97108
}
98109

110+
public function testAddOptionFull()
111+
{
112+
$command = new \TestCommand();
113+
$command->addOption('foo', ['f'], InputOption::VALUE_OPTIONAL, 'Description', 'default', ['a', 'b']);
114+
$option = $command->getDefinition()->getOption('foo');
115+
$this->assertSame('f', $option->getShortcut());
116+
$this->assertSame('Description', $option->getDescription());
117+
$this->assertSame('default', $option->getDefault());
118+
$this->assertSame(['a', 'b'], $option->getSuggestedValues(new CompletionInput()));
119+
}
120+
99121
public function testSetHidden()
100122
{
101123
$command = new \TestCommand();

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Console\Tests\Input;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Exception\LogicException;
1517
use Symfony\Component\Console\Input\InputArgument;
1618

1719
class InputArgumentTest extends TestCase
@@ -95,4 +97,23 @@ public function testSetDefaultWithArrayArgument()
9597
$argument = new InputArgument('foo', InputArgument::IS_ARRAY);
9698
$argument->setDefault('default');
9799
}
100+
101+
public function testGetSuggestedValues()
102+
{
103+
$values = ['foo', 'bar'];
104+
$option = new InputArgument('foo', null, '', null, $values);
105+
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
106+
107+
$option = new InputArgument('foo', null, '', null, fn (CompletionInput $input): array => $values);
108+
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
109+
}
110+
111+
public function testGetSuggestedValuesClosureReturnIncorrectType()
112+
{
113+
$this->expectException(LogicException::class);
114+
$this->expectExceptionMessage('Closure for argument "foo" must return an array. Got "string".');
115+
116+
$option = new InputArgument('foo', InputArgument::OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid');
117+
$option->getSuggestedValues(new CompletionInput());
118+
}
98119
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Console\Tests\Input;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Exception\LogicException;
1517
use Symfony\Component\Console\Input\InputOption;
1618

1719
class InputOptionTest extends TestCase
@@ -194,4 +196,32 @@ public function testEquals()
194196
$option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
195197
$this->assertFalse($option->equals($option2));
196198
}
199+
200+
public function testGetSuggestedValues()
201+
{
202+
$values = ['foo', 'bar'];
203+
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, $values);
204+
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
205+
206+
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input): array => $values);
207+
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
208+
}
209+
210+
public function testGetSuggestedValuesClosureReturnIncorrectType()
211+
{
212+
$this->expectException(LogicException::class);
213+
$this->expectExceptionMessage('Closure for option "foo" must return an array. Got "string".');
214+
215+
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid');
216+
$option->getSuggestedValues(new CompletionInput());
217+
}
218+
219+
public function testSuggestedValuesErrorIfNoValue()
220+
{
221+
$this->expectException(LogicException::class);
222+
$this->expectExceptionMessage('Cannot set suggested values if the option does not accept a value.');
223+
224+
$option = new InputOption('foo', null, InputOption::VALUE_NONE, '', null, ['foo']);
225+
$option->getSuggestedValues(new CompletionInput());
226+
}
197227
}

0 commit comments

Comments
 (0)
0