Skip to content

[Console] Fixes multiselect choice question defaults in non-interactive mode #27772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,23 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
}

if (!$input->isInteractive()) {
if ($question instanceof ChoiceQuestion) {
$default = $question->getDefault();

if (null !== $default && $question instanceof ChoiceQuestion) {
$choices = $question->getChoices();

return $choices[$question->getDefault()];
if (!$question->isMultiselect()) {
return isset($choices[$default]) ? $choices[$default] : $default;
}

$default = explode(',', $default);
foreach ($default as $k => $v) {
$v = trim($v);
$default[$k] = isset($choices[$v]) ? $choices[$v] : $v;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return $question->getDefault();
return $default;
}

if (!$question->getValidator()) {
Expand Down
57 changes: 57 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,63 @@ public function testAskChoice()
$this->assertEquals('Superman', $questionHelper->ask($this->createInputInterfaceMock(true), $this->createOutputInterface(), $question));
}

public function testAskChoiceNonInteractive()
{
$questionHelper = new QuestionHelper();

$helperSet = new HelperSet(array(new FormatterHelper()));
$questionHelper->setHelperSet($helperSet);
$questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));

$heroes = array('Superman', 'Batman', 'Spiderman');

$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');

$this->assertSame('Superman', $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));

$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, 'Batman');
$this->assertSame('Batman', $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));

$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
$this->assertNull($questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));

$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');
$question->setValidator(null);
$this->assertSame('Superman', $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));

try {
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
$questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question);
} catch (\InvalidArgumentException $e) {
$this->assertSame('Value "" is invalid', $e->getMessage());
}

$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
$question->setMultiselect(true);
$this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));

$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
$question->setMultiselect(true);
$question->setValidator(null);
$this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));

$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, Batman');
$question->setMultiselect(true);
$this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));

$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, null);
$question->setMultiselect(true);
$this->assertNull($questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));

try {
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '');
$question->setMultiselect(true);
$questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question);
} catch (\InvalidArgumentException $e) {
$this->assertSame('Value "" is invalid', $e->getMessage());
}
}

public function testAsk()
{
$dialog = new QuestionHelper();
Expand Down