Completed
Push — master ( a787fa...dc9af5 )
by Vincent
05:35
created

AssertLinks   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
c 1
b 0
f 0
dl 0
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertLinksObjectEquals() 0 5 2
A linkEqualsConstraint() 0 3 1
A assertLinksObjectContains() 0 4 1
A assertLinkObjectEquals() 0 3 1
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]);
0 ignored issues
show
Bug introduced by
$expected of type array is incompatible with the type null|string expected by parameter $expected of VGirol\JsonApiAssert\Ass...ssertLinkObjectEquals(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        static::assertLinkObjectEquals(/** @scrutinizer ignore-type */ $expected, $links[$name]);
Loading history...
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