Skip to content

Commit 117fdf6

Browse files
authored
Add a method to get the input raw arguments (#160)
1 parent 323d773 commit 117fdf6

File tree

4 files changed

+121
-6
lines changed

4 files changed

+121
-6
lines changed

phpstan-tests.neon.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ parameters:
3838
- path: tests/Process/SymfonyProcessLauncherTest.php
3939
message: '#DummyProcess#'
4040

41-
- path: tests/Input/RawOptionsInputTest.php
41+
- path: tests/Input/RawInputTest.php
4242
message: '#FakeInput#'
4343

4444
- path: tests/ParallelExecutorFactoryTest.php

src/Input/InputOptionsSerializer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function serialize(
4949
array $excludedOptionNames
5050
): array {
5151
$filteredOptions = array_diff_key(
52-
RawOptionsInput::getRawOptions($input),
52+
RawInput::getRawOptions($input),
5353
array_fill_keys($excludedOptionNames, ''),
5454
);
5555

src/Input/RawOptionsInput.php renamed to src/Input/RawInput.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* @internal
2323
*/
24-
final class RawOptionsInput extends Input
24+
final class RawInput extends Input
2525
{
2626
/**
2727
* @codeCoverageIgnore
@@ -31,6 +31,18 @@ private function __construct(?InputDefinition $definition)
3131
parent::__construct($definition);
3232
}
3333

34+
/**
35+
* Returns all the given arguments NOT merged with the default values.
36+
*
37+
* @return array<string|bool|int|float|null|array<string|bool|int|float|null>>
38+
*/
39+
public static function getRawArguments(InputInterface $input): array
40+
{
41+
return $input instanceof Input
42+
? $input->arguments
43+
: [];
44+
}
45+
3446
/**
3547
* Returns all the given options NOT merged with the default values.
3648
*

tests/Input/RawOptionsInputTest.php renamed to tests/Input/RawInputTest.php

+106-3
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,129 @@
1515

1616
use PHPUnit\Framework\TestCase;
1717
use Symfony\Component\Console\Input\ArrayInput;
18+
use Symfony\Component\Console\Input\InputArgument;
1819
use Symfony\Component\Console\Input\InputDefinition;
1920
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Input\InputOption;
2122
use Webmozarts\Console\Parallelization\SymfonyVersion;
2223

2324
/**
24-
* @covers \Webmozarts\Console\Parallelization\Input\RawOptionsInput
25+
* @covers \Webmozarts\Console\Parallelization\Input\RawInput
2526
*
2627
* @internal
2728
*/
28-
final class RawOptionsInputTest extends TestCase
29+
final class RawInputTest extends TestCase
2930
{
31+
/**
32+
* @dataProvider inputArgumentProvider
33+
*/
34+
public function test_it_can_get_an_input_arguments(
35+
InputInterface $input,
36+
array $expected
37+
): void {
38+
$actual = RawInput::getRawArguments($input);
39+
40+
self::assertSame($expected, $actual);
41+
}
42+
43+
public static function inputArgumentProvider(): iterable
44+
{
45+
$isSymfony4 = SymfonyVersion::isSymfony4();
46+
47+
yield 'input with no arguments' => [
48+
new ArrayInput([], null),
49+
[],
50+
];
51+
52+
yield 'input with arguments default arguments' => [
53+
new ArrayInput(
54+
[],
55+
new InputDefinition([
56+
new InputArgument(
57+
'arg1',
58+
InputArgument::OPTIONAL,
59+
),
60+
new InputArgument(
61+
'arg2',
62+
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
63+
),
64+
]),
65+
),
66+
[],
67+
];
68+
69+
yield 'input with minimum arguments' => [
70+
new ArrayInput(
71+
[
72+
'arg1' => 'value1',
73+
'arg2' => null,
74+
'arg3' => null,
75+
],
76+
new InputDefinition([
77+
new InputArgument(
78+
'arg1',
79+
InputArgument::REQUIRED,
80+
),
81+
new InputArgument(
82+
'arg2',
83+
InputArgument::OPTIONAL,
84+
),
85+
new InputArgument(
86+
'arg3',
87+
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
88+
),
89+
]),
90+
),
91+
[
92+
'arg1' => 'value1',
93+
'arg2' => null,
94+
'arg3' => null,
95+
],
96+
];
97+
98+
yield 'input with all arguments' => [
99+
new ArrayInput(
100+
[
101+
'arg1' => 'value1',
102+
'arg2' => 'value2',
103+
'arg3' => 'value3 value4',
104+
],
105+
new InputDefinition([
106+
new InputArgument(
107+
'arg1',
108+
InputArgument::REQUIRED,
109+
),
110+
new InputArgument(
111+
'arg2',
112+
InputArgument::OPTIONAL,
113+
),
114+
new InputArgument(
115+
'arg3',
116+
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
117+
),
118+
]),
119+
),
120+
[
121+
'arg1' => 'value1',
122+
'arg2' => 'value2',
123+
'arg3' => 'value3 value4',
124+
],
125+
];
126+
127+
yield 'non standard input' => [
128+
new FakeInput(),
129+
[],
130+
];
131+
}
132+
30133
/**
31134
* @dataProvider inputOptionProvider
32135
*/
33136
public function test_it_can_get_an_input_options(
34137
InputInterface $input,
35138
array $expected
36139
): void {
37-
$actual = RawOptionsInput::getRawOptions($input);
140+
$actual = RawInput::getRawOptions($input);
38141

39142
self::assertSame($expected, $actual);
40143
}

0 commit comments

Comments
 (0)