Skip to content

Commit

Permalink
Complete negatable options
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed May 18, 2022
1 parent 1894919 commit 829d5d1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Completion/Output/BashCompletionOutput.php
Expand Up @@ -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));
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Command/CompleteCommandTest.php
Expand Up @@ -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']];
}

Expand Down
33 changes: 33 additions & 0 deletions Tests/Completion/Output/BashCompletionOutputTest.php
@@ -0,0 +1,33 @@
<?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\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;
}
}
51 changes: 51 additions & 0 deletions Tests/Completion/Output/CompletionOutputTestCase.php
@@ -0,0 +1,51 @@
<?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\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));
}
}

0 comments on commit 829d5d1

Please sign in to comment.