diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index 17295adb3aec4..0b9711d29902d 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -308,6 +308,12 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu continue; } else { + for ($b = 0; $b < 4; $b++) { + if (false === mb_ord($c)) { + $c .= fread($inputStream, 1); + } + } + $output->write($c); $ret .= $c; ++$i; diff --git a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php index 805b1311f572a..46cfe8184278d 100644 --- a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php @@ -237,6 +237,43 @@ public function testAskWithAutocompleteWithExactMatch() $this->assertSame('b', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question)); } + public function getInputs() + { + return [ + ['$'], // 1 byte character + ['¢'], // 2 bytes character + ['€'], // 3 bytes character + ['𐍈'], // 4 bytes character + ]; + } + + /** + * @dataProvider getInputs + */ + public function testAskWithAutocompleteWithMultiByteCharacter($character) + { + if (!$this->hasSttyAvailable()) { + $this->markTestSkipped('`stty` is required to test autocomplete functionality'); + } + + $inputStream = $this->getInputStream("$character\n"); + + $possibleChoices = [ + '$' => '1 byte character', + '¢' => '2 bytes character', + '€' => '3 bytes character', + '𐍈' => '4 bytes character', + ]; + + $dialog = new QuestionHelper(); + $dialog->setHelperSet(new HelperSet([new FormatterHelper()])); + + $question = new ChoiceQuestion('Please select a character', $possibleChoices); + $question->setMaxAttempts(1); + + $this->assertSame($character, $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question)); + } + public function testAutocompleteWithTrailingBackslash() { if (!$this->hasSttyAvailable()) {