Skip to content

Commit ab1bb3a

Browse files
author
Robin Chalas
committed
[Console] Fix inconsistent result for choice questions in non-interactive mode
1 parent 98ae3cd commit ab1bb3a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Helper/QuestionHelper.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
4949
if (!$input->isInteractive()) {
5050
$default = $question->getDefault();
5151

52-
if (null !== $default && $question instanceof ChoiceQuestion) {
52+
if (null === $default) {
53+
return $default;
54+
}
55+
56+
if ($validator = $question->getValidator()) {
57+
return \call_user_func($question->getValidator(), $default);
58+
} elseif ($question instanceof ChoiceQuestion) {
5359
$choices = $question->getChoices();
5460

5561
if (!$question->isMultiselect()) {

Tests/Helper/QuestionHelperTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public function testAskChoiceNonInteractive()
137137
$question->setMultiselect(true);
138138
$this->assertNull($questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
139139

140+
$question = new ChoiceQuestion('Who are your favorite superheros?', ['a' => 'Batman', 'b' => 'Superman'], 'a');
141+
$this->assertSame('a', $questionHelper->ask($this->createStreamableInputInterfaceMock('', false), $this->createOutputInterface(), $question), 'ChoiceQuestion validator returns the key if it\'s a string');
142+
140143
try {
141144
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '');
142145
$question->setMultiselect(true);

0 commit comments

Comments
 (0)