Skip to content

Commit

Permalink
[Console] Fix command testing with missing inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Chalas committed Feb 16, 2019
1 parent 37b0eeb commit 7676bf1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Expand Up @@ -166,7 +166,7 @@ private function doAsk(OutputInterface $output, Question $question)
if (false === $ret) {
$ret = fgets($inputStream, 4096);
if (false === $ret) {
throw new RuntimeException('Aborted');
throw new RuntimeException('Aborted.');
}
$ret = trim($ret);
}
Expand Down Expand Up @@ -252,8 +252,9 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
while (!feof($inputStream)) {
$c = fread($inputStream, 1);

// Backspace Character
if ("\177" === $c) {
if (false === $c || ('' === $ret && '' === $c && null === $question->getDefault())) {
throw new RuntimeException('Aborted.');
} else if ("\177" === $c) { // Backspace Character
if (0 === $numMatches && 0 !== $i) {
--$i;
// Move cursor backwards
Expand Down Expand Up @@ -380,7 +381,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream)
shell_exec(sprintf('stty %s', $sttyMode));

if (false === $value) {
throw new RuntimeException('Aborted');
throw new RuntimeException('Aborted.');
}

$value = trim($value);
Expand Down
5 changes: 2 additions & 3 deletions src/Symfony/Component/Console/Tester/CommandTester.php
Expand Up @@ -62,9 +62,8 @@ public function execute(array $input, array $options = [])
}

$this->input = new ArrayInput($input);
if ($this->inputs) {
$this->input->setStream(self::createStream($this->inputs));
}
// Use an in-memory input stream even if no inputs are set so that QuestionHelper::ask() does not rely on the blocking STDIN.
$this->input->setStream(self::createStream($this->inputs));

if (isset($options['interactive'])) {
$this->input->setInteractive($options['interactive']);
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Validator\Constraints\Choice;

/**
* @group tty
Expand Down Expand Up @@ -908,6 +909,16 @@ public function testAskThrowsExceptionOnMissingInput()
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
}

/**
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
* @expectedExceptionMessage Aborted
*/
public function testAskThrowsExceptionOnMissingInputForChoiceQuestion()
{
$dialog = new QuestionHelper();
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new ChoiceQuestion('Choice', ['a', 'b']));
}

/**
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
* @expectedExceptionMessage Aborted
Expand Down
32 changes: 30 additions & 2 deletions src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Tester\CommandTester;
Expand Down Expand Up @@ -139,7 +140,7 @@ public function testCommandWithDefaultInputs()

/**
* @expectedException \RuntimeException
* @expectedMessage Aborted
* @expectedExceptionMessage Aborted
*/
public function testCommandWithWrongInputsNumber()
{
Expand All @@ -153,13 +154,40 @@ public function testCommandWithWrongInputsNumber()
$command->setHelperSet(new HelperSet([new QuestionHelper()]));
$command->setCode(function ($input, $output) use ($questions, $command) {
$helper = $command->getHelper('question');
$helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
$helper->ask($input, $output, new Question($questions[0]));
$helper->ask($input, $output, new Question($questions[1]));
$helper->ask($input, $output, new Question($questions[2]));
});

$tester = new CommandTester($command);
$tester->setInputs(['a', 'Bobby', 'Fine']);
$tester->execute([]);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Aborted
*/
public function testCommandWithQuestionsButNoInputs()
{
$questions = [
'What\'s your name?',
'How are you?',
'Where do you come from?',
];

$command = new Command('foo');
$command->setHelperSet(new HelperSet([new QuestionHelper()]));
$command->setCode(function ($input, $output) use ($questions, $command) {
$helper = $command->getHelper('question');
$helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
$helper->ask($input, $output, new Question($questions[0]));
$helper->ask($input, $output, new Question($questions[1]));
$helper->ask($input, $output, new Question($questions[2]));
});

$tester = new CommandTester($command);
$tester->setInputs(['Bobby', 'Fine']);
$tester->execute([]);
}

Expand Down

0 comments on commit 7676bf1

Please sign in to comment.