From 03e4ab609b1908ee5db5ecebc900ae9c089a1cba Mon Sep 17 00:00:00 2001 From: ogizanagi Date: Thu, 14 May 2015 16:13:05 +0200 Subject: [PATCH] [Console] Fix first choice was invalid when using value Using question helper and choices questions: - first choice was not selectable by value. - ChoiceList with associative choices with mixed string and int keys has same issue with first choice. Add test for all choices. - Fix inconsistency by always returning values as strings --- .../Console/Question/ChoiceQuestion.php | 8 +-- .../Tests/Helper/QuestionHelperTest.php | 71 +++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Question/ChoiceQuestion.php b/src/Symfony/Component/Console/Question/ChoiceQuestion.php index ce4ff67779e7e..9586c5bf3ac02 100644 --- a/src/Symfony/Component/Console/Question/ChoiceQuestion.php +++ b/src/Symfony/Component/Console/Question/ChoiceQuestion.php @@ -149,19 +149,19 @@ private function getDefaultValidator() $result = array_search($value, $choices); if (!$isAssoc) { - if (!empty($result)) { + if (false !== $result) { $result = $choices[$result]; } elseif (isset($choices[$value])) { $result = $choices[$value]; } - } elseif (empty($result) && array_key_exists($value, $choices)) { + } elseif (false === $result && isset($choices[$value])) { $result = $value; } - if (empty($result)) { + if (false === $result) { throw new \InvalidArgumentException(sprintf($errorMessage, $value)); } - array_push($multiselectChoices, $result); + array_push($multiselectChoices, (string) $result); } if ($multiselect) { diff --git a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php index ccd889b0aa24c..be872a9d52c1c 100644 --- a/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php @@ -210,6 +210,76 @@ public function testAskAndValidate() } } + /** + * @dataProvider simpleAnswerProvider + */ + public function testSelectChoiceFromSimpleChoices($providedAnswer, $expectedValue) + { + $possibleChoices = array( + 'My environment 1', + 'My environment 2', + 'My environment 3', + ); + + $dialog = new QuestionHelper(); + $dialog->setInputStream($this->getInputStream($providedAnswer."\n")); + $helperSet = new HelperSet(array(new FormatterHelper())); + $dialog->setHelperSet($helperSet); + + $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices); + $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question); + + $this->assertSame($expectedValue, $answer); + } + + public function simpleAnswerProvider() + { + return array( + array(0, 'My environment 1'), + array(1, 'My environment 2'), + array(2, 'My environment 3'), + array('My environment 1', 'My environment 1'), + array('My environment 2', 'My environment 2'), + array('My environment 3', 'My environment 3'), + ); + } + + /** + * @dataProvider mixedKeysChoiceListAnswerProvider + */ + public function testChoiceFromChoicelistWithMixedKeys($providedAnswer, $expectedValue) + { + $possibleChoices = array( + '0' => 'No environment', + '1' => 'My environment 1', + 'env_2' => 'My environment 2', + 3 => 'My environment 3', + ); + + $dialog = new QuestionHelper(); + $dialog->setInputStream($this->getInputStream($providedAnswer."\n")); + $helperSet = new HelperSet(array(new FormatterHelper())); + $dialog->setHelperSet($helperSet); + + $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices); + $question->setMaxAttempts(1); + $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question); + + $this->assertSame($expectedValue, $answer); + } + + public function mixedKeysChoiceListAnswerProvider() + { + return array( + array('0', '0'), + array('No environment', '0'), + array('1', '1'), + array('env_2', 'env_2'), + array(3, '3'), + array('My environment 1', '1'), + ); + } + /** * @dataProvider answerProvider */ @@ -227,6 +297,7 @@ public function testSelectChoiceFromChoiceList($providedAnswer, $expectedValue) $dialog->setHelperSet($helperSet); $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices); + $question->setMaxAttempts(1); $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question); $this->assertSame($expectedValue, $answer); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy