123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?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\Completion;
- 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 static 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 static 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'];
- }
- public function testBindArgumentWithDefault()
- {
- $definition = new InputDefinition([
- new InputArgument('arg-with-default', InputArgument::OPTIONAL, '', 'default'),
- ]);
- $input = CompletionInput::fromTokens(['bin/console'], 1);
- $input->bind($definition);
- $this->assertEquals(CompletionInput::TYPE_ARGUMENT_VALUE, $input->getCompletionType(), 'Unexpected type');
- $this->assertEquals('arg-with-default', $input->getCompletionName(), 'Unexpected name');
- $this->assertEquals('', $input->getCompletionValue(), 'Unexpected value');
- }
- /**
- * @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 static 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\'']];
- }
- }
|