diff --git a/Completion/Output/BashCompletionOutput.php b/Completion/Output/BashCompletionOutput.php index 8d5ffa6b6..c6f76eb8f 100644 --- a/Completion/Output/BashCompletionOutput.php +++ b/Completion/Output/BashCompletionOutput.php @@ -24,6 +24,9 @@ public function write(CompletionSuggestions $suggestions, OutputInterface $outpu $values = $suggestions->getValueSuggestions(); foreach ($suggestions->getOptionSuggestions() as $option) { $values[] = '--'.$option->getName(); + if ($option->isNegatable()) { + $values[] = '--no-'.$option->getName(); + } } $output->writeln(implode("\n", $values)); } diff --git a/Tests/Command/CompleteCommandTest.php b/Tests/Command/CompleteCommandTest.php index f3ffe7b50..642d5c69a 100644 --- a/Tests/Command/CompleteCommandTest.php +++ b/Tests/Command/CompleteCommandTest.php @@ -119,9 +119,9 @@ public function testCompleteCommandInputDefinition(array $input, array $suggesti public function provideCompleteCommandInputDefinitionInputs() { - yield 'definition' => [['bin/console', 'hello', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-interaction']]; + yield 'definition' => [['bin/console', 'hello', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-ansi', '--no-interaction']]; yield 'custom' => [['bin/console', 'hello'], ['Fabien', 'Robin', 'Wouter']]; - yield 'definition-aliased' => [['bin/console', 'ahoy', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-interaction']]; + yield 'definition-aliased' => [['bin/console', 'ahoy', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-ansi', '--no-interaction']]; yield 'custom-aliased' => [['bin/console', 'ahoy'], ['Fabien', 'Robin', 'Wouter']]; } diff --git a/Tests/Completion/Output/BashCompletionOutputTest.php b/Tests/Completion/Output/BashCompletionOutputTest.php new file mode 100644 index 000000000..84ec56acc --- /dev/null +++ b/Tests/Completion/Output/BashCompletionOutputTest.php @@ -0,0 +1,33 @@ + + * + * 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\Output; + +use Symfony\Component\Console\Completion\Output\BashCompletionOutput; +use Symfony\Component\Console\Completion\Output\CompletionOutputInterface; + +class BashCompletionOutputTest extends CompletionOutputTestCase +{ + public function getCompletionOutput(): CompletionOutputInterface + { + return new BashCompletionOutput(); + } + + public function getExpectedOptionsOutput(): string + { + return "--option1\n--negatable\n--no-negatable".\PHP_EOL; + } + + public function getExpectedValuesOutput(): string + { + return "Green\nRed\nYellow".\PHP_EOL; + } +} diff --git a/Tests/Completion/Output/CompletionOutputTestCase.php b/Tests/Completion/Output/CompletionOutputTestCase.php new file mode 100644 index 000000000..c4551e5b6 --- /dev/null +++ b/Tests/Completion/Output/CompletionOutputTestCase.php @@ -0,0 +1,51 @@ + + * + * 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\Output; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Completion\CompletionSuggestions; +use Symfony\Component\Console\Completion\Output\CompletionOutputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\StreamOutput; + +abstract class CompletionOutputTestCase extends TestCase +{ + abstract public function getCompletionOutput(): CompletionOutputInterface; + + abstract public function getExpectedOptionsOutput(): string; + + abstract public function getExpectedValuesOutput(): string; + + public function testOptionsOutput() + { + $options = [ + new InputOption('option1', 'o', InputOption::VALUE_NONE), + new InputOption('negatable', null, InputOption::VALUE_NEGATABLE), + ]; + $suggestions = new CompletionSuggestions(); + $suggestions->suggestOptions($options); + $stream = fopen('php://memory', 'rw+'); + $this->getCompletionOutput()->write($suggestions, new StreamOutput($stream)); + fseek($stream, 0); + $this->assertEquals($this->getExpectedOptionsOutput(), stream_get_contents($stream)); + } + + public function testValuesOutput() + { + $suggestions = new CompletionSuggestions(); + $suggestions->suggestValues(['Green', 'Red', 'Yellow']); + $stream = fopen('php://memory', 'rw+'); + $this->getCompletionOutput()->write($suggestions, new StreamOutput($stream)); + fseek($stream, 0); + $this->assertEquals($this->getExpectedValuesOutput(), stream_get_contents($stream)); + } +}