1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test; |
15
|
|
|
|
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Client as FrameworkBundleClient; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
18
|
|
|
use Symfony\Component\HttpClient\HttpClientTrait; |
19
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile; |
21
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
22
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
23
|
|
|
use Symfony\Contracts\HttpClient\ResponseStreamInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Convenient test client that makes requests to a Kernel object. |
27
|
|
|
* |
28
|
|
|
* @experimental |
29
|
|
|
* |
30
|
|
|
* @author Kévin Dunglas <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
final class Client implements HttpClientInterface |
33
|
|
|
{ |
34
|
|
|
public const OPTIONS_DEFAULT = [ |
35
|
|
|
'auth_basic' => null, // array|string - an array containing the username as first value, and optionally the |
36
|
|
|
// password as the second one; or string like username:password - enabling HTTP Basic |
37
|
|
|
// authentication (RFC 7617) |
38
|
|
|
'auth_bearer' => null, // string - a token enabling HTTP Bearer authorization (RFC 6750) |
39
|
|
|
'query' => [], // string[] - associative array of query string values to merge with the request's URL |
40
|
|
|
'headers' => ['accept' => ['application/ld+json']], // iterable|string[]|string[][] - headers names provided as keys or as part of values |
41
|
|
|
'body' => '', // array|string|resource|\Traversable|\Closure - the callback SHOULD yield a string |
42
|
|
|
'json' => null, // array|\JsonSerializable - when set, implementations MUST set the "body" option to |
43
|
|
|
// the JSON-encoded value and set the "content-type" headers to a JSON-compatible |
44
|
|
|
'base_uri' => 'http://example.com', // string - the URI to resolve relative URLs, following rules in RFC 3986, section 2 |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
use HttpClientTrait; |
48
|
|
|
|
49
|
|
|
private $fwbClient; |
50
|
|
|
|
51
|
|
|
public function __construct(FrameworkBundleClient $fwbClient) |
52
|
|
|
{ |
53
|
|
|
$this->fwbClient = $fwbClient; |
54
|
|
|
$fwbClient->followRedirects(false); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @see Client::OPTIONS_DEFAULTS for available options |
59
|
|
|
* |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function request(string $method, string $url, array $options = []): ResponseInterface |
63
|
|
|
{ |
64
|
|
|
$basic = $options['auth_basic'] ?? null; |
65
|
|
|
[$url, $options] = $this->prepareRequest($method, $url, $options, self::OPTIONS_DEFAULT); |
66
|
|
|
|
67
|
|
|
$server = []; |
68
|
|
|
// Convert headers to a $_SERVER-like array |
69
|
|
|
foreach ($options['headers'] as $key => $value) { |
70
|
|
|
if ('content-type' === $key) { |
71
|
|
|
$server['CONTENT_TYPE'] = $value[0] ?? ''; |
72
|
|
|
|
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// BrowserKit doesn't support setting several headers with the same name |
77
|
|
|
$server['HTTP_'.strtoupper(str_replace('-', '_', $key))] = $value[0] ?? ''; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($basic) { |
81
|
|
|
$credentials = \is_array($basic) ? $basic : explode(':', $basic, 2); |
82
|
|
|
$server['PHP_AUTH_USER'] = $credentials[0]; |
83
|
|
|
$server['PHP_AUTH_PW'] = $credentials[1] ?? ''; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$info = [ |
87
|
|
|
'redirect_count' => 0, |
88
|
|
|
'redirect_url' => null, |
89
|
|
|
'http_method' => $method, |
90
|
|
|
'start_time' => microtime(true), |
91
|
|
|
'data' => $options['data'] ?? null, |
92
|
|
|
'url' => $url, |
93
|
|
|
]; |
94
|
|
|
$this->fwbClient->request($method, implode('', $url), [], [], $server, $options['body'] ?? null); |
95
|
|
|
|
96
|
|
|
return new Response($this->fwbClient->getResponse(), $this->fwbClient->getInternalResponse(), $info); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function stream($responses, float $timeout = null): ResponseStreamInterface |
103
|
|
|
{ |
104
|
|
|
throw new \LogicException('Not implemented yet'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Gets the underlying test client. |
109
|
|
|
*/ |
110
|
|
|
public function getFrameworkBundleClient(): FrameworkBundleClient |
111
|
|
|
{ |
112
|
|
|
return $this->fwbClient; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// The following methods are proxy methods for FrameworkBundleClient's ones |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns the container. |
119
|
|
|
* |
120
|
|
|
* @return ContainerInterface|null Returns null when the Kernel has been shutdown or not started yet |
121
|
|
|
*/ |
122
|
|
|
public function getContainer() |
123
|
|
|
{ |
124
|
|
|
return $this->fwbClient->getContainer(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the kernel. |
129
|
|
|
* |
130
|
|
|
* @return KernelInterface |
131
|
|
|
*/ |
132
|
|
|
public function getKernel() |
133
|
|
|
{ |
134
|
|
|
return $this->fwbClient->getKernel(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Gets the profile associated with the current Response. |
139
|
|
|
* |
140
|
|
|
* @return HttpProfile|false A Profile instance |
141
|
|
|
*/ |
142
|
|
|
public function getProfile() |
143
|
|
|
{ |
144
|
|
|
return $this->fwbClient->getProfile(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Enables the profiler for the very next request. |
149
|
|
|
* |
150
|
|
|
* If the profiler is not enabled, the call to this method does nothing. |
151
|
|
|
*/ |
152
|
|
|
public function enableProfiler() |
153
|
|
|
{ |
154
|
|
|
$this->fwbClient->enableProfiler(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Disables kernel reboot between requests. |
159
|
|
|
* |
160
|
|
|
* By default, the Client reboots the Kernel for each request. This method |
161
|
|
|
* allows to keep the same kernel across requests. |
162
|
|
|
*/ |
163
|
|
|
public function disableReboot() |
164
|
|
|
{ |
165
|
|
|
$this->fwbClient->disableReboot(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Enables kernel reboot between requests. |
170
|
|
|
*/ |
171
|
|
|
public function enableReboot() |
172
|
|
|
{ |
173
|
|
|
$this->fwbClient->enableReboot(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|