1 | <?php |
||
32 | class RawGuzzleContext implements GuzzleAwareContext |
||
33 | { |
||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | const GUZZLE_EXTENSION_NAME = 'Behat Guzzle Extension'; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | const GUZZLE_EXTENSION_VERSION = '0.4.2'; |
||
43 | |||
44 | /** |
||
45 | * @var Client |
||
46 | * |
||
47 | * @access private |
||
48 | */ |
||
49 | private $client; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | * |
||
54 | * @access private |
||
55 | */ |
||
56 | private $parameters; |
||
57 | |||
58 | /** |
||
59 | * @var Response |
||
60 | * |
||
61 | * @access private |
||
62 | */ |
||
63 | private $response; |
||
64 | |||
65 | /** |
||
66 | * @var mixed |
||
67 | * |
||
68 | * @access private |
||
69 | */ |
||
70 | private $result; |
||
71 | |||
72 | /** |
||
73 | * Execute command |
||
74 | * |
||
75 | * @param string $command Command to execute |
||
76 | * @param array $data Data to send |
||
77 | * |
||
78 | * @access protected |
||
79 | * @return void |
||
80 | */ |
||
81 | 13 | public function executeCommand($command, array $data = array()) |
|
82 | { |
||
83 | 13 | $this->getGuzzleClient()->setUserAgent( |
|
84 | 13 | self::GUZZLE_EXTENSION_NAME . '/' . self::GUZZLE_EXTENSION_VERSION |
|
85 | 13 | ); |
|
86 | |||
87 | 13 | $command = $this->getGuzzleClient()->getCommand($command, $data); |
|
88 | |||
89 | try { |
||
90 | 13 | $result = $this->getGuzzleClient()->execute($command); |
|
91 | 13 | } catch (BadResponseException $e) { |
|
92 | 1 | $this->response = $e->getResponse(); |
|
93 | |||
94 | try { |
||
95 | 1 | $this->result = $this->response->json(); |
|
96 | |||
97 | 1 | return; |
|
98 | } catch (RuntimeException $e) { |
||
99 | // Do nothing |
||
100 | } |
||
101 | |||
102 | try { |
||
103 | $this->result = $this->response->xml(); |
||
104 | |||
105 | return; |
||
106 | } catch (RuntimeException $e) { |
||
107 | // Do nothing |
||
108 | } |
||
109 | |||
110 | return; |
||
111 | } |
||
112 | |||
113 | 12 | $this->response = $command->getResponse(); |
|
114 | 12 | $this->result = $result; |
|
115 | 12 | } |
|
116 | |||
117 | /** |
||
118 | * Returns Client instance |
||
119 | * |
||
120 | * @access public |
||
121 | * @return Client |
||
122 | */ |
||
123 | 16 | public function getGuzzleClient() |
|
134 | |||
135 | /** |
||
136 | * Sets Client instance |
||
137 | * |
||
138 | * @param Client $client Guzzle client |
||
139 | * |
||
140 | * @access public |
||
141 | * @return void |
||
142 | */ |
||
143 | 15 | public function setGuzzleClient(Client $client) |
|
147 | |||
148 | /** |
||
149 | * Add Guzzle header |
||
150 | * |
||
151 | * @param string $field Field name |
||
152 | * @param string $value Header value |
||
153 | * |
||
154 | * @access public |
||
155 | * @return void |
||
156 | */ |
||
157 | 2 | public function addGuzzleHeader($field, $value) |
|
161 | |||
162 | /** |
||
163 | * Remove Guzzle header |
||
164 | * |
||
165 | * @param string $field Field name |
||
166 | * |
||
167 | * @access public |
||
168 | * @return void |
||
169 | */ |
||
170 | 1 | public function removeGuzzleHeader($field) |
|
174 | |||
175 | /** |
||
176 | * Returns specific Guzzle parameter |
||
177 | * |
||
178 | * @param string $name |
||
179 | * |
||
180 | * @access public |
||
181 | * @return mixed |
||
182 | */ |
||
183 | 1 | public function getGuzzleParameter($name) |
|
189 | |||
190 | /** |
||
191 | * Applies the given parameter to the Guzzle configuration. Consider that |
||
192 | * all parameters get reset for each feature context |
||
193 | * |
||
194 | * @param string $name The key of the parameter |
||
195 | * @param string $value The value of the parameter |
||
196 | * |
||
197 | * @access public |
||
198 | * @return void |
||
199 | */ |
||
200 | 1 | public function setGuzzleParameter($name, $value) |
|
204 | |||
205 | /** |
||
206 | * Returns the parameters provided for Guzzle |
||
207 | * |
||
208 | * @access public |
||
209 | * @return array |
||
210 | */ |
||
211 | 1 | public function getGuzzleParameters() |
|
215 | |||
216 | /** |
||
217 | * Sets parameters provided for Guzzle |
||
218 | * |
||
219 | * @param array $parameters |
||
220 | * |
||
221 | * @access public |
||
222 | * @return void |
||
223 | */ |
||
224 | 1 | public function setGuzzleParameters(array $parameters) |
|
228 | |||
229 | /** |
||
230 | * Returns Response instance |
||
231 | * |
||
232 | * @access public |
||
233 | * @return Response |
||
234 | */ |
||
235 | 5 | public function getGuzzleResponse() |
|
239 | |||
240 | /** |
||
241 | * Returns result |
||
242 | * |
||
243 | * @access public |
||
244 | * @return mixed |
||
245 | */ |
||
246 | 8 | public function getGuzzleResult() |
|
250 | |||
251 | /** |
||
252 | * Compare array values |
||
253 | * |
||
254 | * @param array $input Array with values to compare |
||
255 | * @param array $control Array with correct values |
||
256 | * |
||
257 | * @access protected |
||
258 | * @return void |
||
259 | */ |
||
260 | 7 | protected function compareArrays(array $input, array $control) |
|
278 | |||
279 | /** |
||
280 | * Compare array values |
||
281 | * |
||
282 | * @param mixed $input Input value |
||
283 | * @param mixed $control Correct control value |
||
284 | * |
||
285 | * @throws Exception When two field values do not match |
||
286 | * |
||
287 | * @access protected |
||
288 | * @return void |
||
289 | */ |
||
290 | 8 | protected function compareValues($input, $control) |
|
303 | |||
304 | /** |
||
305 | * Get request options |
||
306 | * |
||
307 | * @access private |
||
308 | * @return array |
||
309 | */ |
||
310 | 2 | private function getRequestOptions() |
|
322 | |||
323 | /** |
||
324 | * Set request options |
||
325 | * |
||
326 | * @param array $options Request options |
||
327 | * |
||
328 | * @access private |
||
329 | * @return void |
||
330 | */ |
||
331 | 2 | private function setRequestOptions(array $options) |
|
338 | |||
339 | /** |
||
340 | * Update header |
||
341 | * |
||
342 | * Adds, updates or removes header (if no value is provided) |
||
343 | * |
||
344 | * @param string $field Field name |
||
345 | * @param string $value Header value |
||
|
|||
346 | * |
||
347 | * @access private |
||
348 | * @return void |
||
349 | */ |
||
350 | 2 | private function updateHeader($field, $value = null) |
|
362 | } |
||
363 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.