Skip to content

Commit 46ebf23

Browse files
bug symfony#29844 [Console] Fixed symfony#29835: ConfirmationQuestion with default true for answer '0' (mrthehud)
This PR was squashed before being merged into the 3.4 branch (closes symfony#29844). Discussion ---------- [Console] Fixed symfony#29835: ConfirmationQuestion with default true for answer '0' | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | Almost all, one failure on appveyor? | Fixed tickets | symfony#29835 | License | MIT | Doc PR | n/a <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> When using the ConfirmationQuestion class to ask a yes / no question, if the default is true, and the answer regex is '/^y/i', then any value not starting with [yY] is considered false. This must include "0", which previously would return true, producing results such as: ``` $ php bin/console do:stuff $ Do you want to continue? 0 <enter> $ Ok, continuing! ``` Commits ------- a0a7400 [Console] Fixed symfony#29835: ConfirmationQuestion with default true for answer '0'
2 parents 1a79a13 + a0a7400 commit 46ebf23

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private function getDefaultNormalizer()
5353
return $answer && $answerIsTrue;
5454
}
5555

56-
return !$answer || $answerIsTrue;
56+
return '' === $answer || $answerIsTrue;
5757
};
5858
}
5959
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\ConfirmationQuestion;
16+
17+
class ConfirmationQuestionTest extends TestCase
18+
{
19+
/**
20+
* @dataProvider normalizerUsecases
21+
*/
22+
public function testDefaultRegexUsecases($default, $answers, $expected, $message)
23+
{
24+
$sut = new ConfirmationQuestion('A question', $default);
25+
26+
foreach ($answers as $answer) {
27+
$normalizer = $sut->getNormalizer();
28+
$actual = $normalizer($answer);
29+
$this->assertEquals($expected, $actual, sprintf($message, $answer));
30+
}
31+
}
32+
33+
public function normalizerUsecases()
34+
{
35+
return [
36+
[
37+
true,
38+
['y', 'Y', 'yes', 'YES', 'yEs', ''],
39+
true,
40+
'When default is true, the normalizer must return true for "%s"',
41+
],
42+
[
43+
true,
44+
['n', 'N', 'no', 'NO', 'nO', 'foo', '1', '0'],
45+
false,
46+
'When default is true, the normalizer must return false for "%s"',
47+
],
48+
[
49+
false,
50+
['y', 'Y', 'yes', 'YES', 'yEs'],
51+
true,
52+
'When default is false, the normalizer must return true for "%s"',
53+
],
54+
[
55+
false,
56+
['n', 'N', 'no', 'NO', 'nO', 'foo', '1', '0', ''],
57+
false,
58+
'When default is false, the normalizer must return false for "%s"',
59+
],
60+
];
61+
}
62+
}

0 commit comments

Comments
 (0)