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\Message\RequestInterface; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* An implementation of the HTTP client that is useful for automated tests. |
18
|
|
|
* |
19
|
|
|
* This mock does not send requests but stores them for later retrieval. |
20
|
|
|
* You can configure the mock with responses to return and/or exceptions to throw. |
21
|
|
|
* |
22
|
|
|
* @author David de Boer <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Client implements HttpClient, HttpAsyncClient |
25
|
|
|
{ |
26
|
|
|
use HttpAsyncClientEmulator; |
27
|
|
|
use VersionBridgeClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ResponseFactory |
31
|
|
|
*/ |
32
|
|
|
private $responseFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $conditionalResults = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var RequestInterface[] |
41
|
|
|
*/ |
42
|
|
|
private $requests = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ResponseInterface[] |
46
|
|
|
*/ |
47
|
|
|
private $responses = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var ResponseInterface|null |
51
|
|
|
*/ |
52
|
|
|
private $defaultResponse; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Exception[] |
56
|
|
|
*/ |
57
|
|
|
private $exceptions = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Exception|null |
61
|
|
|
*/ |
62
|
|
|
private $defaultException; |
63
|
|
|
|
64
|
15 |
|
public function __construct(ResponseFactory $responseFactory = null) |
65
|
|
|
{ |
66
|
15 |
|
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
67
|
15 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
11 |
|
public function doSendRequest(RequestInterface $request) |
73
|
|
|
{ |
74
|
11 |
|
$this->requests[] = $request; |
75
|
|
|
|
76
|
11 |
|
foreach ($this->conditionalResults as $result) { |
77
|
|
|
/** |
78
|
|
|
* @var RequestMatcher |
79
|
|
|
*/ |
80
|
4 |
|
$matcher = $result['matcher']; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var callable |
84
|
|
|
*/ |
85
|
4 |
|
$callable = $result['callable']; |
86
|
|
|
|
87
|
4 |
|
if ($matcher->matches($request)) { |
88
|
4 |
|
return $callable($request); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
8 |
|
if (count($this->exceptions) > 0) { |
93
|
1 |
|
throw array_shift($this->exceptions); |
94
|
|
|
} |
95
|
|
|
|
96
|
7 |
|
if (count($this->responses) > 0) { |
97
|
3 |
|
return array_shift($this->responses); |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
if ($this->defaultException) { |
101
|
1 |
|
throw $this->defaultException; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
if ($this->defaultResponse) { |
105
|
1 |
|
return $this->defaultResponse; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Return success response by default |
109
|
2 |
|
return $this->responseFactory->createResponse(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param RequestMatcher $requestMatcher |
114
|
|
|
* @param ResponseInterface|\Exception|callable $result |
115
|
|
|
*/ |
116
|
4 |
|
public function on(RequestMatcher $requestMatcher, $result) |
117
|
|
|
{ |
118
|
4 |
|
$callable = null; |
|
|
|
|
119
|
|
|
|
120
|
|
|
switch (true) { |
121
|
4 |
|
case is_callable($result): |
122
|
1 |
|
$callable = $result; |
123
|
|
|
|
124
|
1 |
|
break; |
125
|
3 |
|
case $result instanceof ResponseInterface: |
126
|
|
|
$callable = function () use ($result) { |
127
|
1 |
|
return $result; |
128
|
2 |
|
}; |
129
|
|
|
|
130
|
2 |
|
break; |
131
|
1 |
|
case $result instanceof \Exception: |
132
|
1 |
|
$callable = function () use ($result) { |
133
|
1 |
|
throw $result; |
134
|
1 |
|
}; |
135
|
|
|
|
136
|
1 |
|
break; |
137
|
|
|
default: |
138
|
|
|
throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable'); |
139
|
|
|
} |
140
|
4 |
|
$this->conditionalResults[] = [ |
141
|
4 |
|
'matcher' => $requestMatcher, |
142
|
4 |
|
'callable' => $callable, |
143
|
|
|
]; |
144
|
4 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Adds an exception that will be thrown. |
148
|
|
|
*/ |
149
|
2 |
View Code Duplication |
public function addException(\Exception $exception) |
|
|
|
|
150
|
|
|
{ |
151
|
2 |
|
if (!$exception instanceof Exception) { |
152
|
|
|
@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); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
$this->exceptions[] = $exception; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
159
|
|
|
* |
160
|
|
|
* If both a default exception and a default response are set, the exception will be thrown. |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function setDefaultException(\Exception $defaultException = null) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
if (!$defaultException instanceof Exception) { |
165
|
|
|
@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); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
$this->defaultException = $defaultException; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Adds a response that will be returned in first in first out order. |
172
|
|
|
*/ |
173
|
|
|
public function addResponse(ResponseInterface $response) |
174
|
|
|
{ |
175
|
|
|
$this->responses[] = $response; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
180
|
|
|
*/ |
181
|
|
|
public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
182
|
|
|
{ |
183
|
|
|
$this->defaultResponse = $defaultResponse; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns requests that were sent. |
188
|
|
|
* |
189
|
|
|
* @return RequestInterface[] |
190
|
|
|
*/ |
191
|
|
|
public function getRequests() |
192
|
|
|
{ |
193
|
|
|
return $this->requests; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getLastRequest() |
197
|
|
|
{ |
198
|
|
|
return end($this->requests); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function reset() |
202
|
|
|
{ |
203
|
|
|
$this->responses = []; |
204
|
|
|
$this->exceptions = []; |
205
|
|
|
$this->requests = []; |
206
|
|
|
$this->setDefaultException(); |
207
|
|
|
$this->setDefaultResponse(); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.