8000 [Console] Fix bug with utf-8 bug, change writePrompt to use one function prepareChoices by proggga · Pull Request #33911 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Fix bug with utf-8 bug, change writePrompt to use one function prepareChoices #33911

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix review, fix test, not it show that previouse code fails with utf
  • Loading branch information
proggga committed Oct 24, 2019
commit 21c5a8b1b2644ab24abb31fca9e31d83e5fa8372
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/
class QuestionHelper extends Helper
{
const DEFAULT_PROMPT = ' > ';
private $inputStream;
private static $shell;
private static $stty;
Expand Down Expand Up @@ -209,7 +208,8 @@ protected function writePrompt(OutputInterface $output, Question $question)
}

/**
* @param $tag
* @param string $tag
* @param ChoiceQuestion $question
*
* @return string[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ protected function writePrompt(OutputInterface $output, Question $question)

$output->writeln($messages);
// ChoiceQuestion can have any prompt
$output->write($question->getPrompt() ?: static::DEFAULT_PROMPT);
$output->write($question->getPrompt() ?: Question::DEFAULT_PROMPT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPrompt() will always return a string. The constant is also useless. Just write the prompt.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's another bug fixed by this PR btw. Unrelated to the linked issue.

} else {
$output->write(static::DEFAULT_PROMPT);
$output->write(Question::DEFAULT_PROMPT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here keep the old behavior?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should actually move the prompt from the ChoiceQuestion to the Question class? 🤔

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Question/ChoiceQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ChoiceQuestion extends Question
{
private $choices;
private $multiselect = false;
private $prompt = null;
private $prompt = ' > ';
private $errorMessage = 'Value "%s" is invalid';

/**
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/Question/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
class Question
{
const DEFAULT_PROMPT = ' > ';
private $question;
private $attempts;
private $hidden = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,38 @@ public function testAskEscapeAndFormatLabel()

public function testForUtf8Keys()
{
$question = 'Lorem ipsum?';
$possibleChoices = [
'foo' => 'foo',
'żółw' => 'bar',
'łabądź' => 'baz',
];
$question_result = ' <info>Lorem ipsum?</info> [<comment>foo</comment>]:';
$outputShown = [
' [<comment>foo </comment>] foo',
' [<comment>żółw </comment>] bar',
' [<comment>łabądź</comment>] baz',
];

$dialog = new SymfonyQuestionHelper();

$question = new ChoiceQuestion($question, $possibleChoices, 'foo');

$output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
$output->method('getFormatter')->willReturn(new OutputFormatter());

$input = $this->createStreamableInputInterfaceMock($this->getInputStream('foo'));
$output->expects($this->exactly(2))->method('writeln')->withConsecutive([$question_result], [$outputShown]);
$dialog->ask($input, $output, $question);
$question = 'Lorem ipsum?';
$possibleChoices = [
'foo' => 'foo',
'żółw' => 'bar',
'łabądź' => 'baz',
];
$question_result = ' <info>Lorem ipsum?</info> [<comment>foo</comment>]:';
$outputShown = [
' [<comment>foo </comment>] foo',
' [<comment>żółw </comment>] bar',
' [<comment>łabądź</comment>] baz',
];

$dialog = new SymfonyQuestionHelper();

$question = new ChoiceQuestion($question, $possibleChoices, 'foo');

$output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
$output->method('getFormatter')->willReturn(new OutputFormatter());
$question->setValidator(null);

$input = $this->createStreamableInputInterfaceMock($this->getInputStream('foo'));
$result = [];
$output->method('writeln')->willReturnCallback(function ($params) use (&$result) {
if (is_array($params)) {
$result = array_merge($result, $params);
} else {
$result[] = $params;
}
});
$dialog->ask($input, $output, $question);
$this->assertEquals(array_merge([$question_result],$outputShown), $result);
}

public function testAskDefaultPrompt()
Expand Down
0