Skip to content

Commit

Permalink
Merge branch '5.0' into 5.1
Browse files Browse the repository at this point in the history
* 5.0:
  Make PhpDocExtractor compatible with phpDocumentor v5
  Reset question validator attempts only for actual stdin
  bumped Symfony version to 5.0.11
  updated VERSION for 5.0.10
  updated CHANGELOG for 5.0.10
  bumped Symfony version to 4.4.11
  updated VERSION for 4.4.10
  updated CHANGELOG for 4.4.10
  • Loading branch information
fabpot committed Jun 15, 2020
2 parents 0f0a271 + d981566 commit 95242c2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
8 changes: 5 additions & 3 deletions Helper/QuestionHelper.php
Expand Up @@ -508,14 +508,16 @@ private function getShell()

private function isTty(): bool
{
$inputStream = !$this->inputStream && \defined('STDIN') ? STDIN : $this->inputStream;
if (!\defined('STDIN')) {
return true;
}

if (\function_exists('stream_isatty')) {
return stream_isatty($inputStream);
return stream_isatty(fopen('php://input', 'r'));
}

if (\function_exists('posix_isatty')) {
return posix_isatty($inputStream);
return posix_isatty(fopen('php://input', 'r'));
}

return true;
Expand Down
39 changes: 28 additions & 11 deletions Tests/Helper/QuestionHelperTest.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Console\Tests\Helper;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\FormatterHelper;
Expand All @@ -21,6 +22,7 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Console\Tester\ApplicationTester;

/**
* @group tty
Expand Down Expand Up @@ -727,21 +729,36 @@ public function testAskThrowsExceptionOnMissingInputWithValidator()
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question);
}

public function testAskThrowsExceptionFromValidatorEarlyWhenTtyIsMissing()
public function testQuestionValidatorRepeatsThePrompt()
{
$this->expectException('Exception');
$this->expectExceptionMessage('Bar, not Foo');
$tries = 0;
$application = new Application();
$application->setAutoExit(false);
$application->register('question')
->setCode(function ($input, $output) use (&$tries) {
$question = new Question('This is a promptable question');
$question->setValidator(function ($value) use (&$tries) {
++$tries;
if (!$value) {
throw new \Exception();
}

$output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
$output->expects($this->once())->method('writeln');
return $value;
});

(new QuestionHelper())->ask($input, $output, $question);

(new QuestionHelper())->ask(
$this->createStreamableInputInterfaceMock($this->getInputStream('Foo'), true),
$output,
(new Question('Q?'))->setHidden(true)->setValidator(function ($input) {
throw new \Exception("Bar, not $input");
return 0;
})
);
;

$tester = new ApplicationTester($application);
$tester->setInputs(['', 'not-empty']);

$statusCode = $tester->run(['command' => 'question'], ['interactive' => true]);

$this->assertSame(2, $tries);
$this->assertSame($statusCode, 0);
}

public function testEmptyChoices()
Expand Down

0 comments on commit 95242c2

Please sign in to comment.