[Console] Fix inconsistent result for choice questions in non-interactive mode

This commit is contained in:
Robin Chalas 2019-03-30 18:26:16 +01:00
parent d45ecefe0c
commit 198b895eac
2 changed files with 10 additions and 1 deletions

View File

@ -49,7 +49,13 @@ class QuestionHelper extends Helper
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()) {

View File

@ -137,6 +137,9 @@ class QuestionHelperTest extends AbstractQuestionHelperTest
$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);