@@ -276,11 +276,9 @@ You can also use a validator with a hidden question::
276276Testing a Command that Expects Input
277277------------------------------------
278278
279- 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 ::
279+ If you want to write a unit test for a command which expects user inputs
280+ from the command line, you need to set the inputs like this ::
281281
282- use Symfony\Component\Console\Helper\QuestionHelper;
283- use Symfony\Component\Console\Helper\HelperSet;
284282 use Symfony\Component\Console\Tester\CommandTester;
285283
286284 // ...
@@ -289,26 +287,15 @@ from the command line, you need to set the helper input stream::
289287 // ...
290288 $commandTester = new CommandTester($command);
291289
292- $helper = $command->getHelper('question');
293- $helper->setInputStream($this->getInputStream("Test\n"));
290+ $commandTester->setInputs(array('Test'));
294291 // Equals to a user inputting "Test" and hitting ENTER
295- // If you need to enter a confirmation, "yes\n " will work
292+ // If you need to enter a confirmation, adding a second input saying "yes" will work
296293
297294 $commandTester->execute(array('command' => $command->getName()));
298295
299296 // $this->assertRegExp('/.../', $commandTester->getDisplay());
300297 }
301298
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- }
310-
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.
299+ By calling :method: `CommandTester::setInputs `, you imitate what the console would
300+ do internally with all user input through the CLI. This way you can test any
301+ user interaction (even complex ones) by passing the appropriate inputs.
0 commit comments