8000 bug #27772 [Console] Fixes multiselect choice question defaults in no… · symfony/symfony@bad4867 · GitHub
[go: up one dir, main page]

Skip to content

Commit bad4867

Browse files
committed
bug #27772 [Console] Fixes multiselect choice question defaults in non-interactive mode (veewee)
This PR was merged into the 2.8 branch. Discussion ---------- [Console] Fixes multiselect choice question defaults in non-interactive mode | Q | A | ------------- | --- | Branch? | >4.1.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | [This commit](41ffc69) introduced a warning in multiselect mode: ``` PHP Notice: Undefined index: in /tmp/vendor/symfony/console/Helper/QuestionHelper.php on line 52 PHP Stack trace: PHP 1. {main}() /tmp/vendor/phpro/grumphp/bin/grumphp:0 PHP 2. GrumPHP\Console\Application->run() /tmp/vendor/phpro/grumphp/bin/grumphp:31 PHP 3. GrumPHP\Console\Application->run() /tmp/vendor/phpro/grumphp/src/Console/Application.php:240 PHP 4. GrumPHP\Console\Application->doRun() /tmp/vendor/symfony/console/Application.php:145 PHP 5. GrumPHP\Console\Application->doRunCommand() /tmp/vendor/symfony/console/Application.php:262 PHP 6. GrumPHP\Console\Command\ConfigureCommand->run() /tmp/vendor/symfony/console/Application.php:904 PHP 7. GrumPHP\Console\Command\ConfigureCommand->execute() /tmp/vendor/symfony/console/Command/Command.php:251 PHP 8. GrumPHP\Console\Command\ConfigureCommand->buildConfiguration() /tmp/vendor/phpro/grumphp/src/Console/Command/ConfigureCommand.php:95 PHP 9. Symfony\Component\Console\Helper\QuestionHelper->ask() /tmp/vendor/phpro/grumphp/src/Console/Command/ConfigureCommand.php:156 Notice: Undefined index: in /tmp/vendor/symfony/console/Helper/QuestionHelper.php on line 52 ``` This PR fixes this issue by parsing the default value using the built-in validator if available. (which most likely is ...) Commits ------- 099e265 [Console] Fixes multiselect choice question in interactive mode with default values
2 parents 1a1a026 + 099e265 commit bad4867

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,23 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
4646
}
4747

4848
if (!$input->isInteractive()) {
49-
if ($question instanceof ChoiceQuestion) {
49+
$default = $question->getDefault();
50+
51+
if (null !== $default && $question instanceof ChoiceQuestion) {
5052
$choices = $question->getChoices();
5153

52-
return $choices[$question->getDefault()];
54+
if (!$question->isMultiselect()) {
55+
return isset($choices[$default]) ? $choices[$default] : $default;
56+
}
57+
58+
$default = explode(',', $default);
59+
foreach ($default as $k => $v) {
60+
$v = trim($v);
61+
$default[$k] = isset($choices[$v]) ? $choices[$v] : $v;
62+
}
5363
}
5464

55-
return $question->getDefault();
65+
return $default;
5666
}
5767

5868
if (!$question->getValidator()) {

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,63 @@ public function testAskChoice()
9090
$this->assertEquals('Superman', $questionHelper->ask($this->createInputInterfaceMock(true), $this->createOutputInterface(), $question));
9191
}
9292

93+
public function testAskChoiceNonInteractive()
94+
{
95+
$questionHelper = new QuestionHelper();
96+
97+
$helperSet = new HelperSet(array(new FormatterHelper()));
98+
$questionHelper->setHelperSet($helperSet);
99+
$questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
100+
101+
$heroes = array('Superman', 'Batman', 'Spiderman');
102+
103+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');
104+
105+
$this->assertSame('Superman', $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
106+
107+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, 'Batman');
108+
$this->assertSame('Batman', $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
109+
110+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
111+
$this->assertNull($questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
112+
113+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');
114+
$question->setValidator(null);
115+
$this->assertSame('Superman', $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
116+
117+
try {
118+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
119+
$questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question);
120+
} catch (\InvalidArgumentException $e) {
121+
$this->assertSame('Value "" is invalid', $e->getMessage());
122+
}
123+
124+
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
125+
$question->setMultiselect(true);
126+
$this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
127+
128+
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
129+
$question->setMultiselect(true);
130+
$question->setValidator(null);
131+
$this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
132+
133+
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, Batman');
134+
$question->setMultiselect(true);
135+
$this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
136+
137+
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, null);
138+
$question->setMultiselect(true);
139+
$this->assertNull($questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
140+
141+
try {
142+
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '');
143+
$question->setMultiselect(true);
144+
$questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question);
145+
} catch (\InvalidArgumentException $e) {
146+
$this->assertSame('Value "" is invalid', $e->getMessage());
147+
}
148+
}
149+
93150
public function testAsk()
94151
{
95152
$dialog = new QuestionHelper();

0 commit comments

Comments
 (0)
0