Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c51dda0

Browse files
author
Robin Chalas
committed
[Console] Fix SymfonyQuestionHelper::askQuestion() with choice value as default
1 parent 40fff43 commit c51dda0

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function writePrompt(OutputInterface $output, Question $question)
8282

8383
case $question instanceof ChoiceQuestion:
8484
$choices = $question->getChoices();
85-
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default]));
85+
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(isset($choices[$default]) ? $choices[$default] : $default));
8686

8787
break;
8888

src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ public function testAskChoice()
7575
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
7676
}
7777

78+
public function testAskChoiceWithChoiceValueAsDefault()
79+
{
80+
$questionHelper = new SymfonyQuestionHelper();
81+
$helperSet = new HelperSet(array(new FormatterHelper()));
82+
$questionHelper->setHelperSet($helperSet);
83+
$questionHelper->setInputStream($this->getInputStream("Batman\n"));
84+
$question = new ChoiceQuestion('What is your favorite superhero?', array('Superman', 'Batman', 'Spiderman'), 'Batman');
85+
$question->setMaxAttempts(1);
86+
87+
$this->assertSame('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
88+
$this->assertOutputContains('What is your favorite superhero? [Batman]', $output);
89+
}
90+
7891
public function testAskReturnsNullIfValidatorAllowsIt()
7992
{
8093
$questionHelper = new SymfonyQuestionHelper();

0 commit comments

Comments
 (0)