Skip to content

Commit

Permalink
bug #30878 [Console] Fix inconsistent result for choice questions in …
Browse files Browse the repository at this point in the history
…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
-------

198b895 [Console] Fix inconsistent result for choice questions in non-interactive mode
  • Loading branch information
fabpot committed Apr 6, 2019
2 parents d45ecef + 198b895 commit 0c8e8a5
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 0c8e8a5

Please sign in to comment.