Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eed3cc5

Browse files
committed
[Console] Escape default value and question in SymfonyStyle::ask()
1 parent b28cd81 commit eed3cc5

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Question\ConfirmationQuestion;
1818
use Symfony\Component\Console\Question\Question;
1919
use Symfony\Component\Console\Style\SymfonyStyle;
20+
use Symfony\Component\Console\Formatter\OutputFormatter;
2021

2122
/**
2223
* Symfony Style Guide compliant question helper.
@@ -52,7 +53,7 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
5253
*/
5354
protected function writePrompt(OutputInterface $output, Question $question)
5455
{
55-
$text = $question->getQuestion();
56+
$text = OutputFormatter::escape($question->getQuestion());
5657
$default = $question->getDefault();
5758

5859
switch (true) {
@@ -74,18 +75,18 @@ protected function writePrompt(OutputInterface $output, Question $question)
7475
$default[$key] = $choices[trim($value)];
7576
}
7677

77-
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, implode(', ', $default));
78+
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
7879

7980
break;
8081

8182
case $question instanceof ChoiceQuestion:
8283
$choices = $question->getChoices();
83-
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]);
84+
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default]));
8485

8586
break;
8687

8788
default:
88-
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default);
89+
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
8990
}
9091

9192
$output->writeln($text);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Helper\HelperSet;
77
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
88
use Symfony\Component\Console\Output\StreamOutput;
9+
use Symfony\Component\Console\Question\Question;
910
use Symfony\Component\Console\Question\ChoiceQuestion;
1011

1112
/**
@@ -73,6 +74,24 @@ public function testAskChoice()
7374
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
7475
}
7576

77+
public function testAskEscapeDefaultValue()
78+
{
79+
$helper = new SymfonyQuestionHelper();
80+
$helper->setInputStream($this->getInputStream('\\'));
81+
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
82+
83+
$this->assertOutputContains('Can I have a backslash? [\]', $output);
84+
}
85+
86+
public function testAskEscapeLabel()
87+
{
88+
$helper = new SymfonyQuestionHelper();
89+
$helper->setInputStream($this->getInputStream('sure'));
90+
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want a \?'));
91+
92+
$this->assertOutputContains('Do you want a \?', $output);
93+
}
94+
7695
protected function getInputStream($input)
7796
{
7897
$stream = fopen('php://memory', 'r+', false);

0 commit comments

Comments
 (0)