bug #30878 [Console] Fix inconsistent result for choice questions in non-interactive mode (chalasr)

This PR was merged into the 3.4 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  |no
| BC breaks?    | no
| Deprecations? | n/a
| Tests pass?   | yes
| Fixed tickets | #30774
| License       | MIT
| Doc PR        | n/a

Commits
-------

198b895eac [Console] Fix inconsistent result for choice questions in non-interactive mode
This commit is contained in:
Fabien Potencier 2019-04-06 11:16:33 +02:00
commit 0c8e8a50b0
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);