1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Muzzle\Messages; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7; |
6
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
|
9
|
|
|
class AssertableRequest implements RequestInterface |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
use ContentAssertions; |
13
|
|
|
use RequestDecorator; |
14
|
|
|
use JsonMessage; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Asserts that the request contains the given header and equals the optional value. |
18
|
|
|
* |
19
|
|
|
* @param string $headerName |
20
|
|
|
* @param mixed $value |
21
|
|
|
* @return $this |
22
|
|
|
*/ |
23
|
|
|
public function assertHeader($headerName, $value = null) |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
PHPUnit::assertTrue( |
27
|
|
|
$this->hasHeader($headerName), |
28
|
|
|
"Header [{$headerName}] not present on request." |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$actual = $this->getHeader($headerName); |
32
|
|
|
|
33
|
|
|
if (! is_null($value)) { |
34
|
|
|
$expected = (array) $value; |
35
|
|
|
sort($expected); |
36
|
|
|
sort($actual); |
37
|
|
|
|
38
|
|
|
$message = sprintf( |
39
|
|
|
"Header [%s] was found, but value(s) [%s] does not match [%s].", |
40
|
|
|
$headerName, |
41
|
|
|
implode(', ', $actual), |
42
|
|
|
implode(', ', $expected) |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
PHPUnit::assertArraySubset($expected, $actual, $message); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Assert that the given string matches the request target. |
53
|
|
|
* |
54
|
|
|
* @param string $target |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function assertRequestTarget($target) |
58
|
|
|
{ |
59
|
|
|
|
60
|
|
|
PHPUnit::assertEquals($target, $this->getRequestTarget()); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Assert that the given string matches the HTTP request method. |
67
|
|
|
* |
68
|
|
|
* @param string $method |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function assertMethod(string $method) |
72
|
|
|
{ |
73
|
|
|
|
74
|
|
|
PHPUnit::assertEquals(strtoupper($method), $this->getMethod()); |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Assert that the given string matches the scheme component of the URI. |
81
|
|
|
* |
82
|
|
|
* @param string $scheme |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function assertUriScheme($scheme) |
86
|
|
|
{ |
87
|
|
|
|
88
|
|
|
PHPUnit::assertEquals($scheme, $this->getUri()->getScheme()); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Assert that the given string matches the authority component of the URI. |
95
|
|
|
* |
96
|
|
|
* @param string $authority |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function assertUriAuthority($authority) |
100
|
|
|
{ |
101
|
|
|
|
102
|
|
|
PHPUnit::assertEquals($authority, $this->getUri()->getAuthority()); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Assert that the given string matches the user information component of the URI. |
109
|
|
|
* |
110
|
|
|
* @param string $userInfo |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function assertUriUserInfo($userInfo) |
114
|
|
|
{ |
115
|
|
|
|
116
|
|
|
PHPUnit::assertEquals($userInfo, $this->getUri()->getUserInfo()); |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Assert that the given string matches the host component of the URI. |
123
|
|
|
* |
124
|
|
|
* @param string $host |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
|
|
public function assertUriHost($host) |
128
|
|
|
{ |
129
|
|
|
|
130
|
|
|
PHPUnit::assertEquals($host, $this->getUri()->getHost()); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Assert that the given string matches the port component of the URI. |
137
|
|
|
* |
138
|
|
|
* @param int|null $port |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
|
|
public function assertUriPort($port = null) |
142
|
|
|
{ |
143
|
|
|
|
144
|
|
|
PHPUnit::assertEquals($port, $this->getUri()->getPort()); |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Assert that the given string matches the path component of the URI. |
152
|
|
|
* |
153
|
|
|
* @param string $path |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function assertUriPath($path) |
157
|
|
|
{ |
158
|
|
|
|
159
|
|
|
PHPUnit::assertEquals($path, $this->getUri()->getPath()); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Assert that the given string matches the fragment component of the URI. |
167
|
|
|
* |
168
|
|
|
* @param string $fragment |
169
|
|
|
* @return $this |
170
|
|
|
*/ |
171
|
|
|
public function assertUriFragment($fragment) |
172
|
|
|
{ |
173
|
|
|
|
174
|
|
|
PHPUnit::assertEquals($fragment, $this->getUri()->getFragment()); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Assert that the given string matches the query component of the URI. |
182
|
|
|
* |
183
|
|
|
* @param string $query |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function assertUriQuery($query) |
187
|
|
|
{ |
188
|
|
|
|
189
|
|
|
PHPUnit::assertEquals($query, $this->getUri()->getQuery()); |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Assert that the given key exists in the query. |
196
|
|
|
* |
197
|
|
|
* @param string $key |
198
|
|
|
* @return $this |
199
|
|
|
*/ |
200
|
|
|
public function assertUriQueryHasKey($key) |
201
|
|
|
{ |
202
|
|
|
|
203
|
|
|
$query = Psr7\parse_query($this->getUri()->getQuery()); |
|
|
|
|
204
|
|
|
PHPUnit::assertArrayHasKey($key, $query); |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Assert that the given key does not exist in the query. |
211
|
|
|
* |
212
|
|
|
* @param string $key |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
|
|
public function assertUriQueryNotHasKey($key) |
216
|
|
|
{ |
217
|
|
|
|
218
|
|
|
$query = Psr7\parse_query($this->getUri()->getQuery()); |
|
|
|
|
219
|
|
|
PHPUnit::assertArrayNotHasKey($key, $query); |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Assert that the given key exists in the query. |
226
|
|
|
* |
227
|
|
|
* @param array $values |
228
|
|
|
* @return $this |
229
|
|
|
*/ |
230
|
|
|
public function assertUriQueryContains(array $values) |
231
|
|
|
{ |
232
|
|
|
|
233
|
|
|
$query = Psr7\parse_query($this->getUri()->getQuery()); |
|
|
|
|
234
|
|
|
PHPUnit::assertArraySubset($values, $query, false, (function ($expected, $actual) { |
235
|
|
|
|
236
|
|
|
return 'Could not find ' . PHP_EOL |
237
|
|
|
. var_export($expected, true) . PHP_EOL |
238
|
|
|
. 'within response' . PHP_EOL |
239
|
|
|
. var_export($actual, true) . PHP_EOL; |
240
|
|
|
})($values, $query)); |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function assertUriEquals(Psr7\Uri $uri) |
246
|
|
|
{ |
247
|
|
|
|
248
|
|
|
PHPUnit::assertEquals($this->getUri(), $uri, sprintf('Failed asserting %s equals %s', $this->getUri(), $uri)); |
249
|
|
|
|
250
|
|
|
return $this; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|