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\Application;
15 use Symfony\Component\Console\Input\ArrayInput;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Output\ConsoleOutput;
18 use Symfony\Component\Console\Output\OutputInterface;
19 use Symfony\Component\Console\Output\StreamOutput;
22 * Eases the testing of console applications.
24 * When testing an application, don't forget to disable the auto exit flag:
26 * $application = new Application();
27 * $application->setAutoExit(false);
29 * @author Fabien Potencier <fabien@symfony.com>
31 class ApplicationTester
37 * @var OutputInterface
40 private $captureStreamsIndependently = false;
42 public function __construct(Application $application)
44 $this->application = $application;
48 * Executes the application.
52 * * interactive: Sets the input interactive flag
53 * * decorated: Sets the output decorated flag
54 * * verbosity: Sets the output verbosity flag
55 * * capture_stderr_separately: Make output of stdOut and stdErr separately available
57 * @param array $input An array of arguments and options
58 * @param array $options An array of options
60 * @return int The command exit code
62 public function run(array $input, $options = array())
64 $this->input = new ArrayInput($input);
65 if (isset($options['interactive'])) {
66 $this->input->setInteractive($options['interactive']);
69 $this->captureStreamsIndependently = array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately'];
70 if (!$this->captureStreamsIndependently) {
71 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
72 if (isset($options['decorated'])) {
73 $this->output->setDecorated($options['decorated']);
75 if (isset($options['verbosity'])) {
76 $this->output->setVerbosity($options['verbosity']);
79 $this->output = new ConsoleOutput(
80 isset($options['verbosity']) ? $options['verbosity'] : ConsoleOutput::VERBOSITY_NORMAL,
81 isset($options['decorated']) ? $options['decorated'] : null
84 $errorOutput = new StreamOutput(fopen('php://memory', 'w', false));
85 $errorOutput->setFormatter($this->output->getFormatter());
86 $errorOutput->setVerbosity($this->output->getVerbosity());
87 $errorOutput->setDecorated($this->output->isDecorated());
89 $reflectedOutput = new \ReflectionObject($this->output);
90 $strErrProperty = $reflectedOutput->getProperty('stderr');
91 $strErrProperty->setAccessible(true);
92 $strErrProperty->setValue($this->output, $errorOutput);
94 $reflectedParent = $reflectedOutput->getParentClass();
95 $streamProperty = $reflectedParent->getProperty('stream');
96 $streamProperty->setAccessible(true);
97 $streamProperty->setValue($this->output, fopen('php://memory', 'w', false));
100 return $this->statusCode = $this->application->run($this->input, $this->output);
104 * Gets the display returned by the last execution of the application.
106 * @param bool $normalize Whether to normalize end of lines to \n or not
108 * @return string The display
110 public function getDisplay($normalize = false)
112 rewind($this->output->getStream());
114 $display = stream_get_contents($this->output->getStream());
117 $display = str_replace(PHP_EOL, "\n", $display);
124 * Gets the output written to STDERR by the application.
126 * @param bool $normalize Whether to normalize end of lines to \n or not
130 public function getErrorOutput($normalize = false)
132 if (!$this->captureStreamsIndependently) {
133 throw new \LogicException('The error output is not available when the tester is run without "capture_stderr_separately" option set.');
136 rewind($this->output->getErrorOutput()->getStream());
138 $display = stream_get_contents($this->output->getErrorOutput()->getStream());
141 $display = str_replace(PHP_EOL, "\n", $display);
148 * Gets the input instance used by the last execution of the application.
150 * @return InputInterface The current input instance
152 public function getInput()
158 * Gets the output instance used by the last execution of the application.
160 * @return OutputInterface The current output instance
162 public function getOutput()
164 return $this->output;
168 * Gets the status code returned by the last execution of the application.
170 * @return int The status code
172 public function getStatusCode()
174 return $this->statusCode;