8000 [Console] Fix an autocompletion question helper issue with non-sequen… · symfony/symfony@5bc2deb · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bc2deb

Browse files
committed
[Console] Fix an autocompletion question helper issue with non-sequentially indexed choices
1 parent a1c95f7 commit 5bc2deb

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/Symfony/Component/Console/Question/Question.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ public function getAutocompleterValues()
135135
*/
136136
public function setAutocompleterValues($values)
137137
{
138-
if (is_array($values) && $this->isAssoc($values)) {
139-
$values = array_merge(array_keys($values), array_values($values));
138+
if (is_array($values)) {
139+
$values = array_values($values);
140+
if ($this->isAssoc($values)) {
141+
$values = array_merge(array_keys($values), $values);
142+
}
140143
}
141144

142145
if (null !== $values && !is_array($values)) {

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ public function testAskWithAutocomplete()
135135
$this->assertEquals('FooBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
136136
}
137137

138+
public function testAskWithAutocompleteWithAssocArray()
139+
{
140+
if (!$this->hasSttyAvailable()) {
141+
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
142+
}
143+
144+
// <UP ARROW><UP ARROW><NEWLINE><DOWN ARROW><DOWN ARROW><NEWLINE>
145+
$inputStream = $this->getInputStream("\033[A\033[A\n\033[B\033[B\n");
146+
147+
$dialog = new QuestionHelper();
148+
$dialog->setInputStream($inputStream);
149+
$dialog->setHelperSet(new HelperSet(array(new FormatterHelper())));
150+
151+
$question = new ChoiceQuestion('Please select a bundle', array(1 => 'AcmeDemoBundle', 4 => 'AsseticBundle'));
152+
$question->setMaxAttempts(1);
153+
154+
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
155+
$this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
156+
}
157+
138158
public function testAskHiddenResponse()
139159
{
140160
if ('\\' === DIRECTORY_SEPARATOR) {

0 commit comments

Comments
 (0)
0