[Console] Fixes multiselect choice question in interactive mode with default values
This commit is contained in:
parent
5e6ea4e146
commit
099e265711
@ -46,13 +46,23 @@ class QuestionHelper extends Helper
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$input->isInteractive()) {
|
if (!$input->isInteractive()) {
|
||||||
if ($question instanceof ChoiceQuestion) {
|
$default = $question->getDefault();
|
||||||
|
|
||||||
|
if (null !== $default && $question instanceof ChoiceQuestion) {
|
||||||
$choices = $question->getChoices();
|
$choices = $question->getChoices();
|
||||||
|
|
||||||
return $choices[$question->getDefault()];
|
if (!$question->isMultiselect()) {
|
||||||
|
return isset($choices[$default]) ? $choices[$default] : $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $question->getDefault();
|
$default = explode(',', $default);
|
||||||
|
foreach ($default as $k => $v) {
|
||||||
|
$v = trim($v);
|
||||||
|
$default[$k] = isset($choices[$v]) ? $choices[$v] : $v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$question->getValidator()) {
|
if (!$question->getValidator()) {
|
||||||
|
@ -90,6 +90,63 @@ class QuestionHelperTest extends TestCase
|
|||||||
$this->assertEquals('Superman', $questionHelper->ask($this->createInputInterfaceMock(true), $this->createOutputInterface(), $question));
|
$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()
|
public function testAsk()
|
||||||
{
|
{
|
||||||
$dialog = new QuestionHelper();
|
$dialog = new QuestionHelper();
|
||||||
|
Reference in New Issue
Block a user