From e7b6ae849cde638a0553e6ddf241208791feb9bd Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Mon, 22 May 2017 10:45:15 +0200 Subject: [PATCH 1/2] [Console] Derive choice from default --- .../Component/Console/Helper/QuestionHelper.php | 7 +++++++ .../Console/Tests/Helper/QuestionHelperTest.php | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index 676c3c045114f..f37d97e9c0bbb 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -163,6 +163,13 @@ protected function writePrompt(OutputInterface $output, Question $question) $message = $question->getQuestion(); if ($question instanceof ChoiceQuestion) { + if (!$question->getChoices()) { + $output->writeln($question->getQuestion()); + $output->write($question->getPrompt()); + + return; + } + $maxWidth = max(array_map(array($this, 'strlen'), array_keys($question->getChoices()))); $messages = (array) $question->getQuestion(); diff --git a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php index 49ba0ee06c79a..2d70d3ca00930 100644 --- a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php @@ -434,6 +434,21 @@ public function testAskThrowsExceptionOnMissingInputWithValidator() $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Value "irrelevant" is invalid + */ + public function testEmptyChoices() + { + $dialog = new QuestionHelper(); + $dialog->setInputStream($this->getInputStream("irrelevant\n")); + + $question = new ChoiceQuestion('Question', array(), 'irrelevant'); + $question->setMaxAttempts(1); + + $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question); + } + protected function getInputStream($input) { $stream = fopen('php://memory', 'r+', false); From ec43c0905a2afbdf9961feca6098070ef3d64d81 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Mon, 22 May 2017 16:35:32 +0200 Subject: [PATCH 2/2] different approach --- .../Component/Console/Helper/QuestionHelper.php | 7 ------- .../Component/Console/Question/ChoiceQuestion.php | 4 ++++ .../Console/Tests/Helper/QuestionHelperTest.php | 12 +++--------- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index f37d97e9c0bbb..676c3c045114f 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -163,13 +163,6 @@ protected function writePrompt(OutputInterface $output, Question $question) $message = $question->getQuestion(); if ($question instanceof ChoiceQuestion) { - if (!$question->getChoices()) { - $output->writeln($question->getQuestion()); - $output->write($question->getPrompt()); - - return; - } - $maxWidth = max(array_map(array($this, 'strlen'), array_keys($question->getChoices()))); $messages = (array) $question->getQuestion(); diff --git a/src/Symfony/Component/Console/Question/ChoiceQuestion.php b/src/Symfony/Component/Console/Question/ChoiceQuestion.php index e5b6ff4ad7217..f07ace4b161ec 100644 --- a/src/Symfony/Component/Console/Question/ChoiceQuestion.php +++ b/src/Symfony/Component/Console/Question/ChoiceQuestion.php @@ -32,6 +32,10 @@ class ChoiceQuestion extends Question */ public function __construct($question, array $choices, $default = null) { + if (!$choices) { + throw new \LogicException('Choice question must have at least 1 choice available.'); + } + parent::__construct($question, $default); $this->choices = $choices; diff --git a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php index 2d70d3ca00930..2309965deeff8 100644 --- a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php @@ -435,18 +435,12 @@ public function testAskThrowsExceptionOnMissingInputWithValidator() } /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Value "irrelevant" is invalid + * @expectedException \LogicException + * @expectedExceptionMessage Choice question must have at least 1 choice available. */ public function testEmptyChoices() { - $dialog = new QuestionHelper(); - $dialog->setInputStream($this->getInputStream("irrelevant\n")); - - $question = new ChoiceQuestion('Question', array(), 'irrelevant'); - $question->setMaxAttempts(1); - - $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question); + new ChoiceQuestion('Question', array(), 'irrelevant'); } protected function getInputStream($input)