8000 [Console] Escape question text and default value in SymfonyStyle::ask() by chalasr · Pull Request #19961 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Escape question text and default value in SymfonyStyle::ask() #19961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Console] Escape default value and question in SymfonyStyle::ask()
  • Loading branch information
chalasr committed Sep 18, 2016
commit eed3cc5b5272dc2105b66483853d495613e20613
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Formatter\OutputFormatter;

/**
* Symfony Style Guide compliant question helper.
Expand Down Expand Up @@ -52,7 +53,7 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
*/
protected function writePrompt(OutputInterface $output, Question $question)
{
$text = $question->getQuestion();
$text = OutputFormatter::escape($question->getQuestion());
$default = $question->getDefault();

switch (true) {
Expand All @@ -74,18 +75,18 @@ protected function writePrompt(OutputInterface $output, Question $question)
$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;

case $question instanceof ChoiceQuestion:
$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;

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Question\ChoiceQuestion;

/**
Expand Down Expand Up @@ -73,6 +74,24 @@ public function testAskChoice()
$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)
{
$stream = fopen('php://memory', 'r+', false);
Expand Down
0