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\Input\InputInterface;
|
Chris@0
|
17 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@17
|
18 use Symfony\Component\Console\Output\StreamOutput;
|
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@17
|
31 private $inputs = [];
|
Chris@0
|
32 private $statusCode;
|
Chris@0
|
33
|
Chris@0
|
34 public function __construct(Command $command)
|
Chris@0
|
35 {
|
Chris@0
|
36 $this->command = $command;
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Executes the command.
|
Chris@0
|
41 *
|
Chris@0
|
42 * Available execution options:
|
Chris@0
|
43 *
|
Chris@0
|
44 * * interactive: Sets the input interactive flag
|
Chris@0
|
45 * * decorated: Sets the output decorated flag
|
Chris@0
|
46 * * verbosity: Sets the output verbosity flag
|
Chris@0
|
47 *
|
Chris@0
|
48 * @param array $input An array of command arguments and options
|
Chris@0
|
49 * @param array $options An array of execution options
|
Chris@0
|
50 *
|
Chris@0
|
51 * @return int The command exit code
|
Chris@0
|
52 */
|
Chris@17
|
53 public function execute(array $input, array $options = [])
|
Chris@0
|
54 {
|
Chris@0
|
55 // set the command name automatically if the application requires
|
Chris@0
|
56 // this argument and no command name was passed
|
Chris@0
|
57 if (!isset($input['command'])
|
Chris@0
|
58 && (null !== $application = $this->command->getApplication())
|
Chris@0
|
59 && $application->getDefinition()->hasArgument('command')
|
Chris@0
|
60 ) {
|
Chris@17
|
61 $input = array_merge(['command' => $this->command->getName()], $input);
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 $this->input = new ArrayInput($input);
|
Chris@0
|
65 if ($this->inputs) {
|
Chris@0
|
66 $this->input->setStream(self::createStream($this->inputs));
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 if (isset($options['interactive'])) {
|
Chris@0
|
70 $this->input->setInteractive($options['interactive']);
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
|
Chris@0
|
74 $this->output->setDecorated(isset($options['decorated']) ? $options['decorated'] : false);
|
Chris@0
|
75 if (isset($options['verbosity'])) {
|
Chris@0
|
76 $this->output->setVerbosity($options['verbosity']);
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 return $this->statusCode = $this->command->run($this->input, $this->output);
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Gets the display returned by the last execution of the command.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @param bool $normalize Whether to normalize end of lines to \n or not
|
Chris@0
|
86 *
|
Chris@0
|
87 * @return string The display
|
Chris@0
|
88 */
|
Chris@0
|
89 public function getDisplay($normalize = false)
|
Chris@0
|
90 {
|
Chris@0
|
91 rewind($this->output->getStream());
|
Chris@0
|
92
|
Chris@0
|
93 $display = stream_get_contents($this->output->getStream());
|
Chris@0
|
94
|
Chris@0
|
95 if ($normalize) {
|
Chris@0
|
96 $display = str_replace(PHP_EOL, "\n", $display);
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 return $display;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * Gets the input instance used by the last execution of the command.
|
Chris@0
|
104 *
|
Chris@0
|
105 * @return InputInterface The current input instance
|
Chris@0
|
106 */
|
Chris@0
|
107 public function getInput()
|
Chris@0
|
108 {
|
Chris@0
|
109 return $this->input;
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * Gets the output instance used by the last execution of the command.
|
Chris@0
|
114 *
|
Chris@0
|
115 * @return OutputInterface The current output instance
|
Chris@0
|
116 */
|
Chris@0
|
117 public function getOutput()
|
Chris@0
|
118 {
|
Chris@0
|
119 return $this->output;
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 /**
|
Chris@0
|
123 * Gets the status code returned by the last execution of the application.
|
Chris@0
|
124 *
|
Chris@0
|
125 * @return int The status code
|
Chris@0
|
126 */
|
Chris@0
|
127 public function getStatusCode()
|
Chris@0
|
128 {
|
Chris@0
|
129 return $this->statusCode;
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * Sets the user inputs.
|
Chris@0
|
134 *
|
Chris@14
|
135 * @param array $inputs An array of strings representing each input
|
Chris@14
|
136 * passed to the command input stream
|
Chris@0
|
137 *
|
Chris@0
|
138 * @return CommandTester
|
Chris@0
|
139 */
|
Chris@0
|
140 public function setInputs(array $inputs)
|
Chris@0
|
141 {
|
Chris@0
|
142 $this->inputs = $inputs;
|
Chris@0
|
143
|
Chris@0
|
144 return $this;
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 private static function createStream(array $inputs)
|
Chris@0
|
148 {
|
Chris@0
|
149 $stream = fopen('php://memory', 'r+', false);
|
Chris@0
|
150
|
Chris@17
|
151 foreach ($inputs as $input) {
|
Chris@17
|
152 fwrite($stream, $input.PHP_EOL);
|
Chris@17
|
153 }
|
Chris@17
|
154
|
Chris@0
|
155 rewind($stream);
|
Chris@0
|
156
|
Chris@0
|
157 return $stream;
|
Chris@0
|
158 }
|
Chris@0
|
159 }
|