Skip to content

Commit 26fdbe0

Browse files
committed
[Console] Adapt doc for easier testing of commands needing user inputs
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' removing an ending dot in comment for consistency
1 parent a01f87f commit 26fdbe0

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

components/console/helpers/questionhelper.rst

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ Testing a Command that Expects Input
277277
------------------------------------
278278

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

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

292-
$helper = $command->getHelper('question');
293-
$helper->setInputStream($this->getInputStream("Test\n"));
294292
// Equals to a user inputting "Test" and hitting ENTER
295-
// If you need to enter a confirmation, "yes\n" will work
293+
$commandTester->setInputs(array('Test'));
294+
295+
// Equals to a user inputting "This", "That" and hitting ENTER
296+
// This can be used for answering two separated questions for instance
297+
$commandTester->setInputs(array('This', 'That'));
298+
299+
// For simulating a positive answer to a confirmation question, adding an
300+
// additional input saying "yes" will work
301+
$commandTester->setInputs(array('yes'));
296302

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

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

302-
protected function getInputStream($input)
303-
{
304-
$stream = fopen('php://memory', 'r+', false);
305-
fputs($stream, $input);
306-
rewind($stream);
307-
308-
return $stream;
309-
}
308+
By calling :method:`Symfony\\Component\\Console\\Tester\\CommandTester::setInputs`,
309+
you imitate what the console would do internally with all user input through the CLI.
310+
This method takes an array as only argument with, for each input that the command expects,
311+
a string representing what the user would have typed.
312+
This way you can test any user interaction (even complex ones) by passing the appropriate inputs.
310313

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy