@@ -97,4 +97,43 @@ You can set the max number of times to ask in the ``$attempts`` argument.
9797If you reach this max number it will use the default value, which is given
9898in the last argument. Using ``false `` means the amount of attempts is infinite.
9999The user will be asked as long as he provides an invalid answer and will only
100- be able to proceed if her input is valid.
100+ be able to proceed if her input is valid.
101+
102+ Testing a command which expects input
103+ -------------------------------------
104+
105+ If you want to write a unit test for a command which expects some kind of input
106+ from the command line, you need to overwrite the HelperSet used by the command::
107+
108+ use Symfony\Component\Console\Helper\DialogHelper;
109+ use Symfony\Component\Console\Helper\HelperSet;
110+
111+ // ...
112+ public function testExecute()
113+ {
114+ // ...
115+ $commandTester = new CommandTester($command);
116+
117+ $dialog = $command->getHelper('dialog');
118+ $dialog->setInputStream($this->getInputStream('Test\n'));
119+ // Equals to a user inputing "Test" and hitting ENTER
120+ // If you need to enter a confirmation, "yes\n" will work
121+
122+ $commandTester->execute(array('command' => $command->getName()));
123+
124+ // $this->assertRegExp('/.../', $commandTester->getDisplay());
125+ }
126+
127+ protected function getInputStream($input)
128+ {
129+ $stream = fopen('php://memory', 'r+', false);
130+ fputs($stream, $input);
131+ rewind($stream);
132+
133+ return $stream;
134+ }
135+
136+ By setting the inputStream of the `DialogHelper `, you do the same the
137+ console would do internally with all user input through the cli. This way
138+ you can test any user interaction (even complex ones) by passing an appropriate
139+ input stream.