|
1
|
|
|
<?php |
|
2
|
|
|
namespace VGirol\JsonApiAssert\Asserts; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
|
5
|
|
|
use VGirol\JsonApiAssert\Messages; |
|
6
|
|
|
use PHPUnit\Framework\ExpectationFailedException; |
|
7
|
|
|
|
|
8
|
|
|
trait AssertRelationshipsObject |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Asserts that a relationships object is valid. |
|
12
|
|
|
* |
|
13
|
|
|
* @param array $relationships |
|
14
|
|
|
* |
|
15
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
16
|
|
|
*/ |
|
17
|
8 |
|
public static function assertIsValidRelationshipsObject($relationships) |
|
18
|
|
|
{ |
|
19
|
8 |
|
static::assertIsNotArrayOfObjects($relationships); |
|
20
|
|
|
|
|
21
|
7 |
|
foreach ($relationships as $key => $relationship) { |
|
22
|
7 |
|
static::assertIsValidMemberName($key); |
|
23
|
6 |
|
static::assertIsValidRelationshipObject($relationship); |
|
24
|
|
|
} |
|
25
|
6 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Asserts that a relationship object is valid. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $relationship |
|
31
|
|
|
* |
|
32
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
33
|
|
|
*/ |
|
34
|
10 |
|
public static function assertIsValidRelationshipObject($relationship) |
|
35
|
|
|
{ |
|
36
|
10 |
|
$expected = ['links', 'data', 'meta']; |
|
37
|
10 |
|
static::assertContainsAtLeastOneMember($expected, $relationship); |
|
38
|
|
|
|
|
39
|
8 |
|
$withPagination = false; |
|
40
|
8 |
|
if (isset($relationship['data'])) { |
|
41
|
8 |
|
$data = $relationship['data']; |
|
42
|
8 |
|
static::assertIsValidResourceLinkage($data); |
|
43
|
8 |
|
$withPagination = static::isToManyResourceLinkage($data); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
8 |
|
if (isset($relationship['links'])) { |
|
47
|
3 |
|
$links = $relationship['links']; |
|
48
|
3 |
|
static::assertIsValidRelationshipLinksObject($links, $withPagination); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
8 |
|
if (isset($relationship['meta'])) { |
|
52
|
1 |
|
static::assertIsValidMetaObject($relationship['meta']); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
8 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Asserts that a link object extracted from a relationship object is valid. |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $data |
|
60
|
|
|
* @param boolean $withPagination |
|
61
|
|
|
* |
|
62
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public static function assertIsValidRelationshipLinksObject($data, $withPagination) |
|
65
|
|
|
{ |
|
66
|
3 |
|
$allowed = ['self', 'related']; |
|
67
|
3 |
|
if ($withPagination) { |
|
68
|
1 |
|
$allowed = array_merge($allowed, ['first', 'last', 'next', 'prev']); |
|
69
|
|
|
} |
|
70
|
3 |
|
static::assertIsValidLinksObject($data, $allowed); |
|
71
|
3 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Asserts that a resource linkage object is valid. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $data |
|
77
|
|
|
* |
|
78
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
|
79
|
|
|
*/ |
|
80
|
15 |
|
public static function assertIsValidResourceLinkage($data) |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
15 |
|
PHPUnit::assertIsArray( |
|
84
|
15 |
|
$data, |
|
85
|
15 |
|
Messages::RESOURCE_LINKAGE_NOT_ARRAY |
|
86
|
|
|
); |
|
87
|
|
|
try { |
|
88
|
13 |
|
PHPUnit::assertNotEmpty($data); |
|
89
|
1 |
|
} catch (ExpectationFailedException $e) { |
|
90
|
13 |
|
return; |
|
91
|
|
|
} |
|
92
|
2 |
|
} catch (ExpectationFailedException $e) { |
|
93
|
2 |
|
PHPUnit::assertNull( |
|
94
|
2 |
|
$data, |
|
95
|
2 |
|
Messages::RESOURCE_LINKAGE_NOT_ARRAY |
|
96
|
|
|
); |
|
97
|
1 |
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
12 |
|
if (static::isArrayOfObjects($data)) { |
|
101
|
3 |
|
foreach ($data as $resource) { |
|
102
|
3 |
|
static::assertIsValidResourceIdentifierObject($resource); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} else { |
|
105
|
9 |
|
static::assertIsValidResourceIdentifierObject($data); |
|
106
|
|
|
} |
|
107
|
10 |
|
} |
|
108
|
|
|
|
|
109
|
8 |
|
private static function isToManyResourceLinkage($data) |
|
110
|
|
|
{ |
|
111
|
8 |
|
if (is_null($data)) { |
|
112
|
|
|
return false; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
8 |
|
if (!is_array($data)) { |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
8 |
|
if (empty($data)) { |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
8 |
|
return static::isArrayOfObjects($data); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
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.