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;
19 * Eases the testing of console commands.
21 * @author Fabien Potencier <fabien@symfony.com>
32 * @param Command $command A Command instance to test.
34 public function __construct(Command $command)
36 $this->command = $command;
40 * Executes the command.
44 * * interactive: Sets the input interactive flag
45 * * decorated: Sets the output decorated flag
46 * * verbosity: Sets the output verbosity flag
48 * @param array $input An array of arguments and options
49 * @param array $options An array of options
51 * @return integer The command exit code
53 public function execute(array $input, array $options = array())
55 $this->input = new ArrayInput($input);
56 if (isset($options['interactive'])) {
57 $this->input->setInteractive($options['interactive']);
60 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
61 if (isset($options['decorated'])) {
62 $this->output->setDecorated($options['decorated']);
64 if (isset($options['verbosity'])) {
65 $this->output->setVerbosity($options['verbosity']);
68 return $this->command->run($this->input, $this->output);
72 * Gets the display returned by the last execution of the command.
74 * @return string The display
76 public function getDisplay()
78 rewind($this->output->getStream());
80 return stream_get_contents($this->output->getStream());
84 * Gets the input instance used by the last execution of the command.
86 * @return InputInterface The current input instance
88 public function getInput()
94 * Gets the output instance used by the last execution of the command.
96 * @return OutputInterface The current output instance
98 public function getOutput()
100 return $this->output;