1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Mock; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\HttpAsyncClientEmulator; |
6
|
|
|
use Http\Client\Common\VersionBridgeClient; |
7
|
|
|
use Http\Client\Exception; |
8
|
|
|
use Http\Client\HttpAsyncClient; |
9
|
|
|
use Http\Client\HttpClient; |
10
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
11
|
|
|
use Http\Message\RequestMatcher; |
12
|
|
|
use Http\Message\ResponseFactory; |
13
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* An implementation of the HTTP client that is useful for automated tests. |
19
|
|
|
* |
20
|
|
|
* This mock does not send requests but stores them for later retrieval. |
21
|
|
|
* You can configure the mock with responses to return and/or exceptions to throw. |
22
|
|
|
* |
23
|
|
|
* @author David de Boer <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Client implements HttpClient, HttpAsyncClient |
26
|
|
|
{ |
27
|
|
|
use HttpAsyncClientEmulator; |
28
|
|
|
use VersionBridgeClient; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ResponseFactory |
32
|
|
|
*/ |
33
|
|
|
private $responseFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $conditionalResults = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var RequestInterface[] |
42
|
|
|
*/ |
43
|
|
|
private $requests = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ResponseInterface[] |
47
|
|
|
*/ |
48
|
|
|
private $responses = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var ResponseInterface|null |
52
|
|
|
*/ |
53
|
|
|
private $defaultResponse; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var Exception[] |
57
|
|
|
*/ |
58
|
|
|
private $exceptions = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var Exception|null |
62
|
|
|
*/ |
63
|
|
|
private $defaultException; |
64
|
|
|
|
65
|
15 |
|
public function __construct(ResponseFactory $responseFactory = null) |
66
|
|
|
{ |
67
|
15 |
|
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
68
|
15 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
11 |
|
public function doSendRequest(RequestInterface $request) |
74
|
|
|
{ |
75
|
11 |
|
$this->requests[] = $request; |
76
|
|
|
|
77
|
11 |
|
foreach ($this->conditionalResults as $result) { |
78
|
|
|
/** |
79
|
|
|
* @var RequestMatcher |
80
|
|
|
*/ |
81
|
4 |
|
$matcher = $result['matcher']; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var callable |
85
|
|
|
*/ |
86
|
4 |
|
$callable = $result['callable']; |
87
|
|
|
|
88
|
4 |
|
if ($matcher->matches($request)) { |
89
|
4 |
|
return $callable($request); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
8 |
|
if (count($this->exceptions) > 0) { |
94
|
1 |
|
throw array_shift($this->exceptions); |
95
|
|
|
} |
96
|
|
|
|
97
|
7 |
|
if (count($this->responses) > 0) { |
98
|
3 |
|
return array_shift($this->responses); |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
if ($this->defaultException) { |
102
|
1 |
|
throw $this->defaultException; |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
if ($this->defaultResponse) { |
106
|
1 |
|
return $this->defaultResponse; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Return success response by default |
110
|
2 |
|
return $this->responseFactory->createResponse(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Adds an exception to be thrown or response to be returned if the request |
115
|
|
|
* matcher matches. |
116
|
|
|
* |
117
|
|
|
* For more complex logic, pass a callable as $result. The method is given |
118
|
|
|
* the request and MUST either return a ResponseInterface or throw an |
119
|
|
|
* exception that implements the PSR-18 / HTTPlug exception interface. |
120
|
|
|
* |
121
|
|
|
* @param ResponseInterface|Exception|ClientExceptionInterface|callable $result |
122
|
|
|
*/ |
123
|
4 |
|
public function on(RequestMatcher $requestMatcher, $result) |
124
|
|
|
{ |
125
|
4 |
|
$callable = self::makeCallable($result); |
126
|
|
|
|
127
|
4 |
|
$this->conditionalResults[] = [ |
128
|
4 |
|
'matcher' => $requestMatcher, |
129
|
4 |
|
'callable' => $callable, |
130
|
|
|
]; |
131
|
4 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param ResponseInterface|Exception|ClientExceptionInterface|callable $result |
135
|
|
|
* |
136
|
|
|
* @throws \InvalidArgumentException |
137
|
|
|
* |
138
|
|
|
* @return callable |
139
|
|
|
*/ |
140
|
4 |
|
private static function makeCallable($result) |
141
|
|
|
{ |
142
|
4 |
|
if (is_callable($result)) { |
143
|
1 |
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
3 |
|
if ($result instanceof ResponseInterface) { |
147
|
|
|
return function () use ($result) { |
148
|
1 |
|
return $result; |
149
|
2 |
|
}; |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
if ($result instanceof \Exception) { |
153
|
1 |
|
return function () use ($result) { |
154
|
1 |
|
throw $result; |
155
|
1 |
|
}; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Adds an exception that will be thrown. |
163
|
|
|
*/ |
164
|
2 |
View Code Duplication |
public function addException(\Exception $exception) |
|
|
|
|
165
|
|
|
{ |
166
|
2 |
|
if (!$exception instanceof Exception) { |
167
|
|
|
@trigger_error('Clients may only throw exceptions of type '.Exception::class.'. Setting an exception of class '.get_class($exception).' will not be possible anymore in the future', E_USER_DEPRECATED); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
$this->exceptions[] = $exception; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
174
|
|
|
* |
175
|
|
|
* If both a default exception and a default response are set, the exception will be thrown. |
176
|
|
|
*/ |
177
|
|
View Code Duplication |
public function setDefaultException(\Exception $defaultException = null) |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
if (!$defaultException instanceof Exception) { |
180
|
|
|
@trigger_error('Clients may only throw exceptions of type '.Exception::class.'. Setting an exception of class '.get_class($defaultException).' will not be possible anymore in the future', E_USER_DEPRECATED); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
$this->defaultException = $defaultException; |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Adds a response that will be returned in first in first out order. |
187
|
|
|
*/ |
188
|
|
|
public function addResponse(ResponseInterface $response) |
189
|
|
|
{ |
190
|
|
|
$this->responses[] = $response; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
195
|
|
|
*/ |
196
|
|
|
public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
197
|
|
|
{ |
198
|
|
|
$this->defaultResponse = $defaultResponse; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns requests that were sent. |
203
|
|
|
* |
204
|
|
|
* @return RequestInterface[] |
205
|
|
|
*/ |
206
|
|
|
public function getRequests() |
207
|
|
|
{ |
208
|
|
|
return $this->requests; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getLastRequest() |
212
|
|
|
{ |
213
|
|
|
return end($this->requests); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function reset() |
217
|
|
|
{ |
218
|
|
|
$this->responses = []; |
219
|
|
|
$this->exceptions = []; |
220
|
|
|
$this->requests = []; |
221
|
|
|
$this->setDefaultException(); |
222
|
|
|
$this->setDefaultResponse(); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.