Skip to content

Commit

Permalink
[Console] Fix PHP 8.1 deprecation in ChoiceQuestion
Browse files Browse the repository at this point in the history
  • Loading branch information
Rezyan authored and nicolas-grekas committed Jan 26, 2022
1 parent 26ab273 commit 32ba2ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Question/ChoiceQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ private function getDefaultValidator(): callable
return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
if ($multiselect) {
// Check for a separated comma values
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selected, $matches)) {
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', (string) $selected, $matches)) {
throw new InvalidArgumentException(sprintf($errorMessage, $selected));
}

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

if ($this->isTrimmable()) {
foreach ($selectedChoices as $k => $v) {
$selectedChoices[$k] = trim($v);
$selectedChoices[$k] = trim((string) $v);
}
}

Expand Down
18 changes: 16 additions & 2 deletions Tests/Question/ChoiceQuestionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ class ChoiceQuestionTest extends TestCase
/**
* @dataProvider selectUseCases
*/
public function testSelectUseCases($multiSelect, $answers, $expected, $message)
public function testSelectUseCases($multiSelect, $answers, $expected, $message, $default = null)
{
$question = new ChoiceQuestion('A question', [
'First response',
'Second response',
'Third response',
'Fourth response',
]);
null,
], $default);

$question->setMultiselect($multiSelect);

Expand Down Expand Up @@ -59,6 +60,19 @@ public function selectUseCases()
['First response', 'Second response'],
'When passed multiple answers on MultiSelect, the defaultValidator must return these answers as an array',
],
[
false,
[null],
null,
'When used null as default single answer on singleSelect, the defaultValidator must return this answer as null',
],
[
false,
['First response'],
'First response',
'When used a string as default single answer on singleSelect, the defaultValidator must return this answer as a string',
'First response',
],
];
}

Expand Down

0 comments on commit 32ba2ac

Please sign in to comment.