Skip to content

Commit

Permalink
bug #29754 Ensure final input of CommandTester works with default (Fi…
Browse files Browse the repository at this point in the history
…rehed)

This PR was merged into the 3.4 branch.

Discussion
----------

Ensure final input of CommandTester works with default

| Q             | A
| ------------- | ---
| Branch?       | 4.x
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

If the final element of `CommandTester::setInputs()` is an empty string (to send the default value), the internal stream the tester uses hits EOF and triggers the `Aborted` exception. This appends an additional EOL to the stream after the `implode` to simulate one final return key, allowing the final input to use the default value when used in the tester's documented style.

A test has been added to cover the new behavior, which failed before this change.

Commits
-------

6b87b67 Ensure final input of CommandTester works with default
  • Loading branch information
Robin Chalas committed Jan 4, 2019
2 parents 44bb346 + 6b87b67 commit b645c07
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Symfony/Component/Console/Tester/CommandTester.php
Expand Up @@ -148,7 +148,10 @@ private static function createStream(array $inputs)
{
$stream = fopen('php://memory', 'r+', false);

fwrite($stream, implode(PHP_EOL, $inputs));
foreach ($inputs as $input) {
fwrite($stream, $input.PHP_EOL);
}

rewind($stream);

return $stream;
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php
Expand Up @@ -112,6 +112,31 @@ public function testCommandWithInputs()
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
}

public function testCommandWithDefaultInputs()
{
$questions = array(
'What\'s your name?',
'How are you?',
'Where do you come from?',
);

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

$tester = new CommandTester($command);
$tester->setInputs(array('', '', ''));
$tester->execute(array());

$this->assertEquals(0, $tester->getStatusCode());
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
}

/**
* @expectedException \RuntimeException
* @expectedMessage Aborted
Expand Down

0 comments on commit b645c07

Please sign in to comment.