|
1
|
|
|
<?php |
|
2
|
|
|
namespace VGirol\JsonApiAssert\Asserts; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
|
5
|
|
|
use VGirol\JsonApiAssert\Messages; |
|
6
|
|
|
|
|
7
|
|
|
trait AssertErrorsObject |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Asserts that an errors object is valid. |
|
11
|
|
|
* |
|
12
|
|
|
* @param array $errors |
|
13
|
|
|
* |
|
14
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
15
|
|
|
*/ |
|
16
|
5 |
|
public static function assertIsValidErrorsObject($errors) |
|
17
|
|
|
{ |
|
18
|
5 |
|
static::assertIsArrayOfObjects( |
|
19
|
5 |
|
$errors, |
|
20
|
5 |
|
Messages::ERRORS_OBJECT_NOT_ARRAY |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
3 |
|
foreach ($errors as $error) { |
|
24
|
3 |
|
static::assertIsValidErrorObject($error); |
|
25
|
|
|
} |
|
26
|
2 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Asserts that an error object is valid. |
|
30
|
|
|
* |
|
31
|
|
|
* @param array $error |
|
32
|
|
|
* |
|
33
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
34
|
|
|
*/ |
|
35
|
17 |
|
public static function assertIsValidErrorObject($error) |
|
36
|
|
|
{ |
|
37
|
17 |
|
PHPUnit::assertIsArray( |
|
38
|
17 |
|
$error, |
|
39
|
17 |
|
Messages::ERROR_OBJECT_NOT_ARRAY |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
16 |
|
PHPUnit::assertNotEmpty( |
|
43
|
16 |
|
$error, |
|
44
|
16 |
|
Messages::ERROR_OBJECT_NOT_EMPTY |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
15 |
|
$allowed = ['id', 'links', 'status', 'code', 'title', 'details', 'source', 'meta']; |
|
48
|
15 |
|
static::assertContainsOnlyAllowedMembers( |
|
49
|
15 |
|
$allowed, |
|
50
|
15 |
|
$error |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
13 |
|
if (isset($error['status'])) { |
|
54
|
12 |
|
PHPUnit::assertIsString( |
|
55
|
12 |
|
$error['status'], |
|
56
|
12 |
|
Messages::ERROR_STATUS_IS_NOT_STRING |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
12 |
|
if (isset($error['code'])) { |
|
61
|
4 |
|
PHPUnit::assertIsString( |
|
62
|
4 |
|
$error['code'], |
|
63
|
4 |
|
Messages::ERROR_CODE_IS_NOT_STRING |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
11 |
|
if (isset($error['title'])) { |
|
68
|
2 |
|
PHPUnit::assertIsString( |
|
69
|
2 |
|
$error['title'], |
|
70
|
2 |
|
Messages::ERROR_TITLE_IS_NOT_STRING |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
10 |
|
if (isset($error['details'])) { |
|
75
|
2 |
|
PHPUnit::assertIsString( |
|
76
|
2 |
|
$error['details'], |
|
77
|
2 |
|
Messages::ERROR_DETAILS_IS_NOT_STRING |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
9 |
|
if (isset($error['source'])) { |
|
82
|
5 |
|
static::assertIsValidErrorSourceObject($error['source']); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
5 |
|
if (isset($error['links'])) { |
|
86
|
2 |
|
static::assertIsValidErrorLinksObject($error['links']); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
if (isset($error['meta'])) { |
|
90
|
2 |
|
static::assertIsValidMetaObject($error['meta']); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
3 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Asserts that an error links object is valid. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $links |
|
98
|
|
|
* |
|
99
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public static function assertIsValidErrorLinksObject($links) |
|
102
|
|
|
{ |
|
103
|
2 |
|
$allowed = ['about']; |
|
104
|
2 |
|
static::assertIsValidLinksObject($links, $allowed); |
|
|
|
|
|
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Asserts that an error source object is valid. |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $source |
|
111
|
|
|
* |
|
112
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
113
|
|
|
*/ |
|
114
|
11 |
|
public static function assertIsValidErrorSourceObject($source) |
|
115
|
|
|
{ |
|
116
|
11 |
|
PHPUnit::assertIsArray( |
|
117
|
11 |
|
$source, |
|
118
|
11 |
|
Messages::ERROR_SOURCE_OBJECT_NOT_ARRAY |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
// foreach (array_keys($source) as $name) { |
|
122
|
|
|
// static::assertIsValidMemberName($name); |
|
123
|
|
|
// static::assertIsNotForbiddenMemberName($name); |
|
124
|
|
|
// } |
|
125
|
|
|
|
|
126
|
9 |
|
if (isset($source['pointer'])) { |
|
127
|
6 |
|
PHPUnit::assertIsString( |
|
128
|
6 |
|
$source['pointer'], |
|
129
|
6 |
|
Messages::ERROR_SOURCE_POINTER_IS_NOT_STRING |
|
130
|
|
|
); |
|
131
|
4 |
|
PHPUnit::assertStringStartsWith( |
|
132
|
4 |
|
'/', |
|
133
|
4 |
|
$source['pointer'], |
|
134
|
4 |
|
Messages::ERROR_SOURCE_POINTER_START |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
5 |
|
if (isset($source['parameter'])) { |
|
139
|
3 |
|
PHPUnit::assertIsString( |
|
140
|
3 |
|
$source['parameter'], |
|
141
|
3 |
|
Messages::ERROR_SOURCE_PARAMETER_IS_NOT_STRING |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
3 |
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
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.