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\OutputInterface;
18 use Symfony\Component\Console\Output\StreamOutput;
21 * Eases the testing of console applications.
23 * When testing an application, don't forget to disable the auto exit flag:
25 * $application = new Application();
26 * $application->setAutoExit(false);
28 * @author Fabien Potencier <fabien@symfony.com>
30 class ApplicationTester
40 * @param Application $application An Application instance to test
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
56 * @param array $input An array of arguments and options
57 * @param array $options An array of options
59 * @return int The command exit code
61 public function run(array $input, $options = array())
63 $this->input = new ArrayInput($input);
64 if (isset($options['interactive'])) {
65 $this->input->setInteractive($options['interactive']);
68 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
69 if (isset($options['decorated'])) {
70 $this->output->setDecorated($options['decorated']);
72 if (isset($options['verbosity'])) {
73 $this->output->setVerbosity($options['verbosity']);
76 return $this->statusCode = $this->application->run($this->input, $this->output);
80 * Gets the display returned by the last execution of the application.
82 * @param bool $normalize Whether to normalize end of lines to \n or not
84 * @return string The display
86 public function getDisplay($normalize = false)
88 rewind($this->output->getStream());
90 $display = stream_get_contents($this->output->getStream());
93 $display = str_replace(PHP_EOL, "\n", $display);
100 * Gets the input instance used by the last execution of the application.
102 * @return InputInterface The current input instance
104 public function getInput()
110 * Gets the output instance used by the last execution of the application.
112 * @return OutputInterface The current output instance
114 public function getOutput()
116 return $this->output;
120 * Gets the status code returned by the last execution of the application.
122 * @return int The status code
124 public function getStatusCode()
126 return $this->statusCode;