Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f9f1a9

Browse files
committed
Reset question validator attempts only for actual stdin
1 parent 4d6a02a commit 8f9f1a9

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
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(STDIN);
516518
}
517519

518520
if (\function_exists('posix_isatty')) {
519-
return posix_isatty($inputStream);
521+
return posix_isatty(STDIN);
520522
}
521523

522524
return true;

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

Lines changed: 34 additions & 0 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
@@ -844,6 +846,38 @@ public function testTraversableMultiselectAutocomplete()
844846
$this->assertEquals(['AcmeDemoBundle', 'AsseticBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
845847
}
846848

849+
public function testQuestionValidatorRepeatsThePrompt()
850+
{
851+
$tries = 0;
852+
$application = new Application();
853+
$application->setAutoExit(false);
854+
$application->register('question')
855+
->setCode(function ($input, $output) use (&$tries) {
856+
$question = new Question('This is a promptable question');
857+
$question->setValidator(function ($value) use (&$tries) {
858+
$tries++;
859+
if (!$value) {
860+
throw new \Exception();
861+
}
862+
863+
return $value;
864+
});
865+
866+
(new QuestionHelper())->ask($input, $output, $question);
867+
868+
return 0;
869+
})
870+
;
871+
872+
$tester = new ApplicationTester($application);
873+
$tester->setInputs(['', 'not-empty']);
874+
875+
$statusCode = $tester->run(['command' => 'question'], ['interactive' => true]);
876+
877+
$this->assertSame(2, $tries);
878+
$this->assertSame($statusCode, 0);
879+
}
880+
847881
protected function getInputStream($input)
848882
{
849883
$stream = fopen('php://memory', 'r+', false);

0 commit comments

Comments
 (0)