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>
35 * @param Command $command A Command instance to test
37 public function __construct(Command $command)
39 $this->command = $command;
43 * Executes the command.
45 * Available execution options:
47 * * interactive: Sets the input interactive flag
48 * * decorated: Sets the output decorated flag
49 * * verbosity: Sets the output verbosity flag
51 * @param array $input An array of command arguments and options
52 * @param array $options An array of execution options
54 * @return int The command exit code
56 public function execute(array $input, array $options = array())
58 // set the command name automatically if the application requires
59 // this argument and no command name was passed
60 if (!isset($input['command'])
61 && (null !== $application = $this->command->getApplication())
62 && $application->getDefinition()->hasArgument('command')
64 $input = array_merge(array('command' => $this->command->getName()), $input);
67 $this->input = new ArrayInput($input);
68 if (isset($options['interactive'])) {
69 $this->input->setInteractive($options['interactive']);
72 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
73 $this->output->setDecorated(isset($options['decorated']) ? $options['decorated'] : false);
74 if (isset($options['verbosity'])) {
75 $this->output->setVerbosity($options['verbosity']);
78 return $this->statusCode = $this->command->run($this->input, $this->output);
82 * Gets the display returned by the last execution of the command.
84 * @param bool $normalize Whether to normalize end of lines to \n or not
86 * @return string The display
88 public function getDisplay($normalize = false)
90 rewind($this->output->getStream());
92 $display = stream_get_contents($this->output->getStream());
95 $display = str_replace(PHP_EOL, "\n", $display);
102 * Gets the input instance used by the last execution of the command.
104 * @return InputInterface The current input instance
106 public function getInput()
112 * Gets the output instance used by the last execution of the command.
114 * @return OutputInterface The current output instance
116 public function getOutput()
118 return $this->output;
122 * Gets the status code returned by the last execution of the application.
124 * @return int The status code
126 public function getStatusCode()
128 return $this->statusCode;