Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiSelect ChoiceQuestion when answers have spaces #32503

Merged
merged 1 commit into from Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/Symfony/Component/Console/Question/ChoiceQuestion.php
Expand Up @@ -134,17 +134,15 @@ private function getDefaultValidator()
$isAssoc = $this->isAssoc($choices);

return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
// Collapse all spaces.
$selectedChoices = str_replace(' ', '', $selected);

if ($multiselect) {
// Check for a separated comma values
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) {
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selected, $matches)) {
throw new InvalidArgumentException(sprintf($errorMessage, $selected));
}
$selectedChoices = explode(',', $selectedChoices);

$selectedChoices = array_map('trim', explode(',', $selected));
} else {
$selectedChoices = [$selected];
$selectedChoices = [trim($selected)];
}

$multiselectChoices = [];
Expand Down
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Question;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Question\ChoiceQuestion;

class ChoiceQuestionTest extends TestCase
{
/**
* @dataProvider selectUseCases
*/
public function testSelectUseCases($multiSelect, $answers, $expected, $message)
{
$question = new ChoiceQuestion('A question', [
'First response',
'Second response',
'Third response',
'Fourth response',
]);

$question->setMultiselect($multiSelect);

foreach ($answers as $answer) {
$validator = $question->getValidator();
$actual = $validator($answer);

$this->assertEquals($actual, $expected, $message);
}
}

public function selectUseCases()
{
return [
[
false,
['First response', 'First response ', ' First response', ' First response '],
'First response',
'When passed single answer on singleSelect, the defaultValidator must return this answer as a string',
],
[
true,
['First response', 'First response ', ' First response', ' First response '],
['First response'],
'When passed single answer on MultiSelect, the defaultValidator must return this answer as an array',
],
[
true,
['First response,Second response', ' First response , Second response '],
['First response', 'Second response'],
'When passed multiple answers on MultiSelect, the defaultValidator must return these answers as an array',
],
];
}
}