Skip to content

Commit

Permalink
[Console] Adapt doc for easier testing of commands needing user inputs
Browse files Browse the repository at this point in the history
Fix missing colon

Change userInputs to inputs

Try to fix platformsh build

Re-add useful use statements

Fixed typo, use FQCN::method

Rollback not useful diff

Formatting

Add more examples, precise documentation

typo 'would have type' => 'would have typed'
  • Loading branch information
chalasr committed Jun 23, 2016
1 parent a01f87f commit eaa3294
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions components/console/helpers/questionhelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ Testing a Command that Expects Input
------------------------------------

If you want to write a unit test for a command which expects some kind of input
from the command line, you need to set the helper input stream::
from the command line, you need to set the inputs that the command expects::

use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\HelperSet;
Expand All @@ -289,26 +289,28 @@ from the command line, you need to set the helper input stream::
// ...
$commandTester = new CommandTester($command);

$helper = $command->getHelper('question');
$helper->setInputStream($this->getInputStream("Test\n"));
$commandTester->setInputs(array('Test'));
// Equals to a user inputting "Test" and hitting ENTER
// If you need to enter a confirmation, "yes\n" will work

// Equals to a user inputting "This", "That" and hitting ENTER
// This can be used for answering two separated questions for instance.
$commandTester->setInputs(array('This', 'That'));

// For simulating a positive answer to a confirmation question, adding an
// additional input saying "yes" will work
$commandTester->setInputs(array('yes'));

$commandTester->execute(array('command' => $command->getName()));

// $this->assertRegExp('/.../', $commandTester->getDisplay());
}

protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
fputs($stream, $input);
rewind($stream);

return $stream;
}
By calling :method:`Symfony\\Component\\Console\\Tester\\CommandTester::setInputs`,
you imitate what the console would do internally with all user input through the CLI.
This method takes an array as only argument with, for each input that the command expects,
a string representing what the user would have typed.
This way you can test any user interaction (even complex ones) by passing the appropriate inputs.

By setting the input stream of the ``QuestionHelper``, you imitate what the
console would do internally with all user input through the CLI. This way
you can test any user interaction (even complex ones) by passing an appropriate
input stream.
.. note::
The :class:`Symfony\\Component\\Console\\Tester\\CommandTester` automatically simulates a user
hitting ``ENTER`` after each input, no need for passing an additional input.

0 comments on commit eaa3294

Please sign in to comment.