From 9fbfe1cd2cd04e660de6e93d95e0ad609700f2a0 Mon Sep 17 00:00:00 2001 From: battye Date: Fri, 3 May 2019 17:21:48 +0000 Subject: [PATCH] [Console] Add more autocomplete tests --- .../Console/Helper/QuestionHelper.php | 2 +- .../Tests/Helper/QuestionHelperTest.php | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index 8cba5e195ea43..fb5de4661085f 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -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) { diff --git a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php index 56ba1c6891322..0c25ac30bc14f 100644 --- a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php @@ -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 + + // + // F + // A<3x UP ARROW>,F + // F00o,A,SecurityBundle + // Acme ,As<29x BACKSPACE>S + $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);