1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\Console\Tester;
13:
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;
19:
20: /**
21: * Eases the testing of console applications.
22: *
23: * When testing an application, don't forget to disable the auto exit flag:
24: *
25: * $application = new Application();
26: * $application->setAutoExit(false);
27: *
28: * @author Fabien Potencier <fabien@symfony.com>
29: */
30: class ApplicationTester
31: {
32: private $application;
33: private $input;
34: private $output;
35:
36: /**
37: * Constructor.
38: *
39: * @param Application $application An Application instance to test.
40: */
41: public function __construct(Application $application)
42: {
43: $this->application = $application;
44: }
45:
46: /**
47: * Executes the application.
48: *
49: * Available options:
50: *
51: * * interactive: Sets the input interactive flag
52: * * decorated: Sets the output decorated flag
53: * * verbosity: Sets the output verbosity flag
54: *
55: * @param array $input An array of arguments and options
56: * @param array $options An array of options
57: *
58: * @return integer The command exit code
59: */
60: public function run(array $input, $options = array())
61: {
62: $this->input = new ArrayInput($input);
63: if (isset($options['interactive'])) {
64: $this->input->setInteractive($options['interactive']);
65: }
66:
67: $this->output = new StreamOutput(fopen('php://memory', 'w', false));
68: if (isset($options['decorated'])) {
69: $this->output->setDecorated($options['decorated']);
70: }
71: if (isset($options['verbosity'])) {
72: $this->output->setVerbosity($options['verbosity']);
73: }
74:
75: return $this->application->run($this->input, $this->output);
76: }
77:
78: /**
79: * Gets the display returned by the last execution of the application.
80: *
81: * @return string The display
82: */
83: public function getDisplay()
84: {
85: rewind($this->output->getStream());
86:
87: return stream_get_contents($this->output->getStream());
88: }
89:
90: /**
91: * Gets the input instance used by the last execution of the application.
92: *
93: * @return InputInterface The current input instance
94: */
95: public function getInput()
96: {
97: return $this->input;
98: }
99:
100: /**
101: * Gets the output instance used by the last execution of the application.
102: *
103: * @return OutputInterface The current output instance
104: */
105: public function getOutput()
106: {
107: return $this->output;
108: }
109: }
110: