4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tester;
14 use Symfony\Component\Console\Command\Command;
15 use Symfony\Component\Console\Input\ArrayInput;
16 use Symfony\Component\Console\Output\StreamOutput;
17 use Symfony\Component\Console\Input\InputInterface;
18 use Symfony\Component\Console\Output\OutputInterface;
21 * Eases the testing of console commands.
23 * @author Fabien Potencier <fabien@symfony.com>
24 * @author Robin Chalas <robin.chalas@gmail.com>
31 private $inputs = array();
37 * @param Command $command A Command instance to test
39 public function __construct(Command $command)
41 $this->command = $command;
45 * Executes the command.
47 * Available execution options:
49 * * interactive: Sets the input interactive flag
50 * * decorated: Sets the output decorated flag
51 * * verbosity: Sets the output verbosity flag
53 * @param array $input An array of command arguments and options
54 * @param array $options An array of execution options
56 * @return int The command exit code
58 public function execute(array $input, array $options = array())
60 // set the command name automatically if the application requires
61 // this argument and no command name was passed
62 if (!isset($input['command'])
63 && (null !== $application = $this->command->getApplication())
64 && $application->getDefinition()->hasArgument('command')
66 $input = array_merge(array('command' => $this->command->getName()), $input);
69 $this->input = new ArrayInput($input);
71 $this->input->setStream(self::createStream($this->inputs));
74 if (isset($options['interactive'])) {
75 $this->input->setInteractive($options['interactive']);
78 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
79 $this->output->setDecorated(isset($options['decorated']) ? $options['decorated'] : false);
80 if (isset($options['verbosity'])) {
81 $this->output->setVerbosity($options['verbosity']);
84 return $this->statusCode = $this->command->run($this->input, $this->output);
88 * Gets the display returned by the last execution of the command.
90 * @param bool $normalize Whether to normalize end of lines to \n or not
92 * @return string The display
94 public function getDisplay($normalize = false)
96 rewind($this->output->getStream());
98 $display = stream_get_contents($this->output->getStream());
101 $display = str_replace(PHP_EOL, "\n", $display);
108 * Gets the input instance used by the last execution of the command.
110 * @return InputInterface The current input instance
112 public function getInput()
118 * Gets the output instance used by the last execution of the command.
120 * @return OutputInterface The current output instance
122 public function getOutput()
124 return $this->output;
128 * Gets the status code returned by the last execution of the application.
130 * @return int The status code
132 public function getStatusCode()
134 return $this->statusCode;
138 * Sets the user inputs.
140 * @param array An array of strings representing each input
141 * passed to the command input stream.
143 * @return CommandTester
145 public function setInputs(array $inputs)
147 $this->inputs = $inputs;
152 private static function createStream(array $inputs)
154 $stream = fopen('php://memory', 'r+', false);
156 fwrite($stream, implode(PHP_EOL, $inputs));