From 0a762e2b722e02b15d29d9902d59513e40cec00e Mon Sep 17 00:00:00 2001 From: battye Date: Sat, 4 May 2019 03:42:05 +0000 Subject: [PATCH] [Console] Add another test and some tweaks --- .../Console/Helper/QuestionHelper.php | 54 +++++++++---------- .../Tests/Helper/QuestionHelperTest.php | 9 ++-- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index fb5de4661085f..03f5a850abf38 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -303,10 +303,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu if ($numMatches > 0 && -1 !== $ofs) { $ret = $matches[$ofs]; // Echo out remaining chars for current match - $remainingCharacters = substr($ret, strlen(trim($this->mostRecentlyEnteredValue($fullChoice)))); + $remainingCharacters = substr($ret, \strlen(trim($this->mostRecentlyEnteredValue($fullChoice)))); $output->write($remainingCharacters); $fullChoice .= $remainingCharacters; - $i = strlen($fullChoice); + $i = \strlen($fullChoice); } if ("\n" === $c) { @@ -330,9 +330,8 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu $tempRet = $ret; - if ($question instanceof ChoiceQuestion && $question->isMultiselect()) - { - $tempRet = $this->mostRecentlyEnteredValue($ret); + if ($question instanceof ChoiceQuestion && $question->isMultiselect()) { + $tempRet = $this->mostRecentlyEnteredValue($fullChoice); } $numMatches = 0; @@ -353,7 +352,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu // Save cursor position $output->write("\0337"); // Write highlighted text, complete the partially entered response - $charactersEntered = strlen(trim($this->mostRecentlyEnteredValue($ret))); + $charactersEntered = \strlen(trim($this->mostRecentlyEnteredValue($fullChoice))); $output->write(''.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $charactersEntered)).''); // Restore cursor position $output->write("\0338"); @@ -366,27 +365,28 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu return $fullChoice; } - /** - * Determine the most recent value the user entered - * @param $ret - * @return string - */ - private function mostRecentlyEnteredValue($ret) - { - $tempRet = $ret; - - if (strpos($ret, ',') !== false) - { - $choices = explode(',', $ret); - $lastChoice = trim($choices[count($choices) - 1]); - if (strlen($lastChoice) > 0) - { - $tempRet = $lastChoice; - } - } - - return $tempRet; - } + /** + * Determine the most recent value the user entered. + * + * @param $entered + * + * @return string + */ + private function mostRecentlyEnteredValue($entered) + { + $tempEntered = $entered; + + if (false !== strpos($entered, ',')) { + $choices = explode(',', $entered); + $lastChoice = trim($choices[\count($choices) - 1]); + + if (\strlen($lastChoice) > 0) { + $tempEntered = $lastChoice; + } + } + + return $tempEntered; + } /** * Gets a hidden response from user. diff --git a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php index 0c25ac30bc14f..3c030e04983d9 100644 --- a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php @@ -1020,15 +1020,13 @@ public function testTraversableAutocomplete() 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"); + // Acme,As<29x BACKSPACE>S + // Ac,As<3x BACKSPACE>d + $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\nAc\t,As\t\177\177\177d\t\n"); $dialog = new QuestionHelper(); $helperSet = new HelperSet([new FormatterHelper()]); @@ -1048,6 +1046,7 @@ public function testTraversableMultiselectAutocomplete() $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)); + $this->assertEquals(['AcmeDemoBundle', 'AsseticBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question)); } protected function getInputStream($input)