From 44ee0569bbc465ac8c80536c54919ceaf3bdcd75 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Sat, 27 Jul 2019 14:52:22 +0200 Subject: [PATCH] [Console] Fix disabling ChoiceQuestion answer trimming --- .../Console/Question/ChoiceQuestion.php | 10 ++++++++-- .../Tests/Question/ChoiceQuestionTest.php | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Question/ChoiceQuestion.php b/src/Symfony/Component/Console/Question/ChoiceQuestion.php index 44b70abf11..a4b302db3e 100644 --- a/src/Symfony/Component/Console/Question/ChoiceQuestion.php +++ b/src/Symfony/Component/Console/Question/ChoiceQuestion.php @@ -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 = []; diff --git a/src/Symfony/Component/Console/Tests/Question/ChoiceQuestionTest.php b/src/Symfony/Component/Console/Tests/Question/ChoiceQuestionTest.php index 5ec7a9cacb..9db12f8528 100644 --- a/src/Symfony/Component/Console/Tests/Question/ChoiceQuestionTest.php +++ b/src/Symfony/Component/Console/Tests/Question/ChoiceQuestionTest.php @@ -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')); + } }