From 9c3247cfd4f82294df7eccbb87574d11009633f6 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Wed, 9 Mar 2016 16:17:23 +0000 Subject: [PATCH] [Console] Fix an autocompletion question helper issue with non-sequentially indexed choices --- .../Component/Console/Question/Question.php | 4 ++-- .../Tests/Helper/QuestionHelperTest.php | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Question/Question.php b/src/Symfony/Component/Console/Question/Question.php index 02b190caed..1eb3af663d 100644 --- a/src/Symfony/Component/Console/Question/Question.php +++ b/src/Symfony/Component/Console/Question/Question.php @@ -135,8 +135,8 @@ class Question */ public function setAutocompleterValues($values) { - if (is_array($values) && $this->isAssoc($values)) { - $values = array_merge(array_keys($values), array_values($values)); + if (is_array($values)) { + $values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values); } if (null !== $values && !is_array($values)) { diff --git a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php index f0621006eb..6a4f8aceae 100644 --- a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php @@ -135,6 +135,26 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase $this->assertEquals('FooBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question)); } + public function testAskWithAutocompleteWithNonSequentialKeys() + { + if (!$this->hasSttyAvailable()) { + $this->markTestSkipped('`stty` is required to test autocomplete functionality'); + } + + // + $inputStream = $this->getInputStream("\033[A\033[A\n\033[B\033[B\n"); + + $dialog = new QuestionHelper(); + $dialog->setInputStream($inputStream); + $dialog->setHelperSet(new HelperSet(array(new FormatterHelper()))); + + $question = new ChoiceQuestion('Please select a bundle', array(1 => 'AcmeDemoBundle', 4 => 'AsseticBundle')); + $question->setMaxAttempts(1); + + $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question)); + $this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question)); + } + public function testAskHiddenResponse() { if ('\\' === DIRECTORY_SEPARATOR) {