Skip to content

Commit 2994095

Browse files
ostroluckynicolas-grekas
authored andcommitted
Reset question validator attempts only for actual stdin
1 parent 4d6a02a commit 2994095

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,16 @@ private function getShell()
509509

510510
private function isTty(): bool
511511
{
512-
$inputStream = !$this->inputStream && \defined('STDIN') ? STDIN : $this->inputStream;
512+
if (!\defined('STDIN')) {
513+
return true;
514+
}
513515

514516
if (\function_exists('stream_isatty')) {
515-
return stream_isatty($inputStream);
517+
return stream_isatty(fopen('php://stdin', 'r'));
516518
}
517519

518520
if (\function_exists('posix_isatty')) {
519-
return posix_isatty($inputStream);
521+
return posix_isatty(fopen('php://stdin', 'r'));
520522
}
521523

522524
return true;

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Tests\Helper;
1313

14+
use Symfony\Component\Console\Application;
1415
use Symfony\Component\Console\Exception\InvalidArgumentException;
1516
use Symfony\Component\Console\Formatter\OutputFormatter;
1617
use Symfony\Component\Console\Helper\FormatterHelper;
@@ -21,6 +22,7 @@
2122
use Symfony\Component\Console\Question\ConfirmationQuestion;
2223
use Symfony\Component\Console\Question\Question;
2324
use Symfony\Component\Console\Terminal;
25+
use Symfony\Component\Console\Tester\ApplicationTester;
2426

2527
/**
2628
* @group tty
@@ -727,21 +729,36 @@ public function testAskThrowsExceptionOnMissingInputWithValidator()
727729
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question);
728730
}
729731

730-
public function testAskThrowsExceptionFromValidatorEarlyWhenTtyIsMissing()
732+
public function testQuestionValidatorRepeatsThePrompt()
731733
{
732-
$this->expectException('Exception');
733-
$this->expectExceptionMessage('Bar, not Foo');
734+
$tries = 0;
735+
$application = new Application();
736+
$application->setAutoExit(false);
737+
$application->register('question')
738+
->setCode(function ($input, $output) use (&$tries) {
739+
$question = new Question('This is a promptable question');
740+
$question->setValidator(function ($value) use (&$tries) {
741+
++$tries;
742+
if (!$value) {
743+
throw new \Exception();
744+
}
734745

735-
$output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
736-
$output->expects($this->once())->method('writeln');
746+
return $value;
747+
});
748+
749+
(new QuestionHelper())->ask($input, $output, $question);
737750

738-
(new QuestionHelper())->ask(
739-
$this->createStreamableInputInterfaceMock($this->getInputStream('Foo'), true),
740-
$output,
741-
(new Question('Q?'))->setHidden(true)->setValidator(function ($input) {
742-
throw new \Exception("Bar, not $input");
751+
return 0;
743752
})
744-
);
753+
;
754+
755+
$tester = new ApplicationTester($application);
756+
$tester->setInputs(['', 'not-empty']);
757+
758+
$statusCode = $tester->run(['command' => 'question'], ['interactive' => true]);
759+
760+
$this->assertSame(2, $tries);
761+
$this->assertSame($statusCode, 0);
745762
}
746763

747764
public function testEmptyChoices()

0 commit comments

Comments
 (0)