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

Skip to content

Commit 9132ae1

Browse files
committed
[Console] Fix validation of null values using SymfonyStyle::ask()
1 parent fcd401f commit 9132ae1

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
3535
$question->setValidator(function ($value) use ($validator) {
3636
if (null !== $validator) {
3737
$value = $validator($value);
38-
}
39-
40-
// make required
41-
if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
42-
throw new LogicException('A value is required.');
38+
} else {
39+
// make required
40+
if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
41+
throw new LogicException('A value is required.');
42+
}
4343
}
4444

4545
return $value;

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

Lines 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)