-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
CompletionInputTest.php
122 lines (102 loc) · 7.17 KB
/
CompletionInputTest.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
<?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\Completion\CompletionInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
class CompletionInputTest extends TestCase
{
/**
* @dataProvider provideBindData
*/
public function testBind(CompletionInput $input, string $expectedType, ?string $expectedName, string $expectedValue)
{
$definition = new InputDefinition([
new InputOption('with-required-value', 'r', InputOption::VALUE_REQUIRED),
new InputOption('with-optional-value', 'o', InputOption::VALUE_OPTIONAL),
new InputOption('without-value', 'n', InputOption::VALUE_NONE),
new InputArgument('required-arg', InputArgument::REQUIRED),
new InputArgument('optional-arg', InputArgument::OPTIONAL),
]);
$input->bind($definition);
$this->assertEquals($expectedType, $input->getCompletionType(), 'Unexpected type');
$this->assertEquals($expectedName, $input->getCompletionName(), 'Unexpected name');
$this->assertEquals($expectedValue, $input->getCompletionValue(), 'Unexpected value');
}
public function provideBindData()
{
// option names
yield 'optname-minimal-input' => [CompletionInput::fromTokens(['bin/console', '-'], 1), CompletionInput::TYPE_OPTION_NAME, null, '-'];
yield 'optname-partial' => [CompletionInput::fromTokens(['bin/console', '--with'], 1), CompletionInput::TYPE_OPTION_NAME, null, '--with'];
// option values
yield 'optval-short' => [CompletionInput::fromTokens(['bin/console', '-r'], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', ''];
yield 'optval-short-partial' => [CompletionInput::fromTokens(['bin/console', '-rsymf'], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', 'symf'];
yield 'optval-short-space' => [CompletionInput::fromTokens(['bin/console', '-r'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', ''];
yield 'optval-short-space-partial' => [CompletionInput::fromTokens(['bin/console', '-r', 'symf'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', 'symf'];
yield 'optval-short-before-arg' => [CompletionInput::fromTokens(['bin/console', '-r', 'symfony'], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', ''];
yield 'optval-long' => [CompletionInput::fromTokens(['bin/console', '--with-required-value='], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', ''];
yield 'optval-long-partial' => [CompletionInput::fromTokens(['bin/console', '--with-required-value=symf'], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', 'symf'];
yield 'optval-long-space' => [CompletionInput::fromTokens(['bin/console', '--with-required-value'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', ''];
yield 'optval-long-space-partial' => [CompletionInput::fromTokens(['bin/console', '--with-required-value', 'symf'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', 'symf'];
yield 'optval-short-optional' => [CompletionInput::fromTokens(['bin/console', '-o'], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-optional-value', ''];
yield 'optval-short-space-optional' => [CompletionInput::fromTokens(['bin/console', '-o'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-optional-value', ''];
yield 'optval-long-optional' => [CompletionInput::fromTokens(['bin/console', '--with-optional-value='], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-optional-value', ''];
yield 'optval-long-space-optional' => [CompletionInput::fromTokens(['bin/console', '--with-optional-value'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-optional-value', ''];
// arguments
yield 'arg-minimal-input' => [CompletionInput::fromTokens(['bin/console'], 1), CompletionInput::TYPE_ARGUMENT_VALUE, 'required-arg', ''];
yield 'arg-optional' => [CompletionInput::fromTokens(['bin/console', 'symfony'], 2), CompletionInput::TYPE_ARGUMENT_VALUE, 'optional-arg', ''];
yield 'arg-partial' => [CompletionInput::fromTokens(['bin/console', 'symf'], 1), CompletionInput::TYPE_ARGUMENT_VALUE, 'required-arg', 'symf'];
yield 'arg-optional-partial' => [CompletionInput::fromTokens(['bin/console', 'symfony', 'sen'], 2), CompletionInput::TYPE_ARGUMENT_VALUE, 'optional-arg', 'sen'];
yield 'arg-after-option' => [CompletionInput::fromTokens(['bin/console', '--without-value'], 2), CompletionInput::TYPE_ARGUMENT_VALUE, 'required-arg', ''];
yield 'arg-after-optional-value-option' => [CompletionInput::fromTokens(['bin/console', '--with-optional-value', '--'], 3), CompletionInput::TYPE_ARGUMENT_VALUE, 'required-arg', ''];
// end of definition
yield 'end' => [CompletionInput::fromTokens(['bin/console', 'symfony', 'sensiolabs'], 3), CompletionInput::TYPE_NONE, null, ''];
}
/**
* @dataProvider provideBindWithLastArrayArgumentData
*/
public function testBindWithLastArrayArgument(CompletionInput $input, ?string $expectedValue)
{
$definition = new InputDefinition([
new InputArgument('list-arg', InputArgument::IS_ARRAY | InputArgument::REQUIRED),
]);
$input->bind($definition);
$this->assertEquals(CompletionInput::TYPE_ARGUMENT_VALUE, $input->getCompletionType(), 'Unexpected type');
$this->assertEquals('list-arg', $input->getCompletionName(), 'Unexpected name');
$this->assertEquals($expectedValue, $input->getCompletionValue(), 'Unexpected value');
}
public function provideBindWithLastArrayArgumentData()
{
yield [CompletionInput::fromTokens(['bin/console'], 1), null];
yield [CompletionInput::fromTokens(['bin/console', 'symfony', 'sensiolabs'], 3), null];
yield [CompletionInput::fromTokens(['bin/console', 'symfony', 'sen'], 2), 'sen'];
}
/**
* @dataProvider provideFromStringData
*/
public function testFromString($inputStr, array $expectedTokens)
{
$input = CompletionInput::fromString($inputStr, 1);
$tokensProperty = (new \ReflectionClass($input))->getProperty('tokens');
$tokensProperty->setAccessible(true);
$this->assertEquals($expectedTokens, $tokensProperty->getValue($input));
}
public function provideFromStringData()
{
yield ['bin/console cache:clear', ['bin/console', 'cache:clear']];
yield ['bin/console --env prod', ['bin/console', '--env', 'prod']];
yield ['bin/console --env=prod', ['bin/console', '--env=prod']];
yield ['bin/console -eprod', ['bin/console', '-eprod']];
yield ['bin/console cache:clear "multi word string"', ['bin/console', 'cache:clear', '"multi word string"']];
yield ['bin/console cache:clear \'multi word string\'', ['bin/console', 'cache:clear', '\'multi word string\'']];
}
}