|
1
|
|
|
<?php |
|
2
|
|
|
namespace VGirol\JsonApiAssert\Asserts; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
|
5
|
|
|
use VGirol\JsonApiAssert\Messages; |
|
6
|
|
|
|
|
7
|
|
|
trait AssertResourceObject |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Asserts that a resource has valid structure. |
|
11
|
|
|
* |
|
12
|
|
|
* @param array $resource |
|
13
|
|
|
* |
|
14
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function assertIsValidResourceObject($resource) |
|
17
|
|
|
{ |
|
18
|
|
|
static::assertResourceObjectHasValidTopLevelStructure($resource); |
|
19
|
|
|
static::assertResourceIdMember($resource); |
|
20
|
|
|
static::assertResourceTypeMember($resource); |
|
21
|
|
|
|
|
22
|
|
|
if (isset($resource['attributes'])) { |
|
23
|
|
|
static::assertIsValidAttributesObject($resource['attributes']); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if (isset($resource['relationships'])) { |
|
27
|
|
|
static::assertIsValidRelationshipsObject($resource['relationships']); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if (isset($resource['links'])) { |
|
31
|
|
|
static::assertIsValidResourceLinksObject($resource['links']); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (isset($resource['meta'])) { |
|
35
|
|
|
static::assertIsValidMetaObject($resource['meta']); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
static::assertHasValidFields($resource); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Asserts that a resource object has a valid top-level structure. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $resource |
|
45
|
|
|
* |
|
46
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function assertResourceObjectHasValidTopLevelStructure($resource) |
|
49
|
|
|
{ |
|
50
|
|
|
PHPUnit::assertIsArray( |
|
51
|
|
|
$resource, |
|
52
|
|
|
Messages::RESOURCE_IS_NOT_ARRAY |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
PHPUnit::assertArrayHasKey( |
|
56
|
|
|
'id', |
|
57
|
|
|
$resource, |
|
58
|
|
|
Messages::RESOURCE_ID_MEMBER_IS_ABSENT |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
PHPUnit::assertArrayHasKey( |
|
62
|
|
|
'type', |
|
63
|
|
|
$resource, |
|
64
|
|
|
Messages::RESOURCE_TYPE_MEMBER_IS_ABSENT |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
static::assertContainsAtLeastOneMember(['attributes', 'relationships', 'links', 'meta'], $resource); |
|
68
|
|
|
|
|
69
|
|
|
$allowed = ['id', 'type', 'meta', 'attributes', 'links', 'relationships']; |
|
70
|
|
|
static::assertContainsOnlyAllowedMembers($allowed, $resource); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Asserts that a resource has a valid id member. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $resource |
|
77
|
|
|
* |
|
78
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function assertResourceIdMember($resource) |
|
81
|
|
|
{ |
|
82
|
|
|
PHPUnit::assertNotEmpty( |
|
83
|
|
|
$resource['id'], |
|
84
|
|
|
Messages::RESOURCE_ID_MEMBER_IS_EMPTY |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
PHPUnit::assertIsString( |
|
88
|
|
|
$resource['id'], |
|
89
|
|
|
Messages::RESOURCE_ID_MEMBER_IS_NOT_STRING |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Asserts that a resource has a valid type member. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $resource |
|
97
|
|
|
* |
|
98
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function assertResourceTypeMember($resource) |
|
101
|
|
|
{ |
|
102
|
|
|
PHPUnit::assertNotEmpty( |
|
103
|
|
|
$resource['type'], |
|
104
|
|
|
Messages::RESOURCE_TYPE_MEMBER_IS_EMPTY |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
PHPUnit::assertIsString( |
|
108
|
|
|
$resource['type'], |
|
109
|
|
|
Messages::RESOURCE_TYPE_MEMBER_IS_NOT_STRING |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
static::assertIsValidMemberName($resource['type']); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Asserts that a links object extracted from a resource is valid. |
|
117
|
|
|
* |
|
118
|
|
|
* @param array $data |
|
119
|
|
|
* |
|
120
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function assertIsValidResourceLinksObject($data) |
|
123
|
|
|
{ |
|
124
|
|
|
$allowed = ['self']; |
|
125
|
|
|
static::assertIsValidLinksObject($data, $allowed); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Asserts that a resource identifier object is valid. |
|
130
|
|
|
* |
|
131
|
|
|
* @param array $resource |
|
132
|
|
|
* |
|
133
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function assertIsValidResourceIdentifierObject($resource) |
|
136
|
|
|
{ |
|
137
|
|
|
PHPUnit::assertIsArray( |
|
138
|
|
|
$resource, |
|
139
|
|
|
Messages::RESOURCE_IDENTIFIER_IS_NOT_ARRAY |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
PHPUnit::assertArrayHasKey( |
|
143
|
|
|
'id', |
|
144
|
|
|
$resource, |
|
145
|
|
|
Messages::RESOURCE_ID_MEMBER_IS_ABSENT |
|
146
|
|
|
); |
|
147
|
|
|
static::assertResourceIdMember($resource); |
|
148
|
|
|
|
|
149
|
|
|
PHPUnit::assertArrayHasKey( |
|
150
|
|
|
'type', |
|
151
|
|
|
$resource, |
|
152
|
|
|
Messages::RESOURCE_TYPE_MEMBER_IS_ABSENT |
|
153
|
|
|
); |
|
154
|
|
|
static::assertResourceTypeMember($resource); |
|
155
|
|
|
|
|
156
|
|
|
$allowed = ['id', 'type', 'meta']; |
|
157
|
|
|
static::assertContainsOnlyAllowedMembers($allowed, $resource); |
|
158
|
|
|
|
|
159
|
|
|
if (isset($resource['meta'])) { |
|
160
|
|
|
static::assertIsValidMetaObject($resource['meta']); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Asserts that a resource object has valid fields. |
|
166
|
|
|
* |
|
167
|
|
|
* @param array $resource |
|
168
|
|
|
* |
|
169
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function assertHasValidFields($resource) |
|
172
|
|
|
{ |
|
173
|
|
|
$bHasAttributes = false; |
|
174
|
|
|
if (isset($resource['attributes'])) { |
|
175
|
|
|
$bHasAttributes = true; |
|
176
|
|
|
foreach (array_keys($resource['attributes']) as $name) { |
|
177
|
|
|
static::assertIsNotForbiddenFieldName($name); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if (isset($resource['relationships'])) { |
|
182
|
|
|
foreach (array_keys($resource['relationships']) as $name) { |
|
183
|
|
|
static::assertIsNotForbiddenFieldName($name); |
|
184
|
|
|
|
|
185
|
|
|
if ($bHasAttributes) { |
|
186
|
|
|
PHPUnit::assertArrayNotHasKey( |
|
187
|
|
|
$name, |
|
188
|
|
|
$resource['attributes'], |
|
189
|
|
|
Messages::FIELDS_HAVE_SAME_NAME |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Asserts that a field name is not forbidden. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $name |
|
200
|
|
|
* |
|
201
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
202
|
|
|
*/ |
|
203
|
|
|
public static function assertIsNotForbiddenFieldName($name) |
|
204
|
|
|
{ |
|
205
|
|
|
$forbidden = ['type', 'id']; |
|
206
|
|
|
PHPUnit::assertNotContains( |
|
207
|
|
|
$name, |
|
208
|
|
|
$forbidden, |
|
209
|
|
|
Messages::FIELDS_NAME_NOT_ALLOWED |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.