Added i18n support to ConfirmationQuestion
This commit is contained in:
parent
a96291449d
commit
c2f3f897d6
@ -18,17 +18,22 @@ namespace Symfony\Component\Console\Question;
|
|||||||
*/
|
*/
|
||||||
class ConfirmationQuestion extends Question
|
class ConfirmationQuestion extends Question
|
||||||
{
|
{
|
||||||
|
private $trueAnswerRegex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param string $question The question to ask to the user
|
* @param string $question The question to ask to the user
|
||||||
* @param bool $default The default answer to return, true or false
|
* @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);
|
parent::__construct($question, (bool) $default);
|
||||||
|
|
||||||
$this->setNormalizer($this->getDefaultNormalizer());
|
$this->setNormalizer($this->getDefaultNormalizer());
|
||||||
|
|
||||||
|
$this->trueAnswerRegex = $trueAnswerRegex;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,11 +50,12 @@ class ConfirmationQuestion extends Question
|
|||||||
return $answer;
|
return $answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$answerIsTrue = (bool) preg_match($this->trueAnswerRegex, $answer);
|
||||||
if (false === $default) {
|
if (false === $default) {
|
||||||
return $answer && 'y' === strtolower($answer[0]);
|
return $answer && $answerIsTrue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return !$answer || 'y' === strtolower($answer[0]);
|
return !$answer || $answerIsTrue;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,27 +143,39 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
$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 = new QuestionHelper();
|
||||||
|
|
||||||
$dialog->setInputStream($this->getInputStream("\n\n"));
|
$dialog->setInputStream($this->getInputStream($question."\n"));
|
||||||
$question = new ConfirmationQuestion('Do you like French fries?');
|
$question = new ConfirmationQuestion('Do you like French fries?', $default);
|
||||||
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
$this->assertEquals($expected, $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
|
||||||
$question = new ConfirmationQuestion('Do you like French fries?', false);
|
}
|
||||||
$this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
|
||||||
|
|
||||||
$dialog->setInputStream($this->getInputStream("y\nyes\n"));
|
public function getAskConfirmationData()
|
||||||
$question = new ConfirmationQuestion('Do you like French fries?', false);
|
{
|
||||||
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
return array(
|
||||||
$question = new ConfirmationQuestion('Do you like French fries?', false);
|
array('', true),
|
||||||
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
array('', false, false),
|
||||||
|
array('y', true),
|
||||||
|
array('yes', true),
|
||||||
|
array('n', false),
|
||||||
|
array('no', false),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$dialog->setInputStream($this->getInputStream("n\nno\n"));
|
public function testAskConfirmationWithCustomTrueAnswer()
|
||||||
$question = new ConfirmationQuestion('Do you like French fries?', true);
|
{
|
||||||
$this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
$dialog = new QuestionHelper();
|
||||||
$question = new ConfirmationQuestion('Do you like French fries?', true);
|
|
||||||
$this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
$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, '/^(j|y)/i');
|
||||||
|
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAskAndValidate()
|
public function testAskAndValidate()
|
||||||
|
Reference in New Issue
Block a user