|
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
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Assertions relating to the pagination |
|
12
|
|
|
*/ |
|
13
|
|
|
trait AssertLinks |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Asserts that a links object equals an expected links array. |
|
17
|
|
|
* |
|
18
|
|
|
* @param array $expected |
|
19
|
|
|
* @param array $links |
|
20
|
|
|
* @return void |
|
21
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function assertLinksObjectEquals($expected, $links): void |
|
24
|
|
|
{ |
|
25
|
|
|
PHPUnit::assertEquals(count($expected), count($links)); |
|
26
|
|
|
foreach ($expected as $name => $value) { |
|
27
|
|
|
static::assertLinksObjectContains($name, $value, $links); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Asserts that a links object contains an expected link. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $name |
|
35
|
|
|
* @param array $expected |
|
36
|
|
|
* @param array $links |
|
37
|
|
|
* @return void |
|
38
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function assertLinksObjectContains($name, $expected, $links): void |
|
41
|
|
|
{ |
|
42
|
|
|
static::assertHasMember($name, $links); |
|
43
|
|
|
static::assertLinkObjectEquals($expected, $links[$name]); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Asserts that a link object equals an expected value. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string|null $expected |
|
50
|
|
|
* @param array $link |
|
51
|
|
|
* @return void |
|
52
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function assertLinkObjectEquals($expected, $link, string $message = ''): void |
|
55
|
|
|
{ |
|
56
|
|
|
PHPUnit::assertThat($link, self::linkEqualsConstraint($expected), $message); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns a new instance of the \VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint class. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string|null $expected The expected link |
|
63
|
|
|
* |
|
64
|
|
|
* @return \VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint |
|
65
|
|
|
*/ |
|
66
|
|
|
private static function linkEqualsConstraint($expected): LinkEqualsConstraint |
|
67
|
|
|
{ |
|
68
|
|
|
return new LinkEqualsConstraint($expected); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|