[Console] Fix disabling ChoiceQuestion answer trimming

This commit is contained in:
Robin Chalas 2019-07-27 14:52:22 +02:00
parent 119ac78792
commit 44ee0569bb
2 changed files with 24 additions and 2 deletions

View File

@ -135,9 +135,15 @@ class ChoiceQuestion extends Question
throw new InvalidArgumentException(sprintf($errorMessage, $selected));
}
$selectedChoices = array_map('trim', explode(',', $selected));
$selectedChoices = explode(',', $selected);
} else {
$selectedChoices = [trim($selected)];
$selectedChoices = [$selected];
}
if ($this->isTrimmable()) {
foreach ($selectedChoices as $k => $v) {
$selectedChoices[$k] = trim($v);
}
}
$multiselectChoices = [];

View File

@ -61,4 +61,20 @@ class ChoiceQuestionTest extends TestCase
],
];
}
public function testNonTrimmable()
{
$question = new ChoiceQuestion('A question', [
'First response ',
' Second response',
' Third response ',
]);
$question->setTrimmable(false);
$this->assertSame(' Third response ', $question->getValidator()(' Third response '));
$question->setMultiselect(true);
$this->assertSame(['First response ', ' Second response'], $question->getValidator()('First response , Second response'));
}
}