|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiAssert\Asserts\Structure; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
|
8
|
|
|
use VGirol\JsonApiAssert\Constraint\ContainsAtLeastOneConstraint; |
|
9
|
|
|
use VGirol\JsonApiAssert\Constraint\ContainsOnlyAllowedMembersConstraint; |
|
10
|
|
|
use VGirol\JsonApiAssert\Members; |
|
11
|
|
|
use VGirol\JsonApiAssert\Messages; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Assertions relating to the object's members |
|
15
|
|
|
*/ |
|
16
|
|
|
trait AssertMembers |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Asserts that a json object has an expected member. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $expected |
|
22
|
|
|
* @param array $json |
|
23
|
|
|
* |
|
24
|
|
|
* @return void |
|
25
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
26
|
|
|
* @throws \PHPUnit\Framework\Exception |
|
27
|
|
|
*/ |
|
28
|
51 |
|
public static function assertHasMember($expected, $json): void |
|
29
|
|
|
{ |
|
30
|
51 |
|
if (!\is_string($expected)) { |
|
|
|
|
|
|
31
|
3 |
|
static::invalidArgument(1, 'string', $expected); |
|
32
|
|
|
} |
|
33
|
48 |
|
if (!\is_array($json)) { |
|
|
|
|
|
|
34
|
3 |
|
static::invalidArgument(2, 'array', $json); |
|
35
|
|
|
} |
|
36
|
45 |
|
PHPUnit::assertArrayHasKey($expected, $json, sprintf(Messages::HAS_MEMBER, $expected)); |
|
37
|
39 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Asserts that a json object has expected members. |
|
41
|
|
|
* |
|
42
|
|
|
* @param array<string> $expected |
|
43
|
|
|
* @param array $json |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
47
|
|
|
* @throws \PHPUnit\Framework\Exception |
|
48
|
|
|
*/ |
|
49
|
12 |
|
public static function assertHasMembers($expected, $json): void |
|
50
|
|
|
{ |
|
51
|
12 |
|
if (!\is_array($expected)) { |
|
|
|
|
|
|
52
|
3 |
|
static::invalidArgument(1, 'array', $expected); |
|
53
|
|
|
} |
|
54
|
9 |
|
if (!\is_array($json)) { |
|
|
|
|
|
|
55
|
3 |
|
static::invalidArgument(2, 'array', $json); |
|
56
|
|
|
} |
|
57
|
6 |
|
foreach ($expected as $key) { |
|
58
|
6 |
|
PHPUnit::assertArrayHasKey($key, $json, sprintf(Messages::HAS_MEMBER, $key)); |
|
59
|
|
|
} |
|
60
|
3 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Asserts that a json object has only expected members. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array<string> $expected |
|
66
|
|
|
* @param array $json |
|
67
|
|
|
* |
|
68
|
|
|
* @return void |
|
69
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
70
|
|
|
* @throws \PHPUnit\Framework\Exception |
|
71
|
|
|
*/ |
|
72
|
12 |
|
public static function assertHasOnlyMembers($expected, $json): void |
|
73
|
|
|
{ |
|
74
|
12 |
|
if (!\is_array($expected)) { |
|
|
|
|
|
|
75
|
3 |
|
static::invalidArgument(1, 'array', $expected); |
|
76
|
|
|
} |
|
77
|
9 |
|
if (!\is_array($json)) { |
|
|
|
|
|
|
78
|
3 |
|
static::invalidArgument(2, 'array', $json); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
6 |
|
PHPUnit::assertEquals( |
|
82
|
6 |
|
$expected, |
|
83
|
6 |
|
array_keys($json), |
|
84
|
6 |
|
sprintf(Messages::HAS_ONLY_MEMBERS, implode(', ', $expected)) |
|
85
|
|
|
); |
|
86
|
3 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Asserts that a json object not has an unexpected member. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $expected |
|
92
|
|
|
* @param array $json |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
96
|
|
|
* @throws \PHPUnit\Framework\Exception |
|
97
|
|
|
*/ |
|
98
|
24 |
|
public static function assertNotHasMember($expected, $json): void |
|
99
|
|
|
{ |
|
100
|
24 |
|
if (!\is_string($expected)) { |
|
|
|
|
|
|
101
|
3 |
|
static::invalidArgument(1, 'string', $expected); |
|
102
|
|
|
} |
|
103
|
21 |
|
if (!\is_array($json)) { |
|
|
|
|
|
|
104
|
3 |
|
static::invalidArgument(2, 'array', $json); |
|
105
|
|
|
} |
|
106
|
18 |
|
PHPUnit::assertArrayNotHasKey($expected, $json, sprintf(Messages::NOT_HAS_MEMBER, $expected)); |
|
107
|
9 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Asserts that a json object not has unexpected members. |
|
111
|
|
|
* |
|
112
|
|
|
* @param array<string> $expected |
|
113
|
|
|
* @param array $json |
|
114
|
|
|
* |
|
115
|
|
|
* @return void |
|
116
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
117
|
|
|
* @throws \PHPUnit\Framework\Exception |
|
118
|
|
|
*/ |
|
119
|
9 |
|
public static function assertNotHasMembers($expected, $json): void |
|
120
|
|
|
{ |
|
121
|
9 |
|
if (!\is_array($expected)) { |
|
|
|
|
|
|
122
|
3 |
|
static::invalidArgument(1, 'array', $expected); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
6 |
|
foreach ($expected as $key) { |
|
126
|
6 |
|
static::assertNotHasMember($key, $json); |
|
127
|
|
|
} |
|
128
|
3 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Asserts that a json object has a "data" member. |
|
132
|
|
|
* |
|
133
|
|
|
* @see assertHasMember |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $json |
|
136
|
|
|
* |
|
137
|
|
|
* @return void |
|
138
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
139
|
|
|
*/ |
|
140
|
3 |
|
public static function assertHasData($json): void |
|
141
|
|
|
{ |
|
142
|
3 |
|
static::assertHasMember(Members::DATA, $json); |
|
143
|
3 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Asserts that a json object has an "attributes" member. |
|
147
|
|
|
* |
|
148
|
|
|
* @see assertHasMember |
|
149
|
|
|
* |
|
150
|
|
|
* @param array $json |
|
151
|
|
|
* |
|
152
|
|
|
* @return void |
|
153
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
154
|
|
|
*/ |
|
155
|
3 |
|
public static function assertHasAttributes($json): void |
|
156
|
|
|
{ |
|
157
|
3 |
|
static::assertHasMember(Members::ATTRIBUTES, $json); |
|
158
|
3 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Asserts that a json object has a "links" member. |
|
162
|
|
|
* |
|
163
|
|
|
* @see assertHasMember |
|
164
|
|
|
* |
|
165
|
|
|
* @param array $json |
|
166
|
|
|
* |
|
167
|
|
|
* @return void |
|
168
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
169
|
|
|
*/ |
|
170
|
3 |
|
public static function assertHasLinks($json): void |
|
171
|
|
|
{ |
|
172
|
3 |
|
static::assertHasMember(Members::LINKS, $json); |
|
173
|
3 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Asserts that a json object has a "meta" member. |
|
177
|
|
|
* |
|
178
|
|
|
* @see assertHasMember |
|
179
|
|
|
* |
|
180
|
|
|
* @param array $json |
|
181
|
|
|
* |
|
182
|
|
|
* @return void |
|
183
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
184
|
|
|
*/ |
|
185
|
3 |
|
public static function assertHasMeta($json): void |
|
186
|
|
|
{ |
|
187
|
3 |
|
static::assertHasMember(Members::META, $json); |
|
188
|
3 |
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Asserts that a json object has an "included" member. |
|
192
|
|
|
* |
|
193
|
|
|
* @see assertHasMember |
|
194
|
|
|
* |
|
195
|
|
|
* @param array $json |
|
196
|
|
|
* |
|
197
|
|
|
* @return void |
|
198
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
199
|
|
|
*/ |
|
200
|
3 |
|
public static function assertHasIncluded($json): void |
|
201
|
|
|
{ |
|
202
|
3 |
|
static::assertHasMember(Members::INCLUDED, $json); |
|
203
|
3 |
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Asserts that a json object has a "relationships" member. |
|
207
|
|
|
* |
|
208
|
|
|
* @see assertHasMember |
|
209
|
|
|
* |
|
210
|
|
|
* @param array $json |
|
211
|
|
|
* |
|
212
|
|
|
* @return void |
|
213
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
214
|
|
|
*/ |
|
215
|
3 |
|
public static function assertHasRelationships($json): void |
|
216
|
|
|
{ |
|
217
|
3 |
|
static::assertHasMember(Members::RELATIONSHIPS, $json); |
|
218
|
3 |
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Asserts that a json object has an "errors" member. |
|
222
|
|
|
* |
|
223
|
|
|
* @see assertHasMember |
|
224
|
|
|
* |
|
225
|
|
|
* @param array $json |
|
226
|
|
|
* |
|
227
|
|
|
* @return void |
|
228
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
229
|
|
|
*/ |
|
230
|
3 |
|
public static function assertHasErrors($json): void |
|
231
|
|
|
{ |
|
232
|
3 |
|
static::assertHasMember(Members::ERRORS, $json); |
|
233
|
3 |
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Asserts that a json object has a "jsonapi" member. |
|
237
|
|
|
* |
|
238
|
|
|
* @see assertHasMember |
|
239
|
|
|
* |
|
240
|
|
|
* @param array $json |
|
241
|
|
|
* |
|
242
|
|
|
* @return void |
|
243
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
244
|
|
|
*/ |
|
245
|
3 |
|
public static function assertHasJsonapi($json): void |
|
246
|
|
|
{ |
|
247
|
3 |
|
static::assertHasMember(Members::JSONAPI, $json); |
|
248
|
3 |
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Asserts that a json object contains at least one member from the provided list. |
|
252
|
|
|
* |
|
253
|
|
|
* @param array<string> $expected The expected members |
|
254
|
|
|
* @param array $json The json object |
|
255
|
|
|
* @param string $message An optional message to explain why the test failed |
|
256
|
|
|
* |
|
257
|
|
|
* @return void |
|
258
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
259
|
|
|
*/ |
|
260
|
153 |
|
public static function assertContainsAtLeastOneMember($expected, $json, string $message = ''): void |
|
261
|
|
|
{ |
|
262
|
153 |
|
PHPUnit::assertThat($json, self::containsAtLeastOneMemberConstraint($expected), $message); |
|
263
|
129 |
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Returns a new instance of the \VGirol\JsonApiAssert\Constraint\ContainsAtLeastOneConstraint class. |
|
267
|
|
|
* |
|
268
|
|
|
* @param array $expected The expected members |
|
269
|
|
|
* |
|
270
|
|
|
* @return \VGirol\JsonApiAssert\Constraint\ContainsAtLeastOneConstraint |
|
271
|
|
|
*/ |
|
272
|
165 |
|
private static function containsAtLeastOneMemberConstraint($expected): ContainsAtLeastOneConstraint |
|
273
|
|
|
{ |
|
274
|
165 |
|
return new ContainsAtLeastOneConstraint($expected); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Check if a json object contains at least one member from the list provided. |
|
279
|
|
|
* |
|
280
|
|
|
* @param array $expected The expected members |
|
281
|
|
|
* @param array $json The json object |
|
282
|
|
|
* |
|
283
|
|
|
* @return boolean |
|
284
|
|
|
*/ |
|
285
|
39 |
|
private static function containsAtLeastOneMember($expected, $json): bool |
|
286
|
|
|
{ |
|
287
|
39 |
|
$constraint = static::containsAtLeastOneMemberConstraint($expected); |
|
288
|
|
|
|
|
289
|
39 |
|
return $constraint->check($json); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Asserts that a json object contains only members from the provided list. |
|
294
|
|
|
* |
|
295
|
|
|
* @param array<string> $expected The expected members |
|
296
|
|
|
* @param array $json The json object |
|
297
|
|
|
* @param string $message An optional message to explain why the test failed |
|
298
|
|
|
* |
|
299
|
|
|
* @return void |
|
300
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
301
|
|
|
*/ |
|
302
|
309 |
|
public static function assertContainsOnlyAllowedMembers($expected, $json, string $message = ''): void |
|
303
|
|
|
{ |
|
304
|
309 |
|
$message = Messages::ONLY_ALLOWED_MEMBERS . "\n" . $message; |
|
305
|
309 |
|
PHPUnit::assertThat($json, self::containsOnlyAllowedMembersConstraint($expected), $message); |
|
306
|
255 |
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Returns a new instance of the \VGirol\JsonApiAssert\Constraint\ContainsOnlyAllowedMembersConstraint class. |
|
310
|
|
|
* |
|
311
|
|
|
* @param array $expected The expected members |
|
312
|
|
|
* |
|
313
|
|
|
* @return \VGirol\JsonApiAssert\Constraint\ContainsOnlyAllowedMembersConstraint |
|
314
|
|
|
*/ |
|
315
|
309 |
|
private static function containsOnlyAllowedMembersConstraint($expected): ContainsOnlyAllowedMembersConstraint |
|
316
|
|
|
{ |
|
317
|
309 |
|
return new ContainsOnlyAllowedMembersConstraint($expected); |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|