1
|
|
|
<?php |
2
|
|
|
namespace VGirol\JsonApiAssert\Asserts; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
5
|
|
|
|
6
|
|
|
trait AssertRelationshipsObject |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Asserts that a json fragment is a valid relationships object. |
10
|
|
|
* |
11
|
|
|
* @param array $json |
12
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
13
|
|
|
* |
14
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
15
|
|
|
*/ |
16
|
|
|
public static function assertIsValidRelationshipsObject($json, $strict) |
17
|
|
|
{ |
18
|
9 |
|
static::assertIsNotArrayOfObjects($json); |
19
|
|
|
|
20
|
9 |
|
foreach ($json as $key => $relationship) { |
21
|
|
|
static::assertIsValidMemberName($key, $strict); |
22
|
8 |
|
static::assertIsValidRelationshipObject($relationship, $strict); |
23
|
8 |
|
} |
24
|
6 |
|
} |
25
|
|
|
|
26
|
5 |
|
/** |
27
|
|
|
* Asserts that a json fragment is a valid relationship object. |
28
|
|
|
* |
29
|
|
|
* @param array $json |
30
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
31
|
|
|
* |
32
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
33
|
|
|
*/ |
34
|
|
|
public static function assertIsValidRelationshipObject($json, $strict) |
35
|
|
|
{ |
36
|
15 |
|
$expected = ['links', 'data', 'meta']; |
37
|
|
|
static::assertContainsAtLeastOneMember($expected, $json); |
38
|
15 |
|
|
39
|
15 |
|
if (isset($json['data'])) { |
40
|
|
|
$data = $json['data']; |
41
|
13 |
|
static::assertIsValidResourceLinkage($data, $strict); |
42
|
12 |
|
} |
43
|
12 |
|
|
44
|
|
|
if (isset($json['links'])) { |
45
|
|
|
$links = $json['links']; |
46
|
12 |
|
$withPagination = isset($json['data']) && static::isArrayOfObjects($json['data']); |
47
|
6 |
|
static::assertIsValidRelationshipLinksObject($links, $withPagination, $strict); |
48
|
6 |
|
} |
49
|
6 |
|
|
50
|
|
|
if (isset($json['meta'])) { |
51
|
|
|
static::assertIsValidMetaObject($json['meta'], $strict); |
|
|
|
|
52
|
10 |
|
} |
53
|
3 |
|
} |
54
|
|
|
|
55
|
9 |
|
/** |
56
|
|
|
* Asserts that a json fragment is a valid link object extracted from a relationship object. |
57
|
|
|
* |
58
|
|
|
* @param array $json |
59
|
|
|
* @param boolean $withPagination |
60
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
61
|
|
|
* |
62
|
|
|
* @throws PHPUnit\Framework\ExpectationFailedException |
63
|
|
|
*/ |
64
|
|
|
public static function assertIsValidRelationshipLinksObject($json, $withPagination, $strict) |
65
|
|
|
{ |
66
|
10 |
|
$allowed = ['self', 'related']; |
67
|
|
|
if ($withPagination) { |
68
|
10 |
|
$allowed = array_merge($allowed, ['first', 'last', 'next', 'prev']); |
69
|
10 |
|
} |
70
|
2 |
|
static::assertIsValidLinksObject($json, $allowed, $strict); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
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.