These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * The MIT License (MIT) |
||
7 | * |
||
8 | * Copyright (c) 2014-2017 Spomky-Labs |
||
9 | * |
||
10 | * This software may be modified and distributed under the terms |
||
11 | * of the MIT license. See the LICENSE file for details. |
||
12 | */ |
||
13 | |||
14 | namespace Jose\Test\Context; |
||
15 | |||
16 | use Behat\Gherkin\Node\PyStringNode; |
||
17 | use Symfony\Bundle\FrameworkBundle\Console\Application; |
||
18 | use Symfony\Component\Console\Tester\CommandTester; |
||
19 | use Behat\Behat\Context\Context; |
||
20 | use Behat\Symfony2Extension\Context\KernelDictionary; |
||
21 | |||
22 | final class CommandContext implements Context |
||
23 | { |
||
24 | use KernelDictionary; |
||
25 | |||
26 | /** |
||
27 | * @var null |
||
28 | */ |
||
29 | private $application = null; |
||
30 | |||
31 | /** |
||
32 | * @var null|string |
||
33 | */ |
||
34 | private $command_output = null; |
||
35 | |||
36 | /** |
||
37 | * @var null|\Exception |
||
38 | */ |
||
39 | private $command_exception = null; |
||
40 | |||
41 | /** |
||
42 | * @var null|int |
||
43 | */ |
||
44 | private $command_exit_code = null; |
||
45 | |||
46 | /** |
||
47 | * @param array $command_parameters |
||
48 | */ |
||
49 | public function setCommandParameters($command_parameters) |
||
50 | { |
||
51 | $this->command_parameters = $command_parameters; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param int|null $command_exit_code |
||
56 | */ |
||
57 | public function setCommandExitCode($command_exit_code) |
||
58 | { |
||
59 | $this->command_exit_code = $command_exit_code; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param \Exception|null $command_exception |
||
64 | */ |
||
65 | public function setCommandException($command_exception) |
||
66 | { |
||
67 | $this->command_exception = $command_exception; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | private $command_parameters = []; |
||
74 | |||
75 | /** |
||
76 | * @param string $command_output |
||
77 | */ |
||
78 | protected function setCommandOutput($command_output) |
||
79 | { |
||
80 | $this->command_output = $command_output; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return null|string |
||
85 | */ |
||
86 | protected function getCommandOutput() |
||
87 | { |
||
88 | return $this->command_output; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return null|array |
||
93 | */ |
||
94 | protected function getCommandParameters() |
||
95 | { |
||
96 | return $this->command_parameters; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return null|\Exception |
||
101 | */ |
||
102 | protected function getCommandException() |
||
103 | { |
||
104 | return $this->command_exception; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return null|int |
||
109 | */ |
||
110 | protected function getCommandExitCode() |
||
111 | { |
||
112 | return $this->command_exit_code; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return Application |
||
117 | */ |
||
118 | protected function getApplication() |
||
119 | { |
||
120 | if (null === $this->application) { |
||
121 | $this->application = new Application($this->getKernel()); |
||
0 ignored issues
–
show
|
|||
122 | } |
||
123 | |||
124 | return $this->application; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @Given I wait :time seconds |
||
129 | */ |
||
130 | public function iWaitSeconds($time) |
||
131 | { |
||
132 | sleep((int) $time); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @When I run command :line |
||
137 | */ |
||
138 | public function iRunCommand($line) |
||
139 | { |
||
140 | try { |
||
141 | $command = $this->getApplication()->find($line); |
||
142 | } catch (\Exception $e) { |
||
143 | $this->setCommandException($e); |
||
144 | |||
145 | return; |
||
146 | } |
||
147 | $tester = new CommandTester($command); |
||
148 | |||
149 | try { |
||
150 | $this->setCommandExitCode($tester->execute($this->getCommandParams($command))); |
||
151 | $this->setCommandException(null); |
||
152 | } catch (\Exception $e) { |
||
153 | $this->setCommandException($e); |
||
154 | $this->setCommandExitCode($e->getCode()); |
||
155 | } |
||
156 | $this->setCommandOutput($tester->getDisplay()); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @Given I run command :line with parameters |
||
161 | */ |
||
162 | public function iRunACommandWithParameters($line, PyStringNode $parameterJson) |
||
163 | { |
||
164 | $this->setCommandParameters(json_decode($parameterJson->getRaw(), true)); |
||
165 | if (null === $this->getCommandParameters()) { |
||
166 | throw new \InvalidArgumentException( |
||
167 | 'PyStringNode could not be converted to json.' |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | $this->iRunCommand($line); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @Then I should see |
||
176 | */ |
||
177 | public function iShouldSee(PyStringNode $result) |
||
178 | { |
||
179 | $output = $this->getCommandOutput(); |
||
180 | if ($result->getRaw() !== $output) { |
||
181 | throw new \InvalidArgumentException(sprintf('The output of the command is not the same as expected. I got "%".', $output)); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @Then I should see something like :pattern |
||
187 | */ |
||
188 | public function iShouldSeeSomethingLike($pattern) |
||
189 | { |
||
190 | $result = preg_match($pattern, $this->getCommandOutput(), $matches); |
||
191 | if (0 === $result) { |
||
192 | throw new \Exception(sprintf('The command output "%s" does not match with the pattern', $this->getCommandOutput())); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @Then The command exception should not be thrown |
||
198 | */ |
||
199 | public function theCommandExceptionShouldNotBeThrown() |
||
200 | { |
||
201 | if ($this->getCommandException() instanceof \Exception) { |
||
202 | throw new \Exception(sprintf('An exception was not thrown: "%s".', $this->getCommandException()->getMessage())); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @Then The command exception :exception should be thrown |
||
208 | */ |
||
209 | public function theCommandExceptionShouldBeThrown($exception) |
||
210 | { |
||
211 | if (!$this->getCommandException() instanceof $exception) { |
||
212 | throw new \Exception('The expected exception was not thrown.'); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @Then The command exit code should be :code |
||
218 | */ |
||
219 | public function theCommandExitCodeShouldBe($code) |
||
220 | { |
||
221 | if ($this->getCommandExitCode() !== (int) $code) { |
||
222 | throw new \Exception(sprintf('The exit code is %u.', $this->getCommandExitCode())); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @Then The command exception :exception with message should be thrown |
||
228 | */ |
||
229 | public function theCommandExceptionWithMessageShouldBeThrown($exception, PyStringNode $message) |
||
230 | { |
||
231 | $this->theCommandExceptionShouldBeThrown($exception); |
||
232 | if ($this->getCommandException()->getMessage() !== $message->getRaw()) { |
||
233 | throw new \Exception(sprintf('The message of the exception is "%s".', $this->getCommandException()->getMessage())); |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @Then The command exception :exception with message like :pattern should be thrown |
||
239 | */ |
||
240 | public function theCommandExceptionWithMessageLikeShouldBeThrown($exception, $pattern) |
||
241 | { |
||
242 | $this->theCommandExceptionShouldBeThrown($exception); |
||
243 | if (1 !== preg_match($pattern, $this->getCommandException()->getMessage())) { |
||
244 | throw new \Exception(sprintf('The message of the exception is "%s".', $this->getCommandException()->getMessage())); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param string $command |
||
250 | * |
||
251 | * @return array |
||
252 | */ |
||
253 | private function getCommandParams($command) |
||
254 | { |
||
255 | return array_merge( |
||
256 | $this->getCommandParameters(), |
||
257 | ['command' => $command] |
||
258 | ); |
||
259 | } |
||
260 | } |
||
261 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..