Skip to content

Commit

Permalink
[Console] Fix inconsistent result for choice questions in non-interac…
Browse files Browse the repository at this point in the history
…tive mode
  • Loading branch information
Robin Chalas committed Apr 6, 2019
1 parent d45ecef commit 198b895
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Symfony/Component/Console/Helper/QuestionHelper.php
Expand Up @@ -49,7 +49,13 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
if (!$input->isInteractive()) {
$default = $question->getDefault();

if (null !== $default && $question instanceof ChoiceQuestion) {
if (null === $default) {
return $default;
}

if ($validator = $question->getValidator()) {
return \call_user_func($question->getValidator(), $default);
} elseif ($question instanceof ChoiceQuestion) {
$choices = $question->getChoices();

if (!$question->isMultiselect()) {
Expand Down
Expand Up @@ -137,6 +137,9 @@ public function testAskChoiceNonInteractive()
$question->setMultiselect(true);
$this->assertNull($questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));

$question = new ChoiceQuestion('Who are your favorite superheros?', ['a' => 'Batman', 'b' => 'Superman'], 'a');
$this->assertSame('a', $questionHelper->ask($this->createStreamableInputInterfaceMock('', false), $this->createOutputInterface(), $question), 'ChoiceQuestion validator returns the key if it\'s a string');

try {
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '');
$question->setMultiselect(true);
Expand Down

0 comments on commit 198b895

Please sign in to comment.