Skip to content

Commit 1e31ab9

Browse files
veeweechalasr
authored andcommitted
[Console] Fixes multiselect choice question in interactive mode with default values
1 parent 72dc58e commit 1e31ab9

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

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()) {

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)