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