Skip to content

Commit 338c78d

Browse files
author
Amrouche Hamza
committed
[Console] fix a bug when you are passing a default value and passing -n would ouput the index
1 parent 76b7cac commit 338c78d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
4444
$output = $output->getErrorOutput();
4545
}
4646

47-
if (!$input->isInteractive()) {
47+
$isNotInteractive = !$input->isInteractive();
48+
49+
if ($isNotInteractive && $question instanceof ChoiceQuestion) {
50+
return $question->getChoices()[$question->getDefault()];
51+
}
52+
53+
if ($isNotInteractive) {
4854
return $question->getDefault();
4955
}
5056

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public function testAskChoice()
8484
$question->setMultiselect(true);
8585

8686
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
87+
88+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, 0);
89+
// We are supposed to get the default value since we are in interactive mode
90+
$this->assertEquals('Superman', $questionHelper->ask($this->createInputInterfaceMock(true), $this->createOutputInterface(), $question));
8791
}
8892

8993
public function testAsk()

0 commit comments

Comments
 (0)