8000 [Console] Fix infinite loop on missing input · symfony/symfony@04ccc84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04ccc84

Browse files
committed
[Console] Fix infinite loop on missing input
[Console] Use console exception for missing input
1 parent 779a6df commit 04ccc84

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
< 8000 code class="diff-text-cell hunk">
@@ -136,7 +136,7 @@ public function doAsk(OutputInterface $output, Question $question)
136136
if (false === $ret) {
137137
$ret = fgets($inputStream, 4096);
138138
if (false === $ret) {
139-
throw new \RuntimeException('Aborted');
139+
throw new RuntimeException('Aborted');
140140
}
141141
$ret = trim($ret);
142142
}
@@ -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: 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)
0