These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php declare(strict_types=1); |
||
2 | /* |
||
3 | * This file is part of the KleijnWeb\SwaggerBundle package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | */ |
||
8 | |||
9 | namespace KleijnWeb\SwaggerBundle\Test; |
||
10 | |||
11 | use Symfony\Bundle\FrameworkBundle\Client; |
||
12 | use Symfony\Component\BrowserKit\CookieJar; |
||
13 | use Symfony\Component\BrowserKit\History; |
||
14 | use Symfony\Component\BrowserKit\Request as BrowserKitRequest; |
||
15 | use Symfony\Component\BrowserKit\Response as BrowserKitResponse; |
||
16 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
17 | use Symfony\Component\DomCrawler\Crawler; |
||
18 | use Symfony\Component\DomCrawler\Form; |
||
19 | use Symfony\Component\DomCrawler\Link; |
||
20 | use Symfony\Component\HttpFoundation\Request; |
||
21 | use Symfony\Component\HttpFoundation\Response; |
||
22 | use Symfony\Component\HttpKernel\KernelInterface; |
||
23 | use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile; |
||
24 | |||
25 | /** |
||
26 | * @author John Kleijn <[email protected]> |
||
27 | * @codeCoverageIgnore |
||
28 | */ |
||
29 | class ApiTestClient extends Client |
||
30 | { |
||
31 | /** |
||
32 | * @var Client |
||
33 | */ |
||
34 | private $subject; |
||
35 | |||
36 | /** @noinspection PhpMissingParentConstructorInspection */ |
||
37 | |||
38 | /** |
||
39 | * ApiTestClient constructor. |
||
40 | * |
||
41 | * @param Client $subject |
||
42 | */ |
||
43 | public function __construct(Client $subject) |
||
44 | { |
||
45 | $this->subject = $subject; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Makes a request from a Request object directly. |
||
50 | * |
||
51 | * @param BrowserKitRequest $request A Request instance |
||
52 | * @param bool $changeHistory Whether to update the history or not (only used internally for back(), |
||
53 | * forward(), and reload()) |
||
54 | * |
||
55 | * @return Crawler |
||
56 | */ |
||
57 | public function requestFromRequest(BrowserKitRequest $request, $changeHistory = true) |
||
58 | { |
||
59 | return $this->subject->requestFromRequest($request, $changeHistory); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Returns the container. |
||
64 | * |
||
65 | * @return ContainerInterface |
||
66 | */ |
||
67 | public function getContainer() |
||
68 | { |
||
69 | return $this->subject->getContainer(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns the kernel. |
||
74 | * |
||
75 | * @return KernelInterface |
||
76 | */ |
||
77 | public function getKernel() |
||
78 | { |
||
79 | return $this->subject->getKernel(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Gets the profile associated with the current Response. |
||
84 | * |
||
85 | * @return HttpProfile A Profile instance |
||
86 | */ |
||
87 | public function getProfile() |
||
88 | { |
||
89 | return $this->subject->getProfile(); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Enables the profiler for the very next request. |
||
94 | * |
||
95 | * If the profiler is not enabled, the call to this method does nothing. |
||
96 | */ |
||
97 | public function enableProfiler() |
||
98 | { |
||
99 | $this->subject->enableProfiler(); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | * |
||
105 | * @param Request $request A Request instance |
||
106 | * |
||
107 | * @return Response A Response instance |
||
108 | */ |
||
109 | protected function doRequest($request) |
||
110 | { |
||
111 | return $this->subject->doRequest($request); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | * |
||
117 | * @param Request $request A Request instance |
||
118 | * |
||
119 | * @return Response A Response instance |
||
120 | */ |
||
121 | protected function doRequestInProcess($request) |
||
122 | { |
||
123 | return $this->subject->doRequestInProcess($request); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Returns the script to execute when the request must be insulated. |
||
128 | * |
||
129 | * It assumes that the autoloader is named 'autoload.php' and that it is |
||
130 | * stored in the same directory as the kernel (this is the case for the |
||
131 | * Symfony Standard Edition). If this is not your case, create your own |
||
132 | * client and override this method. |
||
133 | * |
||
134 | * @param Request $request A Request instance |
||
135 | * |
||
136 | * @return string The script content |
||
137 | */ |
||
138 | protected function getScript($request) |
||
139 | { |
||
140 | return $this->subject->getScript($request); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Sets whether to automatically follow redirects or not. |
||
145 | * |
||
146 | * @param bool $followRedirect Whether to follow redirects |
||
147 | * |
||
148 | * @api |
||
149 | */ |
||
150 | public function followRedirects($followRedirect = true) |
||
151 | { |
||
152 | $this->subject->followRedirects($followRedirect); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Sets the maximum number of requests that crawler can follow. |
||
157 | * |
||
158 | * @param int $maxRedirects |
||
159 | */ |
||
160 | public function setMaxRedirects($maxRedirects) |
||
161 | { |
||
162 | $this->subject->setMaxRedirects($maxRedirects); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Sets the insulated flag. |
||
167 | * |
||
168 | * @param bool $insulated Whether to insulate the requests or not |
||
169 | * |
||
170 | * @throws \RuntimeException When Symfony Process Component is not installed |
||
171 | * |
||
172 | * @api |
||
173 | */ |
||
174 | public function insulate($insulated = true) |
||
175 | { |
||
176 | $this->subject->insulate($insulated); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Sets server parameters. |
||
181 | * |
||
182 | * @param array $server An array of server parameters |
||
183 | * |
||
184 | * @api |
||
185 | */ |
||
186 | public function setServerParameters(array $server) |
||
187 | { |
||
188 | $this->subject->setServerParameters($server); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Sets single server parameter. |
||
193 | * |
||
194 | * @param string $key A key of the parameter |
||
195 | * @param string $value A value of the parameter |
||
196 | */ |
||
197 | public function setServerParameter($key, $value) |
||
198 | { |
||
199 | $this->subject->setServerParameter($key, $value); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Gets single server parameter for specified key. |
||
204 | * |
||
205 | * @param string $key A key of the parameter to get |
||
206 | * @param string $default A default value when key is undefined |
||
207 | * |
||
208 | * @return string A value of the parameter |
||
209 | */ |
||
210 | public function getServerParameter($key, $default = '') |
||
211 | { |
||
212 | return $this->subject->getServerParameter($key, $default); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Returns the History instance. |
||
217 | * |
||
218 | * @return History A History instance |
||
219 | * |
||
220 | * @api |
||
221 | */ |
||
222 | public function getHistory() |
||
223 | { |
||
224 | return $this->subject->getHistory(); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Returns the CookieJar instance. |
||
229 | * |
||
230 | * @return CookieJar A CookieJar instance |
||
231 | * |
||
232 | * @api |
||
233 | */ |
||
234 | public function getCookieJar() |
||
235 | { |
||
236 | return $this->subject->getCookieJar(); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Returns the current Crawler instance. |
||
241 | * |
||
242 | * @return Crawler|null A Crawler instance |
||
243 | * |
||
244 | * @api |
||
245 | */ |
||
246 | public function getCrawler() |
||
247 | { |
||
248 | return $this->subject->getCrawler(); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Returns the current BrowserKit Response instance. |
||
253 | * |
||
254 | * @return BrowserKitResponse|null A BrowserKit Response instance |
||
255 | * |
||
256 | * @api |
||
257 | */ |
||
258 | public function getInternalResponse() |
||
259 | { |
||
260 | return $this->subject->getInternalResponse(); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Returns the current origin response instance. |
||
265 | * |
||
266 | * The origin response is the response instance that is returned |
||
267 | * by the code that handles requests. |
||
268 | * |
||
269 | * @return object|null A response instance |
||
270 | * |
||
271 | * @see doRequest() |
||
272 | * |
||
273 | * @api |
||
274 | */ |
||
275 | public function getResponse() |
||
276 | { |
||
277 | return $this->subject->getResponse(); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Returns the current BrowserKit Request instance. |
||
282 | * |
||
283 | * @return BrowserKitRequest|null A BrowserKit Request instance |
||
284 | * |
||
285 | * @api |
||
286 | */ |
||
287 | public function getInternalRequest() |
||
288 | { |
||
289 | return $this->subject->getInternalRequest(); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Returns the current origin Request instance. |
||
294 | * |
||
295 | * The origin request is the request instance that is sent |
||
296 | * to the code that handles requests. |
||
297 | * |
||
298 | * @return object|null A Request instance |
||
299 | * |
||
300 | * @see doRequest() |
||
301 | * |
||
302 | * @api |
||
303 | */ |
||
304 | public function getRequest() |
||
305 | { |
||
306 | return $this->subject->getRequest(); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Clicks on a given link. |
||
311 | * |
||
312 | * @param Link $link A Link instance |
||
313 | * |
||
314 | * @return Crawler |
||
315 | * |
||
316 | * @api |
||
317 | */ |
||
318 | public function click(Link $link) |
||
319 | { |
||
320 | return $this->subject->click($link); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Submits a form. |
||
325 | * |
||
326 | * @param Form $form A Form instance |
||
327 | * @param array $values An array of form field values |
||
328 | * |
||
329 | * @return Crawler |
||
330 | * |
||
331 | * @api |
||
332 | */ |
||
333 | public function submit(Form $form, array $values = []) |
||
334 | { |
||
335 | return $this->subject->submit($form, $values); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Calls a URI. |
||
340 | * |
||
341 | * @param string $method The request method |
||
342 | * @param string $uri The URI to fetch |
||
343 | * @param array $parameters The Request parameters |
||
344 | * @param array $files The files |
||
345 | * @param array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does) |
||
346 | * @param string $content The raw body data |
||
347 | * @param bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), |
||
348 | * and reload()) |
||
349 | * |
||
350 | * @return Crawler |
||
351 | * |
||
352 | * @api |
||
353 | */ |
||
354 | public function request( |
||
355 | $method, |
||
356 | $uri, |
||
357 | array $parameters = [], |
||
358 | array $files = [], |
||
359 | array $server = [], |
||
360 | $content = null, |
||
361 | $changeHistory = true |
||
362 | ) { |
||
363 | return $this->subject->request( |
||
364 | $method, |
||
365 | $uri, |
||
366 | $parameters, |
||
367 | $files, |
||
368 | $server, |
||
369 | $content, |
||
370 | $changeHistory |
||
371 | ); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Filters the BrowserKit request to the origin one. |
||
376 | * |
||
377 | * @param BrowserKitRequest $request The BrowserKit Request to filter |
||
378 | * |
||
379 | * @return object An origin request instance |
||
380 | */ |
||
381 | protected function filterRequest(BrowserKitRequest $request) |
||
382 | { |
||
383 | return $this->subject->filterRequest($request); |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Filters the origin response to the BrowserKit one. |
||
388 | * |
||
389 | * @param Response $response The origin response to filter |
||
390 | * |
||
391 | * @return BrowserKitResponse An BrowserKit Response instance |
||
392 | */ |
||
393 | protected function filterResponse($response) |
||
394 | { |
||
395 | return $this->subject->filterResponse($response); |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Creates a crawler. |
||
400 | * |
||
401 | * This method returns null if the DomCrawler component is not available. |
||
402 | * |
||
403 | * @param string $uri A URI |
||
404 | * @param string $content Content for the crawler to use |
||
405 | * @param string $type Content type |
||
406 | * |
||
407 | * @return Crawler|null |
||
408 | */ |
||
409 | protected function createCrawlerFromContent($uri, $content, $type) |
||
410 | { |
||
411 | return $this->subject->createCrawlerFromContent($uri, $content, $type); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Goes back in the browser history. |
||
416 | * |
||
417 | * @return Crawler |
||
418 | * |
||
419 | * @api |
||
420 | */ |
||
421 | public function back() |
||
422 | { |
||
423 | return $this->subject->back(); |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Goes forward in the browser history. |
||
428 | * |
||
429 | * @return Crawler |
||
430 | * |
||
431 | * @api |
||
432 | */ |
||
433 | public function forward() |
||
434 | { |
||
435 | return $this->subject->forward(); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Reloads the current browser. |
||
440 | * |
||
441 | * @return Crawler |
||
442 | * |
||
443 | * @api |
||
444 | */ |
||
445 | public function reload() |
||
446 | { |
||
447 | return $this->subject->reload(); |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * Follow redirects? |
||
452 | * |
||
453 | * @return Crawler |
||
454 | * |
||
455 | * @throws \LogicException If request was not a redirect |
||
456 | * |
||
457 | * @api |
||
458 | */ |
||
459 | public function followRedirect() |
||
460 | { |
||
461 | return $this->subject->followRedirect(); |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Restarts the client. |
||
466 | * |
||
467 | * It flushes history and all cookies. |
||
468 | * |
||
469 | * @api |
||
470 | */ |
||
471 | public function restart() |
||
472 | { |
||
473 | $this->subject->restart(); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Takes a URI and converts it to absolute if it is not already absolute. |
||
478 | * |
||
479 | * @param string $uri A URI |
||
480 | * |
||
481 | * @return string An absolute URI |
||
482 | */ |
||
483 | protected function getAbsoluteUri($uri) |
||
484 | { |
||
485 | return $this->subject->getAbsoluteUri($uri); |
||
486 | } |
||
487 | } |
||
488 |