Skip to content

Commit 6181df9

Browse files
committed
Merge branch '3.4' into 4.2
* 3.4: make tests forward compatible with DI 4.4 Fix multiSelect ChoiceQuestion when answers have spaces
2 parents f7d8f1b + b333892 commit 6181df9

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@ protected function createContainerFromFile($file, $data = [], $resetCompilerPass
13681368
if ($resetCompilerPasses) {
13691369
$container->getCompilerPassConfig()->setOptimizationPasses([]);
13701370
$container->getCompilerPassConfig()->setRemovingPasses([]);
1371+
$container->getCompilerPassConfig()->setAfterRemovingPasses([]);
13711372
}
13721373
$container->getCompilerPassConfig()->setBeforeOptimizationPasses([new LoggerPass()]);
13731374
$container->getCompilerPassConfig()->setBeforeRemovingPasses([new AddConstraintValidatorsPass(), new TranslatorPass('translator.default', 'translation.reader')]);
@@ -1390,6 +1391,7 @@ protected function createContainerFromClosure($closure, $data = [])
13901391

13911392
$container->getCompilerPassConfig()->setOptimizationPasses([]);
13921393
$container->getCompilerPassConfig()->setRemovingPasses([]);
1394+
$container->getCompilerPassConfig()->setAfterRemovingPasses([]);
13931395
$container->compile();
13941396

13951397
return $container;

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,15 @@ private function getDefaultValidator(): callable
129129
$isAssoc = $this->isAssoc($choices);
130130

131131
return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
132-
// Collapse all spaces.
133-
$selectedChoices = str_replace(' ', '', $selected);
134-
135132
if ($multiselect) {
136133
// Check for a separated comma values
137-
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) {
134+
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selected, $matches)) {
138135
throw new InvalidArgumentException(sprintf($errorMessage, $selected));
139136
}
140-
$selectedChoices = explode(',', $selectedChoices);
137+
138+
$selectedChoices = array_map('trim', explode(',', $selected));
141139
} else {
142-
$selectedChoices = [$selected];
140+
$selectedChoices = [trim($selected)];
143141
}
144142

145143
$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)