1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Muzzle\Messages; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
8
|
|
|
|
9
|
|
|
trait ContentAssertions |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Gets the body of the message. |
14
|
|
|
* |
15
|
|
|
* @return \Psr\Http\Message\StreamInterface Returns the body as a stream. |
16
|
|
|
*/ |
17
|
|
|
abstract public function getBody(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Decodes a JSON body to an array. |
21
|
|
|
* |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
abstract public function decode() : array; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Assert that the given string is contained within the response. |
28
|
|
|
* |
29
|
|
|
* @param string $value |
30
|
|
|
* @return $this |
31
|
|
|
*/ |
32
|
|
|
public function assertSee($value) : self |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
PHPUnit::assertContains($value, $this->getBody()->getContents()); |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Assert that the given string is contained within the response text. |
42
|
|
|
* |
43
|
|
|
* @param string $value |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
|
public function assertSeeText($value) : self |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
PHPUnit::assertContains($value, strip_tags($this->getBody()->getContents())); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Assert that the given string is not contained within the response. |
56
|
|
|
* |
57
|
|
|
* @param string $value |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function assertDoNotSee($value) : self |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
PHPUnit::assertNotContains($value, $this->getBody()->getContents()); |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Assert that the given string is not contained within the response text. |
70
|
|
|
* |
71
|
|
|
* @param string $value |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function assertDoNotSeeText($value) : self |
75
|
|
|
{ |
76
|
|
|
|
77
|
|
|
PHPUnit::assertNotContains($value, strip_tags($this->getBody()->getContents())); |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Assert that the response is a superset of the given JSON. |
84
|
|
|
* |
85
|
|
|
* @param array $data |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function assertJson(array $data) : self |
89
|
|
|
{ |
90
|
|
|
|
91
|
|
|
PHPUnit::assertArraySubset( |
92
|
|
|
$data, |
93
|
|
|
$this->decode(), |
94
|
|
|
false, |
95
|
|
|
$this->assertJsonMessage($data) |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the assertion message for assertJson. |
103
|
|
|
* |
104
|
|
|
* @param array $data |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function assertJsonMessage(array $data) : string |
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
$expected = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
111
|
|
|
|
112
|
|
|
$actual = json_encode($this->decode(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
113
|
|
|
|
114
|
|
|
return 'Unable to find JSON: ' . PHP_EOL . PHP_EOL . |
115
|
|
|
"[{$expected}]" . PHP_EOL . PHP_EOL . |
116
|
|
|
'within response JSON:' . PHP_EOL . PHP_EOL . |
117
|
|
|
"[{$actual}]." . PHP_EOL . PHP_EOL; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Assert that the response has the exact given JSON. |
122
|
|
|
* |
123
|
|
|
* @param array $data |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function assertExactJson(array $data) : self |
127
|
|
|
{ |
128
|
|
|
|
129
|
|
|
$actual = json_encode(Arr::sortRecursive((array) $this->decode())); |
130
|
|
|
|
131
|
|
|
PHPUnit::assertEquals(json_encode(Arr::sortRecursive($data)), $actual); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Assert that the response contains the given JSON fragment. |
138
|
|
|
* |
139
|
|
|
* @param array $data |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function assertJsonFragment(array $data) : self |
143
|
|
|
{ |
144
|
|
|
|
145
|
|
|
$actual = json_encode(Arr::sortRecursive((array) $this->decode())); |
146
|
|
|
|
147
|
|
|
foreach (Arr::sortRecursive($data) as $key => $value) { |
148
|
|
|
$expected = substr(json_encode([$key => $value]), 1, -1); |
149
|
|
|
|
150
|
|
|
PHPUnit::assertTrue( |
151
|
|
|
Str::contains($actual, $expected), |
152
|
|
|
'Unable to find JSON fragment: ' . PHP_EOL . PHP_EOL . |
153
|
|
|
"[{$expected}]" . PHP_EOL . PHP_EOL . |
154
|
|
|
'within' . PHP_EOL . PHP_EOL . |
155
|
|
|
"[{$actual}]." |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Assert that the response does not contain the given JSON fragment. |
164
|
|
|
* |
165
|
|
|
* @param array $data |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function assertJsonMissing(array $data) : self |
169
|
|
|
{ |
170
|
|
|
|
171
|
|
|
$actual = json_encode(Arr::sortRecursive((array) $this->decode())); |
172
|
|
|
|
173
|
|
|
foreach (Arr::sortRecursive($data) as $key => $value) { |
174
|
|
|
$expected = substr(json_encode([$key => $value]), 1, -1); |
175
|
|
|
|
176
|
|
|
PHPUnit::assertFalse( |
177
|
|
|
Str::contains($actual, $expected), |
178
|
|
|
'Found unexpected JSON fragment: ' . PHP_EOL . PHP_EOL . |
179
|
|
|
"[{$expected}]" . PHP_EOL . PHP_EOL . |
180
|
|
|
'within' . PHP_EOL . PHP_EOL . |
181
|
|
|
"[{$actual}]." |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Assert that the response has a given JSON structure. |
190
|
|
|
* |
191
|
|
|
* @param array|null $structure |
192
|
|
|
* @param array|null $responseData |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function assertJsonStructure(array $structure = null, array $responseData = null) : self |
196
|
|
|
{ |
197
|
|
|
|
198
|
|
|
if (is_null($structure)) { |
199
|
|
|
return $this->assertJson($this->json()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if (is_null($responseData)) { |
203
|
|
|
$responseData = $this->decode(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
foreach ($structure as $key => $value) { |
207
|
|
|
if (is_array($value) && $key === '*') { |
208
|
|
|
PHPUnit::assertInternalType('array', $responseData); |
209
|
|
|
|
210
|
|
|
foreach ($responseData as $responseDataItem) { |
211
|
|
|
$this->assertJsonStructure($structure['*'], $responseDataItem); |
212
|
|
|
} |
213
|
|
|
} elseif (is_array($value)) { |
214
|
|
|
PHPUnit::assertArrayHasKey($key, $responseData); |
215
|
|
|
|
216
|
|
|
$this->assertJsonStructure($structure[$key], $responseData[$key]); |
217
|
|
|
} else { |
218
|
|
|
PHPUnit::assertArrayHasKey($value, $responseData); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string|\Psr\Http\Message\StreamInterface $body |
227
|
|
|
* @return $this |
228
|
|
|
*/ |
229
|
|
|
public function assertBodyEquals($body) : self |
230
|
|
|
{ |
231
|
|
|
|
232
|
|
|
$body = (string) $body; |
233
|
|
|
if ($body !== '') { |
234
|
|
|
PHPUnit::assertEquals($body, (string) $this->getBody()); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Return the decoded response JSON. |
242
|
|
|
* |
243
|
|
|
* @return array |
244
|
|
|
*/ |
245
|
|
|
public function json() : array |
246
|
|
|
{ |
247
|
|
|
|
248
|
|
|
return $this->decode(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Dump the content from the response. |
253
|
|
|
* |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
|
|
public function dump() : void |
257
|
|
|
{ |
258
|
|
|
|
259
|
|
|
dd($this->isJson() ? $this->decode() : $this->getBody()->getContents()); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|