Skip to content

Commit 8d5c563

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [Console] Fix infinite loop on missing input
2 parents 2b0cec5 + 74e65e1 commit 8d5c563

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
2020
use Symfony\Component\Console\Question\Question;
2121
use Symfony\Component\Console\Question\ChoiceQuestion;
22+
use Symfony\Component\Console\Exception\RuntimeException;
2223

2324
/**
2425
* The QuestionHelper class provides helpers to interact with the user.
@@ -136,7 +137,7 @@ public function doAsk(OutputInterface $output, Question $question)
136137
if (false === $ret) {
137138
$ret = fgets($inputStream, 4096);
138139
if (false === $ret) {
139-
throw new \RuntimeException('Aborted');
140+
throw new RuntimeException('Aborted');
140141
}
141142
$ret = trim($ret);
142143
}
@@ -399,6 +400,8 @@ private function validateAttempts($interviewer, OutputInterface $output, Questio
399400

400401
try {
401402
return call_user_func($question->getValidator(), $interviewer());
403+
} catch (RuntimeException $e) {
404+
throw $e;
402405
} catch (\Exception $error) {
403406
}
404407
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,37 @@ public function testChoiceOutputFormattingQuestionForUtf8Keys()
402402
$dialog->ask($this->createInputInterfaceMock(), $output, $question);
403403
}
404404

405+
/**
406+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
407+
* @expectedExceptionMessage Aborted
408+
*/
409+
public function testAskThrowsExceptionOnMissingInput()
410+
{
411+
$dialog = new QuestionHelper();
412+
$dialog->setInputStream($this->getInputStream(''));
413+
414+
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), new Question('What\'s your name?'));
415+
}
416+
417+
/**
418+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
419+
* @expectedExceptionMessage Aborted
420+
*/
421+
public function testAskThrowsExceptionOnMissingInputWithValidator()
422+
{
423+
$dialog = new QuestionHelper();
424+
$dialog->setInputStream($this->getInputStream(''));
425+
426+
$question = new Question('What\'s your name?');
427+
$question->setValidator(function () {
428+
if (!$value) {
429+
throw new \Exception('A value is required.');
430+
}
431+
});
432+
433+
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
434+
}
435+
405436
protected function getInputStream($input)
406437
{
407438
$stream = fopen('php://memory', 'r+', false);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ public function testAskEscapeLabel()
101101
$this->assertOutputContains('Do you want a \?', $output);
102102
}
103103

104+
/**
105+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
106+
* @expectedExceptionMessage Aborted
107+
*/
108+
public function testAskThrowsExceptionOnMissingInput()
109+
{
110+
$dialog = new SymfonyQuestionHelper();
111+
112+
$dialog->setInputStream($this->getInputStream(''));
113+
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), new Question('What\'s your name?'));
114+
}
115+
104116
protected function getInputStream($input)
105117
{
106118
$stream = fopen('php://memory', 'r+', false);

0 commit comments

Comments
 (0)