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
|
|
|
* HTTP client mock. |
16
|
|
|
* |
17
|
|
|
* This mock is most useful in tests. It does not send requests but stores them |
18
|
|
|
* for later retrieval. Additionally, you can set an exception to test |
19
|
|
|
* exception handling. |
20
|
|
|
* |
21
|
|
|
* @author David de Boer <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Client implements HttpClient, HttpAsyncClient |
24
|
|
|
{ |
25
|
|
|
use HttpAsyncClientEmulator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ResponseFactory |
29
|
|
|
*/ |
30
|
|
|
private $responseFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RequestInterface[] |
34
|
|
|
*/ |
35
|
|
|
private $requests = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ResponseInterface[] |
39
|
|
|
*/ |
40
|
|
|
private $responses = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Exception[] |
44
|
|
|
*/ |
45
|
|
|
private $exceptions = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ResponseFactory|null $responseFactory |
49
|
|
|
*/ |
50
|
9 |
|
public function __construct(ResponseFactory $responseFactory = null) |
51
|
|
|
{ |
52
|
9 |
|
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
53
|
9 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
4 |
|
public function sendRequest(RequestInterface $request) |
59
|
|
|
{ |
60
|
4 |
|
$this->requests[] = $request; |
61
|
|
|
|
62
|
4 |
|
if (count($this->exceptions) > 0) { |
63
|
1 |
|
throw array_shift($this->exceptions); |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
if (count($this->responses) > 0) { |
67
|
1 |
|
return array_shift($this->responses); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Return success response by default |
71
|
2 |
|
return $this->responseFactory->createResponse(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Adds an exception that will be thrown. |
76
|
|
|
* |
77
|
|
|
* @param \Exception $exception |
78
|
|
|
*/ |
79
|
2 |
|
public function addException(\Exception $exception) |
80
|
|
|
{ |
81
|
2 |
|
$this->exceptions[] = $exception; |
82
|
2 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Adds a response that will be returned. |
86
|
|
|
* |
87
|
|
|
* @param ResponseInterface $response |
88
|
|
|
*/ |
89
|
2 |
|
public function addResponse(ResponseInterface $response) |
90
|
|
|
{ |
91
|
2 |
|
$this->responses[] = $response; |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns requests that were sent. |
96
|
|
|
* |
97
|
|
|
* @return RequestInterface[] |
98
|
|
|
*/ |
99
|
|
|
public function getRequests() |
100
|
|
|
{ |
101
|
|
|
return $this->requests; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return RequestInterface|false |
106
|
|
|
*/ |
107
|
1 |
|
public function getLastRequest() |
108
|
|
|
{ |
109
|
1 |
|
return end($this->requests); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Exception|false |
114
|
|
|
*/ |
115
|
1 |
|
public function getLastException() |
116
|
|
|
{ |
117
|
1 |
|
return end($this->exceptions); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return ResponseInterface|false |
122
|
|
|
*/ |
123
|
1 |
|
public function getLastResponse() |
124
|
|
|
{ |
125
|
1 |
|
return end($this->responses); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|