|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiAssert\Asserts\Content; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
|
8
|
|
|
use VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint; |
|
9
|
|
|
use VGirol\JsonApiAssert\Messages; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This trait adds the ability to test links objects. |
|
13
|
|
|
*/ |
|
14
|
|
|
trait AssertLinks |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Asserts that a links object equals an expected links array. |
|
18
|
|
|
* |
|
19
|
|
|
* It will do the following checks : |
|
20
|
|
|
* 1) asserts that the links array length is equal to the expected links array length. |
|
21
|
|
|
* 2) asserts that each expected link is present in the links array. |
|
22
|
|
|
* |
|
23
|
|
|
* @link https://jsonapi.org/format/#document-links |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $expected An expected links array |
|
26
|
|
|
* @param array $links A links object to inspect |
|
27
|
|
|
* |
|
28
|
|
|
* @return void |
|
29
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
|
30
|
|
|
*/ |
|
31
|
9 |
|
public static function assertLinksObjectEquals($expected, $links): void |
|
32
|
|
|
{ |
|
33
|
9 |
|
$countExpected = count($expected); |
|
34
|
9 |
|
$count = count($links); |
|
35
|
9 |
|
PHPUnit::assertEquals( |
|
36
|
9 |
|
$countExpected, |
|
37
|
9 |
|
$count, |
|
38
|
9 |
|
sprintf(Messages::LINKS_OBJECT_HAVE_NOT_SAME_LENGTH, $count, $countExpected) |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
6 |
|
foreach ($expected as $name => $value) { |
|
42
|
6 |
|
static::assertLinksObjectContains($name, $value, $links); |
|
43
|
|
|
} |
|
44
|
3 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Asserts that a links object contains an expected link. |
|
48
|
|
|
* |
|
49
|
|
|
* It will do the following checks : |
|
50
|
|
|
* 1) asserts that the links array length is equal to the expected links array length. |
|
51
|
|
|
* 2) asserts that each expected link is present in the links array. |
|
52
|
|
|
* |
|
53
|
|
|
* @link https://jsonapi.org/format/#document-links |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $name The name of the link |
|
56
|
|
|
* @param array|string|null $expected The expected link value |
|
57
|
|
|
* @param array $links The links object to inspect |
|
58
|
|
|
* |
|
59
|
|
|
* @return void |
|
60
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
|
61
|
|
|
*/ |
|
62
|
15 |
|
public static function assertLinksObjectContains($name, $expected, $links): void |
|
63
|
|
|
{ |
|
64
|
15 |
|
static::assertHasMember($name, $links); |
|
65
|
12 |
|
static::assertLinkObjectEquals($expected, $links[$name]); |
|
66
|
6 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Asserts that a link object equals an expected value. |
|
70
|
|
|
* |
|
71
|
|
|
* @link https://jsonapi.org/format/#document-links |
|
72
|
|
|
* |
|
73
|
|
|
* @param array|string|null $expected The expected link value |
|
74
|
|
|
* @param array|string|null $link The link to test |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
|
78
|
|
|
*/ |
|
79
|
18 |
|
public static function assertLinkObjectEquals($expected, $link): void |
|
80
|
|
|
{ |
|
81
|
18 |
|
PHPUnit::assertThat($link, self::linkEqualsConstraint($expected)); |
|
82
|
9 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns a new instance of the \VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint class. |
|
86
|
|
|
* |
|
87
|
|
|
* @param array|string|null $expected The expected link |
|
88
|
|
|
* |
|
89
|
|
|
* @return \VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint |
|
90
|
|
|
*/ |
|
91
|
18 |
|
private static function linkEqualsConstraint($expected): LinkEqualsConstraint |
|
92
|
|
|
{ |
|
93
|
18 |
|
return new LinkEqualsConstraint($expected); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|