1
|
|
|
<?php |
2
|
|
|
declare (strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace VGirol\JsonApiAssert\Asserts\Structure; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
7
|
|
|
use VGirol\JsonApiAssert\Members; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Assertions relating to the relationships object |
11
|
|
|
*/ |
12
|
|
|
trait AssertRelationshipsObject |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Asserts that a json fragment is a valid relationships object. |
16
|
|
|
* |
17
|
|
|
* @param array $json |
18
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
19
|
|
|
* @return void |
20
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
21
|
|
|
*/ |
22
|
|
|
public static function assertIsValidRelationshipsObject($json, bool $strict): void |
23
|
|
|
{ |
24
|
|
|
static::assertIsNotArrayOfObjects($json); |
25
|
|
|
|
26
|
|
|
foreach ($json as $key => $relationship) { |
27
|
|
|
static::assertIsValidMemberName($key, $strict); |
28
|
|
|
static::assertIsValidRelationshipObject($relationship, $strict); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Asserts that a json fragment is a valid relationship object. |
34
|
|
|
* |
35
|
|
|
* @param array $json |
36
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
37
|
|
|
* @return void |
38
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
39
|
|
|
*/ |
40
|
|
|
public static function assertIsValidRelationshipObject($json, bool $strict): void |
41
|
|
|
{ |
42
|
|
|
PHPUnit::assertIsArray($json); |
43
|
|
|
|
44
|
|
|
static::assertContainsAtLeastOneMember( |
45
|
|
|
[ |
46
|
|
|
Members::LINKS, |
47
|
|
|
Members::DATA, |
48
|
|
|
Members::META |
49
|
|
|
], |
50
|
|
|
$json |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
if (isset($json[Members::DATA])) { |
54
|
|
|
$data = $json[Members::DATA]; |
55
|
|
|
static::assertIsValidResourceLinkage($data, $strict); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (isset($json[Members::LINKS])) { |
59
|
|
|
$links = $json[Members::LINKS]; |
60
|
|
|
$withPagination = isset($json[Members::DATA]) && static::isArrayOfObjects($json[Members::DATA]); |
61
|
|
|
static::assertIsValidRelationshipLinksObject($links, $withPagination, $strict); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (isset($json[Members::META])) { |
65
|
|
|
static::assertIsValidMetaObject($json[Members::META], $strict); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Asserts that a json fragment is a valid link object extracted from a relationship object. |
71
|
|
|
* |
72
|
|
|
* @param array $json |
73
|
|
|
* @param boolean $withPagination |
74
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
75
|
|
|
* @return void |
76
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
77
|
|
|
*/ |
78
|
|
|
public static function assertIsValidRelationshipLinksObject($json, bool $withPagination, bool $strict): void |
79
|
|
|
{ |
80
|
|
|
$allowed = [ |
81
|
|
|
Members::SELF, |
82
|
|
|
Members::RELATED |
83
|
|
|
]; |
84
|
|
|
if ($withPagination) { |
85
|
|
|
$allowed = array_merge( |
86
|
|
|
$allowed, |
87
|
|
|
[ |
88
|
|
|
Members::FIRST, |
89
|
|
|
Members::LAST, |
90
|
|
|
Members::NEXT, |
91
|
|
|
Members::PREV |
92
|
|
|
] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
static::assertIsValidLinksObject($json, $allowed, $strict); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.