1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiAssert\Asserts\Structure; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Assertions relating to the overall structure of the document |
9
|
|
|
*/ |
10
|
|
|
trait AssertStructure |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Asserts that a json document has valid structure. |
14
|
|
|
* |
15
|
|
|
* It will do the following checks : |
16
|
|
|
* 1) checks top-level members (@see assertHasValidTopLevelMembers) |
17
|
|
|
* |
18
|
|
|
* Optionaly, if presents, it will checks : |
19
|
|
|
* 2) primary data (@see assertIsValidPrimaryData) |
20
|
|
|
* 3) errors object (@see assertIsValidErrorsObject) |
21
|
|
|
* 4) meta object (@see assertIsValidMetaObject) |
22
|
|
|
* 5) jsonapi object (@see assertIsValidJsonapiObject) |
23
|
|
|
* 6) top-level links object (@see assertIsValidTopLevelLinksMember) |
24
|
|
|
* 7) included object (@see assertIsValidIncludedCollection) |
25
|
|
|
* |
26
|
|
|
* @param array $json |
27
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
31
|
|
|
*/ |
32
|
27 |
|
public static function assertHasValidStructure($json, bool $strict): void |
33
|
|
|
{ |
34
|
27 |
|
static::askService('validateStructure', $json, $strict); |
35
|
6 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Asserts that a json document has valid top-level structure. |
39
|
|
|
* |
40
|
|
|
* It will do the following checks : |
41
|
|
|
* 1) asserts that the json document contains at least one of the following top-level members : |
42
|
|
|
* "data", "meta" or "errors" (@see assertContainsAtLeastOneMember). |
43
|
|
|
* 2) asserts that the members "data" and "errors" does not coexist in the same document. |
44
|
|
|
* 3) asserts that the json document contains only the following members : |
45
|
|
|
* "data", "errors", "meta", "jsonapi", "links", "included" (@see assertContainsOnlyAllowedMembers). |
46
|
|
|
* 4) if the json document does not contain a top-level "data" member, the "included" member must not |
47
|
|
|
* be present either. |
48
|
|
|
|
49
|
|
|
* @param array $json |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
53
|
|
|
*/ |
54
|
15 |
|
public static function assertHasValidTopLevelMembers($json): void |
55
|
|
|
{ |
56
|
15 |
|
static::askService('validateTopLevelMembers', $json); |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Asserts that a json fragment is a valid top-level links member. |
61
|
|
|
* |
62
|
|
|
* It will do the following checks : |
63
|
|
|
* 1) asserts that the top-level "links" member contains only the following allowed members : |
64
|
|
|
* "self", "related", "first", "last", "next", "prev" (@see assertIsValidLinksObject). |
65
|
|
|
* |
66
|
|
|
* @param array $json |
67
|
|
|
* @param boolean $withPagination |
68
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
72
|
|
|
*/ |
73
|
6 |
|
public static function assertIsValidTopLevelLinksMember($json, bool $withPagination, bool $strict): void |
74
|
|
|
{ |
75
|
6 |
|
static::askService('validateTopLevelLinksMember', $json, $withPagination, $strict); |
76
|
3 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Asserts a json fragment is a valid primary data object. |
80
|
|
|
* |
81
|
|
|
* It will do the following checks : |
82
|
|
|
* 1) asserts that the primary data is either an object, an array of objects or the `null` value. |
83
|
|
|
* 2) if the primary data is not null, checks if it is a valid single resource or a valid resource collection |
84
|
|
|
* (@see assertIsValidResourceObject or @see assertIsValidResourceIdentifierObject). |
85
|
|
|
* |
86
|
|
|
* @param array|null $json |
87
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
91
|
|
|
*/ |
92
|
30 |
|
public static function assertIsValidPrimaryData($json, bool $strict): void |
93
|
|
|
{ |
94
|
30 |
|
static::askService('validatePrimaryData', $json, $strict); |
95
|
18 |
|
} |
96
|
|
|
|
97
|
|
|
// /** |
98
|
|
|
// * Asserts that a collection of resource object is valid. |
99
|
|
|
// * |
100
|
|
|
// * @param array $list |
101
|
|
|
// * @param boolean $checkType If true, asserts that all resources of the collection are of same type |
102
|
|
|
// * @param boolean $strict If true, excludes not safe characters when checking members name |
103
|
|
|
// * |
104
|
|
|
// * @return void |
105
|
|
|
// * @throws \PHPUnit\Framework\AssertionFailedError |
106
|
|
|
// */ |
107
|
|
|
// private static function assertIsValidPrimaryCollection($list, bool $checkType, bool $strict): void |
108
|
|
|
// { |
109
|
|
|
// $isResourceObject = null; |
110
|
|
|
// foreach ($list as $index => $resource) { |
111
|
|
|
// if ($checkType) { |
112
|
|
|
// // Assert that all resources of the collection are of same type. |
113
|
|
|
// if ($index == 0) { |
114
|
|
|
// $isResourceObject = static::dataIsResourceObject($resource); |
115
|
|
|
// continue; |
116
|
|
|
// } |
117
|
|
|
|
118
|
|
|
// PHPUnit::assertEquals( |
119
|
|
|
// $isResourceObject, |
120
|
|
|
// static::dataIsResourceObject($resource), |
121
|
|
|
// Messages::PRIMARY_DATA_SAME_TYPE |
122
|
|
|
// ); |
123
|
|
|
// } |
124
|
|
|
|
125
|
|
|
// // Check the resource |
126
|
|
|
// static::assertIsValidPrimarySingle($resource, $strict); |
127
|
|
|
// } |
128
|
|
|
// } |
129
|
|
|
|
130
|
|
|
// /** |
131
|
|
|
// * Assert that a single resource object is valid. |
132
|
|
|
// * |
133
|
|
|
// * @param array $resource |
134
|
|
|
// * @param boolean $strict If true, excludes not safe characters when checking members name |
135
|
|
|
// * |
136
|
|
|
// * @return void |
137
|
|
|
// * @throws \PHPUnit\Framework\AssertionFailedError |
138
|
|
|
// */ |
139
|
|
|
// private static function assertIsValidPrimarySingle($resource, bool $strict): void |
140
|
|
|
// { |
141
|
|
|
// if (static::dataIsResourceObject($resource)) { |
142
|
|
|
// static::assertIsValidResourceObject($resource, $strict); |
143
|
|
|
|
144
|
|
|
// return; |
145
|
|
|
// } |
146
|
|
|
|
147
|
|
|
// static::assertIsValidResourceIdentifierObject($resource, $strict); |
148
|
|
|
// } |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Asserts that a collection of included resources is valid. |
152
|
|
|
* |
153
|
|
|
* It will do the following checks : |
154
|
|
|
* 1) asserts that it is an array of objects (@see assertIsArrayOfObjects). |
155
|
|
|
* 2) asserts that each resource of the collection is valid (@see assertIsValidResourceObject). |
156
|
|
|
* 3) asserts that each resource in the collection corresponds to an existing resource linkage |
157
|
|
|
* present in either primary data, primary data relationships or another included resource. |
158
|
|
|
* 4) asserts that each resource in the collection is unique (i.e. each couple id-type is unique). |
159
|
|
|
* |
160
|
|
|
* @param array $included The included top-level member of the json document. |
161
|
|
|
* @param array $data The primary data of the json document. |
162
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
163
|
|
|
* |
164
|
|
|
* @return void |
165
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
166
|
|
|
*/ |
167
|
15 |
|
public static function assertIsValidIncludedCollection($included, $data, bool $strict): void |
168
|
|
|
{ |
169
|
15 |
|
static::askService('validateIncludedCollection', $included, $data, $strict); |
170
|
3 |
|
} |
171
|
|
|
|
172
|
|
|
// /** |
173
|
|
|
// * Checks if a given json fragment is a resource object. |
174
|
|
|
// * |
175
|
|
|
// * @param array $resource |
176
|
|
|
// * |
177
|
|
|
// * @return bool |
178
|
|
|
// */ |
179
|
|
|
// private static function dataIsResourceObject($resource): bool |
180
|
|
|
// { |
181
|
|
|
// $expected = [ |
182
|
|
|
// Members::ATTRIBUTES, |
183
|
|
|
// Members::RELATIONSHIPS, |
184
|
|
|
// Members::LINKS |
185
|
|
|
// ]; |
186
|
|
|
|
187
|
|
|
// return static::containsAtLeastOneMember($expected, $resource); |
188
|
|
|
// } |
189
|
|
|
|
190
|
|
|
// /** |
191
|
|
|
// * Get all the resource identifier objects (resource linkage) presents in a collection of resource. |
192
|
|
|
// * |
193
|
|
|
// * @param array $data |
194
|
|
|
// * |
195
|
|
|
// * @return array |
196
|
|
|
// */ |
197
|
|
|
// private static function getAllResourceIdentifierObjects($data): array |
198
|
|
|
// { |
199
|
|
|
// $arr = []; |
200
|
|
|
// if (empty($data)) { |
201
|
|
|
// return $arr; |
202
|
|
|
// } |
203
|
|
|
// if (!static::isArrayOfObjects($data)) { |
204
|
|
|
// $data = [$data]; |
205
|
|
|
// } |
206
|
|
|
// foreach ($data as $obj) { |
207
|
|
|
// if (!isset($obj[Members::RELATIONSHIPS])) { |
208
|
|
|
// continue; |
209
|
|
|
// } |
210
|
|
|
// foreach ($obj[Members::RELATIONSHIPS] as $relationship) { |
211
|
|
|
// if (!isset($relationship[Members::DATA])) { |
212
|
|
|
// continue; |
213
|
|
|
// } |
214
|
|
|
// $arr = array_merge( |
215
|
|
|
// $arr, |
216
|
|
|
// static::isArrayOfObjects($relationship[Members::DATA]) ? |
217
|
|
|
// $relationship[Members::DATA] : [$relationship[Members::DATA]] |
218
|
|
|
// ); |
219
|
|
|
// } |
220
|
|
|
// } |
221
|
|
|
|
222
|
|
|
// return $arr; |
223
|
|
|
// } |
224
|
|
|
|
225
|
|
|
// /** |
226
|
|
|
// * Checks if a resource is present in a given array. |
227
|
|
|
// * |
228
|
|
|
// * @param array $needle |
229
|
|
|
// * @param array $arr |
230
|
|
|
// * |
231
|
|
|
// * @return bool |
232
|
|
|
// */ |
233
|
|
|
// private static function existsInArray($needle, $arr): bool |
234
|
|
|
// { |
235
|
|
|
// foreach ($arr as $resIdentifier) { |
236
|
|
|
// $test = $resIdentifier[Members::TYPE] === $needle[Members::TYPE] |
237
|
|
|
// && $resIdentifier[Members::ID] === $needle[Members::ID]; |
238
|
|
|
// if ($test) { |
239
|
|
|
// return true; |
240
|
|
|
// } |
241
|
|
|
// } |
242
|
|
|
|
243
|
|
|
// return false; |
244
|
|
|
// } |
245
|
|
|
} |
246
|
|
|
|