[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
This commit is contained in:
parent
ba719d18dd
commit
03e4ab609b
@ -149,19 +149,19 @@ class ChoiceQuestion extends Question
|
|||||||
$result = array_search($value, $choices);
|
$result = array_search($value, $choices);
|
||||||
|
|
||||||
if (!$isAssoc) {
|
if (!$isAssoc) {
|
||||||
if (!empty($result)) {
|
if (false !== $result) {
|
||||||
$result = $choices[$result];
|
$result = $choices[$result];
|
||||||
} elseif (isset($choices[$value])) {
|
} elseif (isset($choices[$value])) {
|
||||||
$result = $choices[$value];
|
$result = $choices[$value];
|
||||||
}
|
}
|
||||||
} elseif (empty($result) && array_key_exists($value, $choices)) {
|
} elseif (false === $result && isset($choices[$value])) {
|
||||||
$result = $value;
|
$result = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($result)) {
|
if (false === $result) {
|
||||||
throw new \InvalidArgumentException(sprintf($errorMessage, $value));
|
throw new \InvalidArgumentException(sprintf($errorMessage, $value));
|
||||||
}
|
}
|
||||||
array_push($multiselectChoices, $result);
|
array_push($multiselectChoices, (string) $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($multiselect) {
|
if ($multiselect) {
|
||||||
|
@ -210,6 +210,76 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
* @dataProvider answerProvider
|
||||||
*/
|
*/
|
||||||
@ -227,6 +297,7 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$dialog->setHelperSet($helperSet);
|
$dialog->setHelperSet($helperSet);
|
||||||
|
|
||||||
$question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
|
$question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
|
||||||
|
$question->setMaxAttempts(1);
|
||||||
$answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
|
$answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
|
||||||
|
|
||||||
$this->assertSame($expectedValue, $answer);
|
$this->assertSame($expectedValue, $answer);
|
||||||
|
Reference in New Issue
Block a user