|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EasyHttp\MockBuilder; |
|
4
|
|
|
|
|
5
|
|
|
use EasyHttp\MockBuilder\Contracts\HeaderAggregate; |
|
6
|
|
|
use EasyHttp\MockBuilder\Contracts\QueryParameterAggregate; |
|
7
|
|
|
use EasyHttp\MockBuilder\Contracts\ResponseBuilder; |
|
8
|
|
|
use EasyHttp\MockBuilder\Iterators\ArrayIterator; |
|
9
|
|
|
use EasyHttp\MockBuilder\Iterators\NotEmptyArrayValuesIterator; |
|
10
|
|
|
use EasyHttp\MockBuilder\Iterators\EmptyArrayValuesIterator; |
|
11
|
|
|
use EasyHttp\MockBuilder\ResponseBuilders\GuzzleResponseBuilder; |
|
12
|
|
|
use Psr\Http\Message\RequestInterface; |
|
13
|
|
|
|
|
14
|
|
|
class Expectation implements QueryParameterAggregate, HeaderAggregate |
|
15
|
|
|
{ |
|
16
|
|
|
protected ResponseBuilder $responseBuilder; |
|
17
|
|
|
|
|
18
|
|
|
private string $method; |
|
19
|
|
|
private string $path; |
|
20
|
|
|
private array $queryParams = []; |
|
21
|
|
|
private array $headers = []; |
|
22
|
|
|
|
|
23
|
|
|
private string $pathRegex; |
|
24
|
|
|
private array $missingQueryParams = []; |
|
25
|
|
|
private array $missingHeaders = []; |
|
26
|
|
|
|
|
27
|
|
|
private array $rejectedHeaders = []; |
|
28
|
|
|
|
|
29
|
|
|
private array $bodyHandlers = []; |
|
30
|
|
|
private array $requestHandlers = []; |
|
31
|
|
|
|
|
32
|
121 |
|
public function then(): ResponseBuilder |
|
33
|
|
|
{ |
|
34
|
121 |
|
$this->responseBuilder = new GuzzleResponseBuilder(); |
|
35
|
|
|
|
|
36
|
121 |
|
return $this->responseBuilder; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
56 |
|
public function responseBuilder(?RequestInterface $request = null): ResponseBuilder |
|
40
|
|
|
{ |
|
41
|
56 |
|
$this->responseBuilder->setRequest($request); |
|
42
|
|
|
|
|
43
|
56 |
|
return $this->responseBuilder; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
121 |
|
public function getMethod(): ?string |
|
47
|
|
|
{ |
|
48
|
121 |
|
return $this->method ?? null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
119 |
|
public function getPath(): ?string |
|
52
|
|
|
{ |
|
53
|
119 |
|
return $this->path ?? null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
118 |
|
public function getPathRegex(): ?string |
|
57
|
|
|
{ |
|
58
|
118 |
|
return $this->pathRegex ?? null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
10 |
|
public function methodIs(string $method): self |
|
62
|
|
|
{ |
|
63
|
10 |
|
$this->method = $method; |
|
64
|
|
|
|
|
65
|
10 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
public function pathIs(string $path): self |
|
69
|
|
|
{ |
|
70
|
4 |
|
$this->path = $path; |
|
71
|
|
|
|
|
72
|
4 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public function pathMatch(string $regex): self |
|
76
|
|
|
{ |
|
77
|
2 |
|
$this->pathRegex = $regex; |
|
78
|
|
|
|
|
79
|
2 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
17 |
|
public function queryParamIs(string $key, string $value): self |
|
83
|
|
|
{ |
|
84
|
17 |
|
$this->queryParams[$key] = $value; |
|
85
|
|
|
|
|
86
|
17 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
15 |
|
public function queryParamExists(string $key): self |
|
90
|
|
|
{ |
|
91
|
15 |
|
$this->queryParams[$key] = null; |
|
92
|
|
|
|
|
93
|
15 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
14 |
|
public function queryParamNotExist(string $key): self |
|
97
|
|
|
{ |
|
98
|
14 |
|
$this->missingQueryParams[] = $key; |
|
99
|
|
|
|
|
100
|
14 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
7 |
|
public function queryParamsAre(array $params): self |
|
104
|
|
|
{ |
|
105
|
7 |
|
array_walk( |
|
106
|
|
|
$params, |
|
107
|
7 |
|
function ($value, $key) { |
|
108
|
6 |
|
$this->queryParamIs($key, $value); |
|
109
|
|
|
} |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
7 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
7 |
|
public function queryParamsExist(array $params): self |
|
116
|
|
|
{ |
|
117
|
7 |
|
array_walk( |
|
118
|
|
|
$params, |
|
119
|
7 |
|
function ($value) { |
|
120
|
6 |
|
$this->queryParamExists($value); |
|
121
|
|
|
} |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
7 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
7 |
|
public function queryParamsNotExist(array $params): self |
|
128
|
|
|
{ |
|
129
|
7 |
|
array_walk( |
|
130
|
|
|
$params, |
|
131
|
7 |
|
function ($value) { |
|
132
|
6 |
|
$this->queryParamNotExist($value); |
|
133
|
|
|
} |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
7 |
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
14 |
|
public function headerIs(string $key, string $value): self |
|
140
|
|
|
{ |
|
141
|
14 |
|
$this->headers[$key] = $value; |
|
142
|
|
|
|
|
143
|
14 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
10 |
|
public function headerIsNot(string $key, string $value): self |
|
147
|
|
|
{ |
|
148
|
10 |
|
$this->rejectedHeaders[$key] = $value; |
|
149
|
|
|
|
|
150
|
10 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
7 |
|
public function headerExists(string $key): self |
|
154
|
|
|
{ |
|
155
|
7 |
|
$this->headers[$key] = null; |
|
156
|
|
|
|
|
157
|
7 |
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
14 |
|
public function headerNotExist(string $key): self |
|
161
|
|
|
{ |
|
162
|
14 |
|
$this->missingHeaders[] = $key; |
|
163
|
|
|
|
|
164
|
14 |
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
7 |
|
public function headersAre(array $headers): self |
|
168
|
|
|
{ |
|
169
|
7 |
|
array_walk( |
|
170
|
|
|
$headers, |
|
171
|
7 |
|
function ($value, $key) { |
|
172
|
6 |
|
$this->headerIs($key, $value); |
|
173
|
|
|
} |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
7 |
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
7 |
|
public function headersExist(array $headers): self |
|
180
|
|
|
{ |
|
181
|
7 |
|
array_walk( |
|
182
|
|
|
$headers, |
|
183
|
7 |
|
function ($value) { |
|
184
|
6 |
|
$this->headerExists($value); |
|
185
|
|
|
} |
|
186
|
|
|
); |
|
187
|
|
|
|
|
188
|
7 |
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
7 |
|
public function headersNotExist(array $headers): self |
|
192
|
|
|
{ |
|
193
|
7 |
|
array_walk( |
|
194
|
|
|
$headers, |
|
195
|
7 |
|
function ($value) { |
|
196
|
6 |
|
$this->headerNotExist($value); |
|
197
|
|
|
} |
|
198
|
|
|
); |
|
199
|
|
|
|
|
200
|
7 |
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
2 |
|
public function body(callable $callback): self |
|
204
|
|
|
{ |
|
205
|
2 |
|
$this->bodyHandlers[] = $callback; |
|
206
|
|
|
|
|
207
|
2 |
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
2 |
|
public function request(callable $callback): self |
|
211
|
|
|
{ |
|
212
|
2 |
|
$this->requestHandlers[] = $callback; |
|
213
|
|
|
|
|
214
|
2 |
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
117 |
|
public function notEmptyQueryParamsIterator(): NotEmptyArrayValuesIterator |
|
218
|
|
|
{ |
|
219
|
117 |
|
return new NotEmptyArrayValuesIterator($this->queryParams); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
106 |
|
public function emptyQueryParamsIterator(): EmptyArrayValuesIterator |
|
223
|
|
|
{ |
|
224
|
106 |
|
return new EmptyArrayValuesIterator($this->queryParams); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
96 |
|
public function missingQueryParamsIterator(): ArrayIterator |
|
228
|
|
|
{ |
|
229
|
96 |
|
return new ArrayIterator($this->missingQueryParams); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
87 |
|
public function notEmptyHeadersIterator(): NotEmptyArrayValuesIterator |
|
233
|
|
|
{ |
|
234
|
87 |
|
return new NotEmptyArrayValuesIterator($this->headers); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
72 |
|
public function emptyHeadersIterator(): EmptyArrayValuesIterator |
|
238
|
|
|
{ |
|
239
|
72 |
|
return new EmptyArrayValuesIterator($this->headers); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
67 |
|
public function missingHeadersIterator(): ArrayIterator |
|
243
|
|
|
{ |
|
244
|
67 |
|
return new ArrayIterator($this->missingHeaders); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
77 |
|
public function rejectedHeadersIterator(): ArrayIterator |
|
248
|
|
|
{ |
|
249
|
77 |
|
return new ArrayIterator($this->rejectedHeaders); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
58 |
|
public function bodyHandlersIterator(): ArrayIterator |
|
253
|
|
|
{ |
|
254
|
58 |
|
return new ArrayIterator($this->bodyHandlers); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
57 |
|
public function requestHandlersIterator(): ArrayIterator |
|
258
|
|
|
{ |
|
259
|
57 |
|
return new ArrayIterator($this->requestHandlers); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|