@@ -276,11 +276,9 @@ You can also use a validator with a hidden question::
276
276
Testing a Command that Expects Input
277
277
------------------------------------
278
278
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 :
281
281
282
- use Symfony\Component\Console\Helper\QuestionHelper;
283
- use Symfony\Component\Console\Helper\HelperSet;
284
282
use Symfony\C omponent\C onsole\T ester\C ommandTester;
285
283
286
284
// ...
@@ -289,26 +287,16 @@ from the command line, you need to set the helper input stream::
289
287
// ...
290
288
$commandTester = new CommandTester($command);
291
289
292
- $helper = $command->getHelper('question');
293
- $helper->setInputStream($this->getInputStream('Test\\n'));
290
+ $commandTester->setUserInputs(array('Test'));
294
291
// 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
296
293
297
294
$commandTester->execute(array('command' => $command->getName()));
298
295
299
296
// $this->assertRegExp('/.../', $commandTester->getDisplay());
300
297
}
301
298
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
299
+ By calling :method: `CommandTester::setUserInputs `, you imitate what the
312
300
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 .
301
+ you can test any user interaction (even complex ones) by passing the appropriate
302
+ inputs .
0 commit comments