Skip to content

Commit

Permalink
[Console] Add more autocomplete tests
Browse files Browse the repository at this point in the history
  • Loading branch information
battye committed May 3, 2019
1 parent 68648b2 commit 9fbfe1c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/QuestionHelper.php
Expand Up @@ -306,7 +306,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
$remainingCharacters = substr($ret, strlen(trim($this->mostRecentlyEnteredValue($fullChoice))));
$output->write($remainingCharacters);
$fullChoice .= $remainingCharacters;
$i = \strlen($ret);
$i = strlen($fullChoice);
}

if ("\n" === $c) {
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Expand Up @@ -1018,6 +1018,38 @@ public function testTraversableAutocomplete()
$this->assertEquals('FooBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
}

public function testTraversableMultiselectAutocomplete()
{
// Test cases:
// 1) default; 2) Tab single; 3) Traverse and tab multiple; 4) Backspace and traverse multiple; 5) Backspace all

// <NEWLINE>
// F<TAB><NEWLINE>
// A<3x UP ARROW><TAB>,F<TAB><NEWLINE>
// F00<BACKSPACE><BACKSPACE>o<TAB>,A<DOWN ARROW>,<SPACE>SecurityBundle<NEWLINE>
// Acme<TAB> ,<SPACE>As<TAB><29x BACKSPACE>S<TAB><NEWLINE>
$inputStream = $this->getInputStream("\nF\t\nA\033[A\033[A\033[A\t,F\t\nF00\177\177o\t,A\033[B\t, SecurityBundle\nAcme\t, As\t\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177S\t\n");

$dialog = new QuestionHelper();
$helperSet = new HelperSet([new FormatterHelper()]);
$dialog->setHelperSet($helperSet);

$question = new ChoiceQuestion(
'Please select a bundle (defaults to AcmeDemoBundle and AsseticBundle)',
['AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle'],
'0,1'
);

// This tests that autocomplete works for all multiselect choices entered by the user
$question->setMultiselect(true);

$this->assertEquals(['AcmeDemoBundle', 'AsseticBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
$this->assertEquals(['FooBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
$this->assertEquals(['AsseticBundle', 'FooBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
$this->assertEquals(['FooBundle', 'AsseticBundle', 'SecurityBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
$this->assertEquals(['SecurityBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
}

protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
Expand Down

0 comments on commit 9fbfe1c

Please sign in to comment.