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

Skip to content

Commit 9a8943e

Browse files
committed
[Console] Fix infinite loop on missing input
Use console exception for missing input Backport console RuntimeException in 2.7
1 parent df130a3 commit 9a8943e

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
<?php
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace Symfony\Component\Console\Exception;
12+
/**
13+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
14+
*/
15+
class RuntimeException extends \RuntimeException
16+
{
17+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function doAsk(OutputInterface $output, Question $question)
134134
if (false === $ret) {
135135
$ret = fgets($inputStream, 4096);
136136
if (false === $ret) {
137-
throw new \RuntimeException('Aborted');
137+
throw new RuntimeException('Aborted');
138138
}
139139
$ret = trim($ret);
140140
}
@@ -397,6 +397,8 @@ private function validateAttempts($interviewer, OutputInterface $output, Questio
397397

398398
try {
399399
return call_user_func($question->getValidator(), $interviewer());
400+
} catch (RuntimeException $e) {
401+
throw $e;
400402
} catch (\Exception $error) {
401403
}
402404
}

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