1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Mock; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\HttpAsyncClientEmulator; |
6
|
|
|
use Http\Client\Exception; |
7
|
|
|
use Http\Client\HttpAsyncClient; |
8
|
|
|
use Http\Client\HttpClient; |
9
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
10
|
|
|
use Http\Message\ResponseFactory; |
11
|
|
|
use Psr\Http\Message\RequestInterface; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* An implementation of the HTTP client that is useful for automated tests. |
16
|
|
|
* |
17
|
|
|
* This mock does not send requests but stores them for later retrieval. |
18
|
|
|
* You can configure the mock with responses to return and/or exceptions to throw. |
19
|
|
|
* |
20
|
|
|
* @author David de Boer <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Client implements HttpClient, HttpAsyncClient |
23
|
|
|
{ |
24
|
|
|
use HttpAsyncClientEmulator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ResponseFactory |
28
|
|
|
*/ |
29
|
|
|
private $responseFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RequestInterface[] |
33
|
|
|
*/ |
34
|
|
|
private $requests = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ResponseInterface[] |
38
|
|
|
*/ |
39
|
|
|
private $responses = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ResponseInterface|null |
43
|
|
|
*/ |
44
|
|
|
private $defaultResponse; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Exception[] |
48
|
|
|
*/ |
49
|
|
|
private $exceptions = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Exception|null |
53
|
|
|
*/ |
54
|
|
|
private $defaultException; |
55
|
|
|
|
56
|
10 |
|
public function __construct(ResponseFactory $responseFactory = null) |
57
|
|
|
{ |
58
|
10 |
|
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
59
|
10 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* This will in order: |
63
|
|
|
* |
64
|
|
|
* - Throw the next exception in the list and advance |
65
|
|
|
* - Return the next response in the list and advance |
66
|
|
|
* - Throw the default exception if set (forever) |
67
|
|
|
* - Return the default response if set (forever) |
68
|
|
|
* - Create a new empty response with the response factory |
69
|
|
|
*/ |
70
|
6 |
|
public function sendRequest(RequestInterface $request): ResponseInterface |
71
|
|
|
{ |
72
|
6 |
|
$this->requests[] = $request; |
73
|
|
|
|
74
|
6 |
|
if (count($this->exceptions) > 0) { |
75
|
1 |
|
throw array_shift($this->exceptions); |
76
|
|
|
} |
77
|
|
|
|
78
|
5 |
|
if (count($this->responses) > 0) { |
79
|
2 |
|
return array_shift($this->responses); |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
if ($this->defaultException) { |
83
|
1 |
|
throw $this->defaultException; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
if ($this->defaultResponse) { |
87
|
1 |
|
return $this->defaultResponse; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Return success response by default |
91
|
1 |
|
return $this->responseFactory->createResponse(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds an exception that will be thrown. |
96
|
|
|
*/ |
97
|
1 |
|
public function addException(\Exception $exception) |
98
|
|
|
{ |
99
|
1 |
|
$this->exceptions[] = $exception; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
104
|
|
|
* |
105
|
|
|
* If both a default exception and a default response are set, the exception will be thrown. |
106
|
|
|
*/ |
107
|
1 |
|
public function setDefaultException(?\Exception $defaultException) |
108
|
|
|
{ |
109
|
1 |
|
$this->defaultException = $defaultException; |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Adds a response that will be returned in first in first out order. |
114
|
|
|
*/ |
115
|
2 |
|
public function addResponse(ResponseInterface $response) |
116
|
|
|
{ |
117
|
2 |
|
$this->responses[] = $response; |
118
|
2 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
122
|
|
|
*/ |
123
|
1 |
|
public function setDefaultResponse(?ResponseInterface $defaultResponse) |
124
|
|
|
{ |
125
|
1 |
|
$this->defaultResponse = $defaultResponse; |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns requests that were sent. |
130
|
|
|
* |
131
|
|
|
* @return RequestInterface[] |
132
|
|
|
*/ |
133
|
|
|
public function getRequests(): array |
134
|
|
|
{ |
135
|
|
|
return $this->requests; |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
public function getLastRequest(): ?RequestInterface |
139
|
|
|
{ |
140
|
2 |
|
return end($this->requests) ?: null; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|