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