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 * @author Fabien Potencier <fabien@symfony.com>
25 class ApplicationTester
34 * @param Application $application An Application instance to test.
36 public function __construct(Application $application)
38 $this->application = $application;
42 * Executes the application.
46 * * interactive: Sets the input interactive flag
47 * * decorated: Sets the output decorated flag
48 * * verbosity: Sets the output verbosity flag
50 * @param array $input An array of arguments and options
51 * @param array $options An array of options
53 * @return integer The command exit code
55 public function run(array $input, $options = array())
57 $this->input = new ArrayInput($input);
58 if (isset($options['interactive'])) {
59 $this->input->setInteractive($options['interactive']);
62 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
63 if (isset($options['decorated'])) {
64 $this->output->setDecorated($options['decorated']);
66 if (isset($options['verbosity'])) {
67 $this->output->setVerbosity($options['verbosity']);
70 return $this->application->run($this->input, $this->output);
74 * Gets the display returned by the last execution of the application.
76 * @return string The display
78 public function getDisplay()
80 rewind($this->output->getStream());
82 return stream_get_contents($this->output->getStream());
86 * Gets the input instance used by the last execution of the application.
88 * @return InputInterface The current input instance
90 public function getInput()
96 * Gets the output instance used by the last execution of the application.
98 * @return OutputInterface The current output instance
100 public function getOutput()
102 return $this->output;