diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index 35974b06ae..8c219f1372 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -46,13 +46,23 @@ class QuestionHelper extends Helper } 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; + } } - return $question->getDefault(); + return $default; } if (!$question->getValidator()) { diff --git a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php index 7a9afb0a8f..a7bba87517 100644 --- a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php @@ -90,6 +90,63 @@ class QuestionHelperTest extends TestCase $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();