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