1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiAssert\Asserts\Structure; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Assertions relating to the resource object |
9
|
|
|
*/ |
10
|
|
|
trait AssertResourceObject |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Asserts that a json fragment is a valid collection of resource objects. |
14
|
|
|
* |
15
|
|
|
* It will do the following checks : |
16
|
|
|
* 1) asserts that the provided resource collection is either an empty array or an array of objects |
17
|
|
|
* (@see assertIsArrayOfObjects). |
18
|
|
|
* 2) asserts that the collection of resources is valid (@see assertIsValidResourceObject). |
19
|
|
|
* |
20
|
|
|
* @param array|null $json |
21
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
25
|
|
|
*/ |
26
|
15 |
|
public static function assertIsValidResourceObjectCollection($json, bool $strict): void |
27
|
|
|
{ |
28
|
15 |
|
static::askService('validateResourceObjectCollection', $json, $strict); |
29
|
6 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Asserts that a json fragment is a valid resource. |
33
|
|
|
* |
34
|
|
|
* It will do the following checks : |
35
|
|
|
* 1) asserts that the resource object has valid top-level structure |
36
|
|
|
* (@see assertResourceObjectHasValidTopLevelStructure). |
37
|
|
|
* 2) asserts that the resource object has valid "type" and "id" members |
38
|
|
|
* (@see assertResourceIdMember and @see assertResourceTypeMember). |
39
|
|
|
* 3) asserts that the resource object has valid fields (@see assertHasValidFields). |
40
|
|
|
* |
41
|
|
|
* Optionaly, if presents, it will checks : |
42
|
|
|
* 4) asserts thats the resource object has valid "attributes" member. |
43
|
|
|
* 5) asserts thats the resource object has valid "relationships" member. |
44
|
|
|
* 6) asserts thats the resource object has valid "links" member. |
45
|
|
|
* 7) asserts thats the resource object has valid "meta" member. |
46
|
|
|
* |
47
|
|
|
* @param array $json |
48
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
52
|
|
|
*/ |
53
|
36 |
|
public static function assertIsValidResourceObject($json, bool $strict): void |
54
|
|
|
{ |
55
|
36 |
|
static::askService('validateResourceObject', $json, $strict); |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Asserts that a resource object has a valid top-level structure. |
60
|
|
|
* |
61
|
|
|
* It will do the following checks : |
62
|
|
|
* 1) asserts that the resource has an "id" member. |
63
|
|
|
* 2) asserts that the resource has a "type" member. |
64
|
|
|
* 3) asserts that the resource contains at least one of the following members : |
65
|
|
|
* "attributes", "relationships", "links", "meta" (@see assertContainsAtLeastOneMember). |
66
|
|
|
* 4) asserts that the resource contains only the following allowed members : |
67
|
|
|
* "id", "type", "meta", "attributes", "links", "relationships" (@see assertContainsOnlyAllowedMembers). |
68
|
|
|
* |
69
|
|
|
* @param array $resource |
70
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
74
|
|
|
*/ |
75
|
18 |
|
public static function assertResourceObjectHasValidTopLevelStructure($resource, bool $strict): void |
76
|
|
|
{ |
77
|
18 |
|
static::askService('validateResourceObjectTopLevelStructure', $resource, $strict); |
78
|
3 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Asserts that a resource id member is valid. |
82
|
|
|
* |
83
|
|
|
* It will do the following checks : |
84
|
|
|
* 1) asserts that the "id" member is not empty. |
85
|
|
|
* 2) asserts that the "id" member is a string. |
86
|
|
|
* |
87
|
|
|
* @param array $resource |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
91
|
|
|
*/ |
92
|
9 |
|
public static function assertResourceIdMember($resource): void |
93
|
|
|
{ |
94
|
9 |
|
static::askService('validateResourceIdMember', $resource); |
95
|
3 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Asserts that a resource type member is valid. |
99
|
|
|
* |
100
|
|
|
* It will do the following checks : |
101
|
|
|
* 1) asserts that the "type" member is not empty. |
102
|
|
|
* 2) asserts that the "type" member is a string. |
103
|
|
|
* 3) asserts that the "type" member has a valid value (@see assertIsValidMemberName). |
104
|
|
|
* |
105
|
|
|
* @param array $resource |
106
|
|
|
* @param boolean $strict If true, excludes not safe characters when checking members name |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
110
|
|
|
*/ |
111
|
15 |
|
public static function assertResourceTypeMember($resource, bool $strict): void |
112
|
|
|
{ |
113
|
15 |
|
static::askService('validateResourceTypeMember', $resource, $strict); |
114
|
3 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Asserts that a json fragment is a valid resource links object. |
118
|
|
|
* |
119
|
|
|
* It will do the following checks : |
120
|
|
|
* 1) asserts that le links object is valid (@see assertIsValidLinksObject) with only "self" member allowed. |
121
|
|
|
* |
122
|
|
|
* @param array $json |
123
|
|
|
* @param boolean $strict If true, excludes not safe characters when checking members name |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
127
|
|
|
*/ |
128
|
6 |
|
public static function assertIsValidResourceLinksObject($json, bool $strict): void |
129
|
|
|
{ |
130
|
6 |
|
static::askService('validateResourceLinksObject', $json, $strict); |
131
|
3 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Asserts that a resource object has valid fields (i.e., resource object’s attributes and its relationships). |
135
|
|
|
* |
136
|
|
|
* It will do the following checks : |
137
|
|
|
* 1) asserts that each attributes member and each relationship name is valid |
138
|
|
|
* (@see assertIsNotForbiddenResourceFieldName) |
139
|
|
|
* |
140
|
|
|
* @param array $resource |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
144
|
|
|
*/ |
145
|
12 |
|
public static function assertHasValidFields($resource): void |
146
|
|
|
{ |
147
|
12 |
|
static::askService('validateFields', $resource); |
148
|
3 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Asserts that a resource field name is not a forbidden name (like "type" or "id"). |
152
|
|
|
* |
153
|
|
|
* @param string $name |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
157
|
|
|
*/ |
158
|
9 |
|
public static function assertIsNotForbiddenResourceFieldName(string $name): void |
159
|
|
|
{ |
160
|
9 |
|
static::askService('isNotForbiddenResourceFieldName', $name); |
161
|
3 |
|
} |
162
|
|
|
} |
163
|
|
|
|