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