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\Input\InputInterface;
17 use Symfony\Component\Console\Output\OutputInterface;
18 use Symfony\Component\Console\Output\StreamOutput;
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();
34 public function __construct(Command $command)
36 $this->command = $command;
40 * Executes the command.
42 * Available execution options:
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 command arguments and options
49 * @param array $options An array of execution options
51 * @return int The command exit code
53 public function execute(array $input, array $options = array())
55 // set the command name automatically if the application requires
56 // this argument and no command name was passed
57 if (!isset($input['command'])
58 && (null !== $application = $this->command->getApplication())
59 && $application->getDefinition()->hasArgument('command')
61 $input = array_merge(array('command' => $this->command->getName()), $input);
64 $this->input = new ArrayInput($input);
66 $this->input->setStream(self::createStream($this->inputs));
69 if (isset($options['interactive'])) {
70 $this->input->setInteractive($options['interactive']);
73 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
74 $this->output->setDecorated(isset($options['decorated']) ? $options['decorated'] : false);
75 if (isset($options['verbosity'])) {
76 $this->output->setVerbosity($options['verbosity']);
79 return $this->statusCode = $this->command->run($this->input, $this->output);
83 * Gets the display returned by the last execution of the command.
85 * @param bool $normalize Whether to normalize end of lines to \n or not
87 * @return string The display
89 public function getDisplay($normalize = false)
91 rewind($this->output->getStream());
93 $display = stream_get_contents($this->output->getStream());
96 $display = str_replace(PHP_EOL, "\n", $display);
103 * Gets the input instance used by the last execution of the command.
105 * @return InputInterface The current input instance
107 public function getInput()
113 * Gets the output instance used by the last execution of the command.
115 * @return OutputInterface The current output instance
117 public function getOutput()
119 return $this->output;
123 * Gets the status code returned by the last execution of the application.
125 * @return int The status code
127 public function getStatusCode()
129 return $this->statusCode;
133 * Sets the user inputs.
135 * @param array $inputs An array of strings representing each input
136 * passed to the command input stream
138 * @return CommandTester
140 public function setInputs(array $inputs)
142 $this->inputs = $inputs;
147 private static function createStream(array $inputs)
149 $stream = fopen('php://memory', 'r+', false);
151 fwrite($stream, implode(PHP_EOL, $inputs));