CompletionInputTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Completion;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Completion\CompletionInput;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputDefinition;
  15. use Symfony\Component\Console\Input\InputOption;
  16. class CompletionInputTest extends TestCase
  17. {
  18. /**
  19. * @dataProvider provideBindData
  20. */
  21. public function testBind(CompletionInput $input, string $expectedType, ?string $expectedName, string $expectedValue)
  22. {
  23. $definition = new InputDefinition([
  24. new InputOption('with-required-value', 'r', InputOption::VALUE_REQUIRED),
  25. new InputOption('with-optional-value', 'o', InputOption::VALUE_OPTIONAL),
  26. new InputOption('without-value', 'n', InputOption::VALUE_NONE),
  27. new InputArgument('required-arg', InputArgument::REQUIRED),
  28. new InputArgument('optional-arg', InputArgument::OPTIONAL),
  29. ]);
  30. $input->bind($definition);
  31. $this->assertEquals($expectedType, $input->getCompletionType(), 'Unexpected type');
  32. $this->assertEquals($expectedName, $input->getCompletionName(), 'Unexpected name');
  33. $this->assertEquals($expectedValue, $input->getCompletionValue(), 'Unexpected value');
  34. }
  35. public static function provideBindData()
  36. {
  37. // option names
  38. yield 'optname-minimal-input' => [CompletionInput::fromTokens(['bin/console', '-'], 1), CompletionInput::TYPE_OPTION_NAME, null, '-'];
  39. yield 'optname-partial' => [CompletionInput::fromTokens(['bin/console', '--with'], 1), CompletionInput::TYPE_OPTION_NAME, null, '--with'];
  40. // option values
  41. yield 'optval-short' => [CompletionInput::fromTokens(['bin/console', '-r'], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', ''];
  42. yield 'optval-short-partial' => [CompletionInput::fromTokens(['bin/console', '-rsymf'], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', 'symf'];
  43. yield 'optval-short-space' => [CompletionInput::fromTokens(['bin/console', '-r'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', ''];
  44. yield 'optval-short-space-partial' => [CompletionInput::fromTokens(['bin/console', '-r', 'symf'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', 'symf'];
  45. yield 'optval-short-before-arg' => [CompletionInput::fromTokens(['bin/console', '-r', 'symfony'], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', ''];
  46. yield 'optval-long' => [CompletionInput::fromTokens(['bin/console', '--with-required-value='], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', ''];
  47. yield 'optval-long-partial' => [CompletionInput::fromTokens(['bin/console', '--with-required-value=symf'], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', 'symf'];
  48. yield 'optval-long-space' => [CompletionInput::fromTokens(['bin/console', '--with-required-value'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', ''];
  49. yield 'optval-long-space-partial' => [CompletionInput::fromTokens(['bin/console', '--with-required-value', 'symf'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-required-value', 'symf'];
  50. yield 'optval-short-optional' => [CompletionInput::fromTokens(['bin/console', '-o'], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-optional-value', ''];
  51. yield 'optval-short-space-optional' => [CompletionInput::fromTokens(['bin/console', '-o'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-optional-value', ''];
  52. yield 'optval-long-optional' => [CompletionInput::fromTokens(['bin/console', '--with-optional-value='], 1), CompletionInput::TYPE_OPTION_VALUE, 'with-optional-value', ''];
  53. yield 'optval-long-space-optional' => [CompletionInput::fromTokens(['bin/console', '--with-optional-value'], 2), CompletionInput::TYPE_OPTION_VALUE, 'with-optional-value', ''];
  54. // arguments
  55. yield 'arg-minimal-input' => [CompletionInput::fromTokens(['bin/console'], 1), CompletionInput::TYPE_ARGUMENT_VALUE, 'required-arg', ''];
  56. yield 'arg-optional' => [CompletionInput::fromTokens(['bin/console', 'symfony'], 2), CompletionInput::TYPE_ARGUMENT_VALUE, 'optional-arg', ''];
  57. yield 'arg-partial' => [CompletionInput::fromTokens(['bin/console', 'symf'], 1), CompletionInput::TYPE_ARGUMENT_VALUE, 'required-arg', 'symf'];
  58. yield 'arg-optional-partial' => [CompletionInput::fromTokens(['bin/console', 'symfony', 'sen'], 2), CompletionInput::TYPE_ARGUMENT_VALUE, 'optional-arg', 'sen'];
  59. yield 'arg-after-option' => [CompletionInput::fromTokens(['bin/console', '--without-value'], 2), CompletionInput::TYPE_ARGUMENT_VALUE, 'required-arg', ''];
  60. yield 'arg-after-optional-value-option' => [CompletionInput::fromTokens(['bin/console', '--with-optional-value', '--'], 3), CompletionInput::TYPE_ARGUMENT_VALUE, 'required-arg', ''];
  61. // end of definition
  62. yield 'end' => [CompletionInput::fromTokens(['bin/console', 'symfony', 'sensiolabs'], 3), CompletionInput::TYPE_NONE, null, ''];
  63. }
  64. /**
  65. * @dataProvider provideBindWithLastArrayArgumentData
  66. */
  67. public function testBindWithLastArrayArgument(CompletionInput $input, ?string $expectedValue)
  68. {
  69. $definition = new InputDefinition([
  70. new InputArgument('list-arg', InputArgument::IS_ARRAY | InputArgument::REQUIRED),
  71. ]);
  72. $input->bind($definition);
  73. $this->assertEquals(CompletionInput::TYPE_ARGUMENT_VALUE, $input->getCompletionType(), 'Unexpected type');
  74. $this->assertEquals('list-arg', $input->getCompletionName(), 'Unexpected name');
  75. $this->assertEquals($expectedValue, $input->getCompletionValue(), 'Unexpected value');
  76. }
  77. public static function provideBindWithLastArrayArgumentData()
  78. {
  79. yield [CompletionInput::fromTokens(['bin/console'], 1), null];
  80. yield [CompletionInput::fromTokens(['bin/console', 'symfony', 'sensiolabs'], 3), null];
  81. yield [CompletionInput::fromTokens(['bin/console', 'symfony', 'sen'], 2), 'sen'];
  82. }
  83. public function testBindArgumentWithDefault()
  84. {
  85. $definition = new InputDefinition([
  86. new InputArgument('arg-with-default', InputArgument::OPTIONAL, '', 'default'),
  87. ]);
  88. $input = CompletionInput::fromTokens(['bin/console'], 1);
  89. $input->bind($definition);
  90. $this->assertEquals(CompletionInput::TYPE_ARGUMENT_VALUE, $input->getCompletionType(), 'Unexpected type');
  91. $this->assertEquals('arg-with-default', $input->getCompletionName(), 'Unexpected name');
  92. $this->assertEquals('', $input->getCompletionValue(), 'Unexpected value');
  93. }
  94. /**
  95. * @dataProvider provideFromStringData
  96. */
  97. public function testFromString($inputStr, array $expectedTokens)
  98. {
  99. $input = CompletionInput::fromString($inputStr, 1);
  100. $tokensProperty = (new \ReflectionClass($input))->getProperty('tokens');
  101. $tokensProperty->setAccessible(true);
  102. $this->assertEquals($expectedTokens, $tokensProperty->getValue($input));
  103. }
  104. public static function provideFromStringData()
  105. {
  106. yield ['bin/console cache:clear', ['bin/console', 'cache:clear']];
  107. yield ['bin/console --env prod', ['bin/console', '--env', 'prod']];
  108. yield ['bin/console --env=prod', ['bin/console', '--env=prod']];
  109. yield ['bin/console -eprod', ['bin/console', '-eprod']];
  110. yield ['bin/console cache:clear "multi word string"', ['bin/console', 'cache:clear', '"multi word string"']];
  111. yield ['bin/console cache:clear \'multi word string\'', ['bin/console', 'cache:clear', '\'multi word string\'']];
  112. }
  113. }