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