Skip to content

Commit 9104ef1

Browse files
IceMaDRobin Chalas
authored and
Robin Chalas
committed
Fix multiSelect ChoiceQuestion when answers have spaces
1 parent 639041c commit 9104ef1

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

src/Symfony/Component/Console/Question/ChoiceQuestion.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,15 @@ private function getDefaultValidator()
134134
$isAssoc = $this->isAssoc($choices);
135135

136136
return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
137-
// Collapse all spaces.
138-
$selectedChoices = str_replace(' ', '', $selected);
139-
140137
if ($multiselect) {
141138
// Check for a separated comma values
142-
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) {
139+
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selected, $matches)) {
143140
throw new InvalidArgumentException(sprintf($errorMessage, $selected));
144141
}
145-
$selectedChoices = explode(',', $selectedChoices);
142+
143+
$selectedChoices = array_map('trim', explode(',', $selected));
146144
} else {
147-
$selectedChoices = [$selected];
145+
$selectedChoices = [trim($selected)];
148146
}
149147

150148
$multiselectChoices = [];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Question;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Question\ChoiceQuestion;
16+
17+
class ChoiceQuestionTest extends TestCase
18+
{
19+
/**
20+
* @dataProvider selectUseCases
21+
*/
22+
public function testSelectUseCases($multiSelect, $answers, $expected, $message)
23+
{
24+
$question = new ChoiceQuestion('A question', [
25+
'First response',
26+
'Second response',
27+
'Third response',
28+
'Fourth response',
29+
]);
30+
31+
$question->setMultiselect($multiSelect);
32+
33+
foreach ($answers as $answer) {
34+
$validator = $question->getValidator();
35+
$actual = $validator($answer);
36+
37+
$this->assertEquals($actual, $expected, $message);
38+
}
39+
}
40+
41+
public function selectUseCases()
42+
{
43+
return [
44+
[
45+
false,
46+
['First response', 'First response ', ' First response', ' First response '],
47+
'First response',
48+
'When passed single answer on singleSelect, the defaultValidator must return this answer as a string',
49+
],
50+
[
51+
true,
52+
['First response', 'First response ', ' First response', ' First response '],
53+
['First response'],
54+
'When passed single answer on MultiSelect, the defaultValidator must return this answer as an array',
55+
],
56+
[
57+
true,
58+
['First response,Second response', ' First response , Second response '],
59+
['First response', 'Second response'],
60+
'When passed multiple answers on MultiSelect, the defaultValidator must return these answers as an array',
61+
],
62+
];
63+
}
64+
}

0 commit comments

Comments
 (0)