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

AssertResourceLinkage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 1
b 0
f 0
dl 0
loc 67
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assertResourceIdentifierEquals() 0 3 1
A assertResourceIdentifierCollectionEquals() 0 9 2
A assertResourceLinkageEquals() 0 27 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiAssert\Asserts\Content;
6
7
use PHPUnit\Framework\Assert as PHPUnit;
8
9
trait AssertResourceLinkage
10
{
11
    /**
12
     * Asserts that a resource identifier object is equal to an expected resource identifier.
13
     *
14
     * @param array $expected
15
     * @param array $json
16
     */
17
    public static function assertResourceIdentifierEquals($expected, $json)
18
    {
19
        PHPUnit::assertSame($expected, $json);
20
    }
21
22
    /**
23
     * Asserts that an array of resource identifer objects correspond to an expected collection.
24
     *
25
     * @param array $expected
26
     * @param array $json
27
     */
28
    public static function assertResourceIdentifierCollectionEquals($expected, $json)
29
    {
30
        static::assertIsArrayOfObjects($json);
31
        PHPUnit::assertEquals(count($expected), count($json));
32
33
        $index = 0;
34
        foreach ($expected as $resource) {
35
            static::assertResourceIdentifierEquals($resource, $json[$index]);
36
            $index++;
37
        }
38
    }
39
40
    /**
41
     * Asserts that a resource linkage object correspond to a given reference object
42
     * which can be either the null value, a single resource identifier object,
43
     * an empty collection or a collection of resource identifier ojects.
44
     *
45
     * @param array|null $expected
46
     * @param array|null $json
47
     * @param boolean   $strict         If true, unsafe characters are not allowed when checking members name.
48
     */
49
    public static function assertResourceLinkageEquals($expected, $json, $strict)
50
    {
51
        static::assertIsValidResourceLinkage($json, $strict);
52
53
        if (is_null($expected)) {
54
            PHPUnit::assertNull($json);
55
56
            return;
57
        }
58
59
        PHPUnit::assertNotNull($json);
60
        /** @var array $json */
61
62
        if (!static::isArrayOfObjects($expected)) {
63
            static::assertIsNotArrayOfObjects($json);
64
            static::assertResourceIdentifierEquals($expected, $json);
65
66
            return;
67
        }
68
69
        if (count($expected) == 0) {
70
            PHPUnit::assertEmpty($json);
71
72
            return;
73
        }
74
75
        static::assertResourceIdentifierCollectionEquals($expected, $json);
76
    }
77
}
78