Skip to content

Commit

Permalink
Added i18n support to ConfirmationQuestion
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj authored and fabpot committed Jan 25, 2015
1 parent a962914 commit c2f3f89
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
16 changes: 11 additions & 5 deletions src/Symfony/Component/Console/Question/ConfirmationQuestion.php
Expand Up @@ -18,17 +18,22 @@
*/
class ConfirmationQuestion extends Question
{
private $trueAnswerRegex;

/**
* Constructor.
*
* @param string $question The question to ask to the user
* @param bool $default The default answer to return, true or false
* @param string $question The question to ask to the user
* @param bool $default The default answer to return, true or false
* @param string $trueAnswerRegex A regex to match the "yes" answer
*/
public function __construct($question, $default = true)
public function __construct($question, $default = true, $trueAnswerRegex = '/^y/i')
{
parent::__construct($question, (bool) $default);

$this->setNormalizer($this->getDefaultNormalizer());

$this->trueAnswerRegex = $trueAnswerRegex;
}

/**
Expand All @@ -45,11 +50,12 @@ private function getDefaultNormalizer()
return $answer;
}

$answerIsTrue = (bool) preg_match($this->trueAnswerRegex, $answer);
if (false === $default) {
return $answer && 'y' === strtolower($answer[0]);
return $answer && $answerIsTrue;
}

return !$answer || 'y' === strtolower($answer[0]);
return !$answer || $answerIsTrue;
};
}
}
42 changes: 27 additions & 15 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Expand Up @@ -143,27 +143,39 @@ public function testAskHiddenResponse()
$this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
}

public function testAskConfirmation()
/**
* @dataProvider getAskConfirmationData
*/
public function testAskConfirmation($question, $expected, $default = true)
{
$dialog = new QuestionHelper();

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

$dialog->setInputStream($this->getInputStream("y\nyes\n"));
$question = new ConfirmationQuestion('Do you like French fries?', false);
public function getAskConfirmationData()
{
return array(
array('', true),
array('', false, false),
array('y', true),
array('yes', true),
array('n', false),
array('no', false),
);
}

public function testAskConfirmationWithCustomTrueAnswer()
{
$dialog = new QuestionHelper();

$dialog->setInputStream($this->getInputStream("j\ny\n"));
$question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
$question = new ConfirmationQuestion('Do you like French fries?', false);
$question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));

$dialog->setInputStream($this->getInputStream("n\nno\n"));
$question = new ConfirmationQuestion('Do you like French fries?', true);
$this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
$question = new ConfirmationQuestion('Do you like French fries?', true);
$this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
}

public function testAskAndValidate()
Expand Down

0 comments on commit c2f3f89

Please sign in to comment.