10000 [Console] Fix infinite loop on missing input · symfony/symfony@9405bd5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9405bd5

Browse files
committed
[Console] Fix infinite loop on missing input
1 parent 779a6df commit 9405bd5

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ private function validateAttempts($interviewer, OutputInterface $output, Questio
399399

400400
try {
401401
return call_user_func($question->getValidator(), $interviewer());
402+
} catch (RuntimeException $e) {
403+
throw $e;
402404
} catch (\Exception $error) {
403405
}
404406
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,25 @@ 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 testAskThrowsExceptionOnMissingInputWithValidator()
410+
{
411+
$dialog = new QuestionHelper();
412+
$dialog->setInputStream($this->getInputStream(''));
413+
414+
$question = new Question('What\'s your name?');
415+
$question->setValidator(function () {
416+
if (!$value) {
417+
throw new \Exception('A value is required.');
418+
}
419+
});
420+
421+
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
422+
}
423+
405424
protected function getInputStream($input)
406425
{
407426
$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)
0