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
|
11 |
|
|
59
|
|
|
/** |
60
|
11 |
|
* @var Exception|null |
61
|
11 |
|
*/ |
62
|
|
|
private $defaultException; |
63
|
|
|
|
64
|
|
|
public function __construct(ResponseFactory $responseFactory = null) |
65
|
|
|
{ |
66
|
7 |
|
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
67
|
|
|
} |
68
|
7 |
|
|
69
|
|
|
/** |
70
|
7 |
|
* {@inheritdoc} |
71
|
1 |
|
*/ |
72
|
|
|
public function doSendRequest(RequestInterface $request) |
73
|
|
|
{ |
74
|
6 |
|
$this->requests[] = $request; |
75
|
2 |
|
|
76
|
|
|
/** |
77
|
|
|
* @var $matcher RequestMatcher |
78
|
4 |
|
* @var ResponseInterface|Exception $result |
79
|
1 |
|
*/ |
80
|
|
|
foreach ($this->conditionalResults as $result) { |
81
|
|
|
$matcher = $result['matcher']; |
82
|
3 |
|
$callable = $result['callable']; |
83
|
1 |
|
|
84
|
|
|
if ($matcher->matches($request)) { |
85
|
|
|
return $callable($request); |
86
|
|
|
} |
87
|
2 |
|
} |
88
|
|
|
|
89
|
|
|
if (count($this->exceptions) > 0) { |
90
|
|
|
throw array_shift($this->exceptions); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
if (count($this->responses) > 0) { |
94
|
|
|
return array_shift($this->responses); |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
if ($this->defaultException) { |
98
|
|
|
throw $this->defaultException; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($this->defaultResponse) { |
102
|
|
|
return $this->defaultResponse; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Return success response by default |
106
|
|
|
return $this->responseFactory->createResponse(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param RequestMatcher $requestMatcher |
111
|
|
|
* @param ResponseInterface|\Exception|callable $result |
112
|
|
|
*/ |
113
|
|
|
public function on(RequestMatcher $requestMatcher, $result) |
114
|
|
|
{ |
115
|
|
|
$callable = null; |
|
|
|
|
116
|
|
|
|
117
|
|
|
switch (true) { |
118
|
|
|
case is_callable($result): |
119
|
|
|
$callable = $result; |
120
|
|
|
|
121
|
|
|
break; |
122
|
|
|
case $result instanceof ResponseInterface: |
123
|
|
|
$callable = function () use ($result) { |
124
|
|
|
return $result; |
125
|
|
|
}; |
126
|
|
|
|
127
|
|
|
break; |
128
|
|
|
case $result instanceof \Exception: |
129
|
|
|
$callable = function () use ($result) { |
130
|
|
|
throw $result; |
131
|
|
|
}; |
132
|
|
|
|
133
|
|
|
break; |
134
|
|
|
default: |
135
|
|
|
throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable'); |
136
|
|
|
} |
137
|
|
|
$this->conditionalResults[] = [ |
138
|
|
|
'matcher' => $requestMatcher, |
139
|
|
|
'callable' => $callable, |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Adds an exception that will be thrown. |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function addException(\Exception $exception) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
if (!$exception instanceof Exception) { |
149
|
|
|
@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); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
$this->exceptions[] = $exception; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
156
|
|
|
* |
157
|
|
|
* If both a default exception and a default response are set, the exception will be thrown. |
158
|
|
|
*/ |
159
|
|
View Code Duplication |
public function setDefaultException(\Exception $defaultException = null) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
if (!$defaultException instanceof Exception) { |
162
|
|
|
@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); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
$this->defaultException = $defaultException; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Adds a response that will be returned in first in first out order. |
169
|
|
|
*/ |
170
|
|
|
public function addResponse(ResponseInterface $response) |
171
|
|
|
{ |
172
|
|
|
$this->responses[] = $response; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
177
|
|
|
*/ |
178
|
|
|
public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
179
|
|
|
{ |
180
|
|
|
$this->defaultResponse = $defaultResponse; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Returns requests that were sent. |
185
|
|
|
* |
186
|
|
|
* @return RequestInterface[] |
187
|
|
|
*/ |
188
|
|
|
public function getRequests() |
189
|
|
|
{ |
190
|
|
|
return $this->requests; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getLastRequest() |
194
|
|
|
{ |
195
|
|
|
return end($this->requests); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function reset() |
199
|
|
|
{ |
200
|
|
|
$this->responses = []; |
201
|
|
|
$this->exceptions = []; |
202
|
|
|
$this->requests = []; |
203
|
|
|
$this->setDefaultException(); |
204
|
|
|
$this->setDefaultResponse(); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
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.