AssertResourceLinkage   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 115
ccs 35
cts 35
cp 1
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assertResourceIdentifierEquals() 0 6 1
A assertResourceIdentifierCollectionEquals() 0 16 2
A assertResourceLinkageEquals() 0 29 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
use VGirol\JsonApiAssert\Messages;
9
10
/**
11
 * This trait adds the ability to test resource linkage (single or collection).
12
 */
13
trait AssertResourceLinkage
14
{
15
    /**
16
     * Asserts that a resource identifier object is equal to an expected resource identifier.
17
     *
18
     * @link https://jsonapi.org/format/#document-resource-object-linkage
19
     * @link https://jsonapi.org/format/#document-resource-identifier-objects
20
     *
21
     * @param array $expected
22
     * @param array $json
23
     *
24
     * @return void
25
     * @throws \PHPUnit\Framework\AssertionFailedError
26
     */
27 27
    public static function assertResourceIdentifierEquals($expected, $json)
28
    {
29 27
        PHPUnit::assertSame(
30 27
            $expected,
31 27
            $json,
32 27
            sprintf(Messages::RESOURCE_IDENTIFIER_IS_NOT_EQUAL, var_export($json, true), var_export($expected, true))
33
        );
34 21
    }
35
36
    /**
37
     * Asserts that an array of resource identifer objects correspond to an expected collection.
38
     *
39
     * It will do the following checks :
40
     * 1) asserts that the given array is an array of objects (@see assertIsArrayOfObjects).
41
     * 2) asserts that the two arrays have the same count of items.
42
     * 3) asserts that each resource identifier object of the array equals the resource identifier
43
     * at the same index in the given expected collection (@see assertResourceIdentifierEquals).
44
     *
45
     * @link https://jsonapi.org/format/#document-resource-object-linkage
46
     * @link https://jsonapi.org/format/#document-resource-identifier-objects
47
     *
48
     * @param array $expected
49
     * @param array $json
50
     *
51
     * @return void
52
     * @throws \PHPUnit\Framework\AssertionFailedError
53
     */
54 21
    public static function assertResourceIdentifierCollectionEquals($expected, $json)
55
    {
56 21
        static::assertIsArrayOfObjects($json);
57
58 18
        $expectedCount = count($expected);
59 18
        $count = count($json);
60 18
        PHPUnit::assertEquals(
61 18
            $expectedCount,
62 18
            $count,
63 18
            sprintf(Messages::RESOURCE_LINKAGE_COLLECTION_HAVE_NOT_SAME_LENGTH, $count, $expectedCount)
64
        );
65
66 15
        $index = 0;
67 15
        foreach ($expected as $resource) {
68 15
            static::assertResourceIdentifierEquals($resource, $json[$index]);
69 15
            $index++;
70
        }
71 9
    }
72
73
    /**
74
     * Asserts that a resource linkage object correspond to a given reference object
75
     *
76
     * Asserts that a resource linkage object correspond to a given reference object
77
     * which can be either the null value, a single resource identifier object,
78
     * an empty collection or a collection of resource identifier ojects.
79
     *
80
     * It will do the following checks :
81
     * 1) if the expected value is `null`, asserts that the resource linkage is `null`.
82
     * 2) if the expected value is a single resource identifier object, asserts that the resource linkage
83
     * is not an array of objects (@see assertIsNotArrayOfObjects)
84
     * and is equal to the given expected object (@see assertResourceIdentifierEquals).
85
     * 3) if the expected value is an empty array, asserts that the resource linkage is an empty array.
86
     * 4) if the expected value is a collection, asserts that the resource linkage corresponds to the given collection
87
     * (@see assertResourceIdentifierCollectionEquals)
88
     *
89
     * @link https://jsonapi.org/format/#document-resource-object-linkage
90
     * @link https://jsonapi.org/format/#document-resource-identifier-objects
91
     *
92
     * @param array|null $expected
93
     * @param array|null $json
94
     * @param boolean    $strict   If true, unsafe characters are not allowed when checking members name.
95
     *
96
     * @return void
97
     * @throws \PHPUnit\Framework\AssertionFailedError
98
     */
99 36
    public static function assertResourceLinkageEquals($expected, $json, $strict)
100
    {
101 36
        static::assertIsValidResourceLinkage($json, $strict);
102
103 33
        if ($expected === null) {
104 6
            PHPUnit::assertNull(
105 6
                $json,
106 6
                sprintf(Messages::RESOURCE_LINKAGE_MUST_BE_NULL, var_export($json, true))
107
            );
108
109 3
            return;
110
        }
111
112 27
        PHPUnit::assertNotNull($json, Messages::RESOURCE_LINKAGE_MUST_NOT_BE_NULL);
113
114 24
        if (!static::isArrayOfObjects($expected)) {
115 9
            static::assertIsNotArrayOfObjects($json);
116 6
            static::assertResourceIdentifierEquals($expected, $json);
117
118 3
            return;
119
        }
120
121 15
        if (count($expected) == 0) {
122 6
            PHPUnit::assertEmpty($json, Messages::RESOURCE_LINKAGE_COLLECTION_MUST_BE_EMPTY);
123
124 3
            return;
125
        }
126
127 9
        static::assertResourceIdentifierCollectionEquals($expected, $json);
128 6
    }
129
}
130