8000 Added i18n support to ConfirmationQuestion · symfony/symfony@3cb280d · GitHub
[go: up one dir, main page]

Skip to content

Commit 3cb280d

Browse files
committed
Added i18n support to ConfirmationQuestion
1 parent 2b7a150 commit 3cb280d

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@
1818
*/
1919
class ConfirmationQuestion extends Question
2020
{
21+
private $trueAnswerRegex;
22+
2123
/**
2224
* Constructor.
2325
*
24-
* @param string $question The question to ask to the user
25-
* @param bool $default The default answer to return, true or false
26+
* @param string $question The question to ask to the user
27+
* @param bool $default The default answer to return, true or false
28+
* @param string $trueAnswerRegex A regex to match the "yes" answer
2629
*/
27-
public function __construct($question, $default = true)
30+
public function __construct($question, $default = true, $trueAnswerRegex = '/^y/i')
2831
{
2932
parent::__construct($question, (bool) $default);
3033

3134
$this->setNormalizer($this->getDefaultNormalizer());
35+
36+
$this->trueAnswerRegex = $trueAnswerRegex;
3237
}
3338

3439
/**
@@ -45,11 +50,12 @@ private function getDefaultNormalizer()
4550
return $answer;
4651
}
4752

53+
$answerIsTrue = (bool) preg_match($this->trueAnswerRegex, $answer);
4854
if (false === $default) {
49-
return $answer && 'y' === strtolower($answer[0]);
55+
return $answer && $answerIsTrue;
5056
}
5157

52-
return !$answer || 'y' === strtolower($answer[0]);
58+
return !$answer || $answerIsTrue;
5359
};
5460
}
5561
}

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,39 @@ public function testAskHiddenResponse()
143143
$this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
144144
}
145145

146-
public function testAskConfirmation()
146+
/**
147+
* @dataProvider getAskConfirmationData
148+
*/
149+
public function testAskConfirmation($question, $expected, $default = true)
147150
{
148151
$dialog = new QuestionHelper();
149152

150-
$dialog->setInputStream($this->getInputStream("\n\n"));
151-
$question = new ConfirmationQuestion('Do you like French fries?');
152-
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
153-
$question = new ConfirmationQuestion('Do you like French fries?', false);
154-
$this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
153+
$dialog->setInputStream($this->getInputStream($question."\n"));
154+
$question = new ConfirmationQuestion('Do you like French fries?', $default);
155+
$this->assertEquals($expected, $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
156+
}
155157

156-
$dialog->setInputStream($this->getInputStream("y\nyes\n"));
157-
$question = new ConfirmationQuestion('Do you like French fries?', false);
158+
public function getAskConfirmationData()
159+
{
160+
return array(
161+
array('', true),
162+
array('', false, false),
163+
array('y', true),
164+
array('yes', true),
165+
array('n', false),
166+
array('no', false),
167+
);
168+
}
169+
170+
public function testAskConfirmationWithCustomTrueAnswer()
171+
{
172+
$dialog = new QuestionHelper();
173+
174+
$dialog->setInputStream($this->getInputStream("j\ny\n"));
175+
$question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
158176
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
159-
$question = new ConfirmationQuestion('Do you like French fries?', false);
177+
$question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
160178
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
161-
162-
$dialog->setInputStream($this->getInputStream("n\nno\n"));
163-
$question = new ConfirmationQuestion('Do you like French fries?', true);
164-
$this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
165-
$question = new ConfirmationQuestion('Do you like French fries?', true);
166-
$this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
167179
}
168180

169181
public function testAskAndValidate()

0 commit comments

Comments
 (0)
0