|
16 | 16 | use Symfony\Component\Console\Output\StreamOutput;
|
17 | 17 | use Symfony\Component\Console\Input\InputInterface;
|
18 | 18 | use Symfony\Component\Console\Output\OutputInterface;
|
| 19 | +use Symfony\Component\Console\Helper\HelperSet; |
| 20 | +use Symfony\Component\Console\Helper\QuestionHelper; |
19 | 21 |
|
20 | 22 | /**
|
21 | 23 | * Eases the testing of console commands.
|
22 | 24 | *
|
23 | 25 | * @author Fabien Potencier <[email protected]>
|
| 26 | + * @author Robin Chalas <[email protected]> |
24 | 27 | */
|
25 | 28 | class CommandTester
|
26 | 29 | {
|
27 | 30 | private $command;
|
28 | 31 | private $input;
|
29 | 32 | private $output;
|
| 33 | + private $userInputs = array(); |
30 | 34 | private $statusCode;
|
31 | 35 |
|
32 | 36 | /**
|
@@ -64,6 +68,10 @@ public function execute(array $input, array $options = array())
|
64 | 68 | $input = array_merge(array('command' => $this->command->getName()), $input);
|
65 | 69 | }
|
66 | 70 |
|
| 71 | + if ($this->userInputs) { |
| 72 | + $this->createInputStream(); |
| 73 | + } |
| 74 | + |
67 | 75 | $this->input = new ArrayInput($input);
|
68 | 76 | if (isset($options['interactive'])) {
|
69 | 77 | $this->input->setInteractive($options['interactive']);
|
@@ -129,4 +137,48 @@ public function getStatusCode()
|
129 | 137 | {
|
130 | 138 | return $this->statusCode;
|
131 | 139 | }
|
| 140 | + |
| 141 | + /** |
| 142 | + * Sets the faked user inputs. |
| 143 | + * |
| 144 | + * @param array $inputs |
| 145 | + * |
| 146 | + * @return CommandTester |
| 147 | + */ |
| 148 | + public function setUserInputs(array $inputs) |
| 149 | + { |
| 150 | + $this->userInputs = $inputs; |
| 151 | + |
| 152 | + return $this; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Gets the faked user inputs. |
| 157 | + * |
| 158 | + * @return array $inputs |
| 159 | + */ |
| 160 | + public function getUserInputs() |
| 161 | + { |
| 162 | + return $this->userInputs; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Creates the command input stream. |
| 167 | + */ |
| 168 | + private function createInputStream() |
| 169 | + { |
| 170 | + $stream = fopen('php://memory', 'r+', false); |
| 171 | + |
| 172 | + fputs($stream, implode("\n", $this->userInputs)); |
| 173 | + rewind($stream); |
| 174 | + |
| 175 | + if (null === $this->command->getHelperSet()) { |
| 176 | + $helper = new QuestionHelper(); |
| 177 | + $this->command->setHelperSet(new HelperSet(array($helper))); |
| 178 | + } else { |
| 179 | + $helper = $this->command->getHelper('question'); |
| 180 | + } |
| 181 | + |
| 182 | + $helper->setInputStream($stream); |
| 183 | + } |
132 | 184 | }
|
0 commit comments