[Console] Escape default value and question in SymfonyStyle::ask()
This commit is contained in:
parent
b28cd81575
commit
eed3cc5b52
@ -17,6 +17,7 @@ use Symfony\Component\Console\Question\ChoiceQuestion;
|
|||||||
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||||
use Symfony\Component\Console\Question\Question;
|
use Symfony\Component\Console\Question\Question;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
use Symfony\Component\Console\Formatter\OutputFormatter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Symfony Style Guide compliant question helper.
|
* Symfony Style Guide compliant question helper.
|
||||||
@ -52,7 +53,7 @@ class SymfonyQuestionHelper extends QuestionHelper
|
|||||||
*/
|
*/
|
||||||
protected function writePrompt(OutputInterface $output, Question $question)
|
protected function writePrompt(OutputInterface $output, Question $question)
|
||||||
{
|
{
|
||||||
$text = $question->getQuestion();
|
$text = OutputFormatter::escape($question->getQuestion());
|
||||||
$default = $question->getDefault();
|
$default = $question->getDefault();
|
||||||
|
|
||||||
switch (true) {
|
switch (true) {
|
||||||
@ -74,18 +75,18 @@ class SymfonyQuestionHelper extends QuestionHelper
|
|||||||
$default[$key] = $choices[trim($value)];
|
$default[$key] = $choices[trim($value)];
|
||||||
}
|
}
|
||||||
|
|
||||||
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, implode(', ', $default));
|
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case $question instanceof ChoiceQuestion:
|
case $question instanceof ChoiceQuestion:
|
||||||
$choices = $question->getChoices();
|
$choices = $question->getChoices();
|
||||||
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]);
|
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default]));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default);
|
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
|
||||||
}
|
}
|
||||||
|
|
||||||
$output->writeln($text);
|
$output->writeln($text);
|
||||||
|
@ -6,6 +6,7 @@ use Symfony\Component\Console\Helper\FormatterHelper;
|
|||||||
use Symfony\Component\Console\Helper\HelperSet;
|
use Symfony\Component\Console\Helper\HelperSet;
|
||||||
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
|
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
|
||||||
use Symfony\Component\Console\Output\StreamOutput;
|
use Symfony\Component\Console\Output\StreamOutput;
|
||||||
|
use Symfony\Component\Console\Question\Question;
|
||||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -73,6 +74,24 @@ class SymfonyQuestionHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
|
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAskEscapeDefaultValue()
|
||||||
|
{
|
||||||
|
$helper = new SymfonyQuestionHelper();
|
||||||
|
$helper->setInputStream($this->getInputStream('\\'));
|
||||||
|
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
|
||||||
|
|
||||||
|
$this->assertOutputContains('Can I have a backslash? [\]', $output);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAskEscapeLabel()
|
||||||
|
{
|
||||||
|
$helper = new SymfonyQuestionHelper();
|
||||||
|
$helper->setInputStream($this->getInputStream('sure'));
|
||||||
|
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want a \?'));
|
||||||
|
|
||||||
|
$this->assertOutputContains('Do you want a \?', $output);
|
||||||
|
}
|
||||||
|
|
||||||
protected function getInputStream($input)
|
protected function getInputStream($input)
|
||||||
{
|
{
|
||||||
$stream = fopen('php://memory', 'r+', false);
|
$stream = fopen('php://memory', 'r+', false);
|
||||||
|
Reference in New Issue
Block a user