@@ -214,6 +214,8 @@ be suffixed with ``Bundle``. You can validate that by using the
214
214
:method: `Symfony\\ Component\\ Console\\ Question\\ Question::setValidator `
215
215
method::
216
216
217
+ use Symfony\Component\Console\Helper\QuestionHelper;
218
+ use Symfony\Component\Console\Helper\HelperSet;
217
219
use Symfony\Component\Console\Question\Question;
218
220
219
221
// ...
@@ -276,11 +278,9 @@ You can also use a validator with a hidden question::
276
278
Testing a Command that Expects Input
277
279
------------------------------------
278
280
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 ::
281
+ If you want to write a unit test for a command which expects user inputs
282
+ from the command line, you need to set the inputs like this ::
281
283
282
- use Symfony\Component\Console\Helper\QuestionHelper;
283
- use Symfony\Component\Console\Helper\HelperSet;
284
284
use Symfony\Component\Console\Tester\CommandTester;
285
285
286
286
// ...
@@ -289,26 +289,15 @@ from the command line, you need to set the helper input stream::
289
289
// ...
290
290
$commandTester = new CommandTester($command);
291
291
292
- $helper = $command->getHelper('question');
293
- $helper->setInputStream($this->getInputStream("Test\n"));
292
+ $commandTester->setInputs(array('Test'));
294
293
// Equals to a user inputting "Test" and hitting ENTER
295
- // If you need to enter a confirmation, "yes\n " will work
294
+ // If you need to enter a confirmation, adding a second input saying "yes" will work
296
295
297
296
$commandTester->execute(array('command' => $command->getName()));
298
297
299
298
// $this->assertRegExp('/.../', $commandTester->getDisplay());
300
299
}
301
300
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.
301
+ By calling :method: `CommandTester::setInputs `, you imitate what the console would
302
+ do internally with all user input through the CLI. This way you can test any
303
+ user interaction (even complex ones) by passing the appropriate inputs.
0 commit comments