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\Command\Command;
15: use Symfony\Component\Console\Input\ArrayInput;
16: use Symfony\Component\Console\Output\StreamOutput;
17:
18: /**
19: * Eases the testing of console commands.
20: *
21: * @author Fabien Potencier <fabien@symfony.com>
22: */
23: class CommandTester
24: {
25: private $command;
26: private $input;
27: private $output;
28:
29: /**
30: * Constructor.
31: *
32: * @param Command $command A Command instance to test.
33: */
34: public function __construct(Command $command)
35: {
36: $this->command = $command;
37: }
38:
39: /**
40: * Executes the command.
41: *
42: * Available options:
43: *
44: * * interactive: Sets the input interactive flag
45: * * decorated: Sets the output decorated flag
46: * * verbosity: Sets the output verbosity flag
47: *
48: * @param array $input An array of arguments and options
49: * @param array $options An array of options
50: *
51: * @return integer The command exit code
52: */
53: public function execute(array $input, array $options = array())
54: {
55: $this->input = new ArrayInput($input);
56: if (isset($options['interactive'])) {
57: $this->input->setInteractive($options['interactive']);
58: }
59:
60: $this->output = new StreamOutput(fopen('php://memory', 'w', false));
61: if (isset($options['decorated'])) {
62: $this->output->setDecorated($options['decorated']);
63: }
64: if (isset($options['verbosity'])) {
65: $this->output->setVerbosity($options['verbosity']);
66: }
67:
68: return $this->command->run($this->input, $this->output);
69: }
70:
71: /**
72: * Gets the display returned by the last execution of the command.
73: *
74: * @return string The display
75: */
76: public function getDisplay()
77: {
78: rewind($this->output->getStream());
79:
80: return stream_get_contents($this->output->getStream());
81: }
82:
83: /**
84: * Gets the input instance used by the last execution of the command.
85: *
86: * @return InputInterface The current input instance
87: */
88: public function getInput()
89: {
90: return $this->input;
91: }
92:
93: /**
94: * Gets the output instance used by the last execution of the command.
95: *
96: * @return OutputInterface The current output instance
97: */
98: public function getOutput()
99: {
100: return $this->output;
101: }
102: }
103: