8000 bug #32781 [Console] Fix disabling ChoiceQuestion answer trimming (ch… · symfony/symfony@a29aff0 · GitHub
[go: up one dir, main page]

Skip to content

Commit a29aff0

Browse files
committed
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 ------- 44ee056 [Console] Fix disabling ChoiceQuestion answer trimming
2 parents 119ac78 + 44ee056 commit a29aff0

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,15 @@ private function getDefaultValidator(): callable
135135
throw new InvalidArgumentException(sprintf($errorMessage, $selected));
136136
}
137137

138-
$selectedChoices = array_map('trim', explode(',', $selected));
138+
$selectedChoices = explode(',', $selected);
139139
} else {
140-
$selectedChoices = [trim($selected)];
140+
$selectedChoices = [$selected];
141+
}
142+
143+
if ($this->isTrimmable()) {
144+
foreach ($selectedChoices as $k => $v) {
145+
$selectedChoices[$k] = trim($v);
146+
}
141147
}
142148

143149
$multiselectChoices = [];

src/Symfony/Component/Console/Tests/Question/ChoiceQuestionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,20 @@ public function selectUseCases()
6161
],
6262
];
6363
}
64+
65+
public function testNonTrimmable()
66+
{
67+
$question = new ChoiceQuestion('A question', [
68+
'First response ',
69+
' Second response',
70+
' Third response ',
71+
]);
72+
$question->setTrimmable(false);
73+
74+
$this->assertSame(' Third response ', $question->getValidator()(' Third response '));
75+
76+
$question->setMultiselect(true);
77+
78+
$this->assertSame(['First response ', ' Second response'], $question->getValidator()('First response , Second response'));
79+
}
6480
}

0 commit comments

Comments
 (0)
0