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