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