Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8698602

Browse filesBrowse files
committed
[Console] Fix validation of null values using SymfonyStyle::ask()
1 parent 9ed8d57 commit 8698602
Copy full SHA for 8698602

File tree

2 files changed

+15
-5
lines changed
Filter options

2 files changed

+15
-5
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
3434
$question->setValidator(function ($value) use ($validator) {
3535
if (null !== $validator) {
3636
$value = $validator($value);
37-
}
38-
39-
// make required
40-
if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
41-
throw new \Exception('A value is required.');
37+
} else {
38+
// make required
39+
if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
40+
throw new LogicException('A value is required.');
41+
}
4242
}
4343

4444
return $value;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php
+10Lines changed: 10 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,15 @@ public function testAskChoice()
7374
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
7475
}
7576

77+
public function testAskReturnsNullIfValidatorAllowsIt()
78+
{
79+
$questionHelper = new SymfonyQuestionHelper();
80+
$questionHelper->setInputStream($this->getInputStream("\n"));
81+
$question = new Question('What is your favorite superhero?');
82+
$question->setValidator(function ($value) { return $value; });
83+
$this->assertNull($questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
84+
}
85+
7686
protected function getInputStream($input)
7787
{
7888
$stream = fopen('php://memory', 'r+', false);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.