Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Console\Tester;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Console\Command\Command;
|
Chris@0
|
15 use Symfony\Component\Console\Input\ArrayInput;
|
Chris@0
|
16 use Symfony\Component\Console\Output\StreamOutput;
|
Chris@0
|
17 use Symfony\Component\Console\Input\InputInterface;
|
Chris@0
|
18 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Eases the testing of console commands.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
24 * @author Robin Chalas <robin.chalas@gmail.com>
|
Chris@0
|
25 */
|
Chris@0
|
26 class CommandTester
|
Chris@0
|
27 {
|
Chris@0
|
28 private $command;
|
Chris@0
|
29 private $input;
|
Chris@0
|
30 private $output;
|
Chris@0
|
31 private $inputs = array();
|
Chris@0
|
32 private $statusCode;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Constructor.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param Command $command A Command instance to test
|
Chris@0
|
38 */
|
Chris@0
|
39 public function __construct(Command $command)
|
Chris@0
|
40 {
|
Chris@0
|
41 $this->command = $command;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Executes the command.
|
Chris@0
|
46 *
|
Chris@0
|
47 * Available execution options:
|
Chris@0
|
48 *
|
Chris@0
|
49 * * interactive: Sets the input interactive flag
|
Chris@0
|
50 * * decorated: Sets the output decorated flag
|
Chris@0
|
51 * * verbosity: Sets the output verbosity flag
|
Chris@0
|
52 *
|
Chris@0
|
53 * @param array $input An array of command arguments and options
|
Chris@0
|
54 * @param array $options An array of execution options
|
Chris@0
|
55 *
|
Chris@0
|
56 * @return int The command exit code
|
Chris@0
|
57 */
|
Chris@0
|
58 public function execute(array $input, array $options = array())
|
Chris@0
|
59 {
|
Chris@0
|
60 // set the command name automatically if the application requires
|
Chris@0
|
61 // this argument and no command name was passed
|
Chris@0
|
62 if (!isset($input['command'])
|
Chris@0
|
63 && (null !== $application = $this->command->getApplication())
|
Chris@0
|
64 && $application->getDefinition()->hasArgument('command')
|
Chris@0
|
65 ) {
|
Chris@0
|
66 $input = array_merge(array('command' => $this->command->getName()), $input);
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 $this->input = new ArrayInput($input);
|
Chris@0
|
70 if ($this->inputs) {
|
Chris@0
|
71 $this->input->setStream(self::createStream($this->inputs));
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 if (isset($options['interactive'])) {
|
Chris@0
|
75 $this->input->setInteractive($options['interactive']);
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
|
Chris@0
|
79 $this->output->setDecorated(isset($options['decorated']) ? $options['decorated'] : false);
|
Chris@0
|
80 if (isset($options['verbosity'])) {
|
Chris@0
|
81 $this->output->setVerbosity($options['verbosity']);
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 return $this->statusCode = $this->command->run($this->input, $this->output);
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * Gets the display returned by the last execution of the command.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @param bool $normalize Whether to normalize end of lines to \n or not
|
Chris@0
|
91 *
|
Chris@0
|
92 * @return string The display
|
Chris@0
|
93 */
|
Chris@0
|
94 public function getDisplay($normalize = false)
|
Chris@0
|
95 {
|
Chris@0
|
96 rewind($this->output->getStream());
|
Chris@0
|
97
|
Chris@0
|
98 $display = stream_get_contents($this->output->getStream());
|
Chris@0
|
99
|
Chris@0
|
100 if ($normalize) {
|
Chris@0
|
101 $display = str_replace(PHP_EOL, "\n", $display);
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 return $display;
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * Gets the input instance used by the last execution of the command.
|
Chris@0
|
109 *
|
Chris@0
|
110 * @return InputInterface The current input instance
|
Chris@0
|
111 */
|
Chris@0
|
112 public function getInput()
|
Chris@0
|
113 {
|
Chris@0
|
114 return $this->input;
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * Gets the output instance used by the last execution of the command.
|
Chris@0
|
119 *
|
Chris@0
|
120 * @return OutputInterface The current output instance
|
Chris@0
|
121 */
|
Chris@0
|
122 public function getOutput()
|
Chris@0
|
123 {
|
Chris@0
|
124 return $this->output;
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * Gets the status code returned by the last execution of the application.
|
Chris@0
|
129 *
|
Chris@0
|
130 * @return int The status code
|
Chris@0
|
131 */
|
Chris@0
|
132 public function getStatusCode()
|
Chris@0
|
133 {
|
Chris@0
|
134 return $this->statusCode;
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 /**
|
Chris@0
|
138 * Sets the user inputs.
|
Chris@0
|
139 *
|
Chris@0
|
140 * @param array An array of strings representing each input
|
Chris@0
|
141 * passed to the command input stream.
|
Chris@0
|
142 *
|
Chris@0
|
143 * @return CommandTester
|
Chris@0
|
144 */
|
Chris@0
|
145 public function setInputs(array $inputs)
|
Chris@0
|
146 {
|
Chris@0
|
147 $this->inputs = $inputs;
|
Chris@0
|
148
|
Chris@0
|
149 return $this;
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 private static function createStream(array $inputs)
|
Chris@0
|
153 {
|
Chris@0
|
154 $stream = fopen('php://memory', 'r+', false);
|
Chris@0
|
155
|
Chris@0
|
156 fwrite($stream, implode(PHP_EOL, $inputs));
|
Chris@0
|
157 rewind($stream);
|
Chris@0
|
158
|
Chris@0
|
159 return $stream;
|
Chris@0
|
160 }
|
Chris@0
|
161 }
|