bug #32781 [Console] Fix disabling ChoiceQuestion answer trimming (chalasr)

This PR was merged into the 4.4 branch.

Discussion
----------

[Console] Fix disabling ChoiceQuestion answer trimming

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

44ee0569bb [Console] Fix disabling ChoiceQuestion answer trimming
This commit is contained in:
Fabien Potencier 2019-07-27 19:07:55 +02:00
commit a29aff0370
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'));
}
}