Skip to content

Commit

Permalink
bug #25521 [Console] fix a bug when you are passing a default value a…
Browse files Browse the repository at this point in the history
…nd passing -n would output the index (Simperfit)

This PR was merged into the 2.7 branch.

Discussion
----------

[Console] fix a bug when you are passing a default value and passing -n would output the index

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #25470
| License       | MIT
| Doc PR        |

We are fixing a bug when you are using a default value with -n, it would output the index instead of the value.

Simple reproducer Simperfit/symfony-reproducer@5079dd1?diff=split#diff-77efcc28bc5309e1af9ac07a1e073009R40

Commits
-------

41ffc69 [Console] fix a bug when you are passing a default value and passing -n would ouput the index
  • Loading branch information
nicolas-grekas committed Dec 18, 2017
2 parents 11bee7f + 41ffc69 commit 98bfe80
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Expand Up @@ -45,6 +45,12 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
}

if (!$input->isInteractive()) {
if ($question instanceof ChoiceQuestion) {
$choices = $question->getChoices();

return $choices[$question->getDefault()];
}

return $question->getDefault();
}

Expand Down
Expand Up @@ -84,6 +84,10 @@ public function testAskChoice()
$question->setMultiselect(true);

$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));

$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, 0);
// We are supposed to get the default value since we are not in interactive mode
$this->assertEquals('Superman', $questionHelper->ask($this->createInputInterfaceMock(true), $this->createOutputInterface(), $question));
}

public function testAsk()
Expand Down

0 comments on commit 98bfe80

Please sign in to comment.