Completed
Push — master ( bfd868...ee3a72 )
by Vincent
12:29 queued 10:26
created

AssertRelationshipsObject   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 116
ccs 48
cts 51
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A assertIsValidRelationshipLinksObject() 0 7 2
A assertIsValidResourceLinkage() 0 26 5
A assertIsValidRelationshipsObject() 0 7 2
A assertIsValidRelationshipObject() 0 19 4
A isToManyResourceLinkage() 0 15 4
1
<?php
2
namespace VGirol\JsonApiAssert\Asserts;
3
4
use PHPUnit\Framework\Assert as PHPUnit;
5
use VGirol\JsonApiAssert\Messages;
6
use PHPUnit\Framework\ExpectationFailedException;
7
8
trait AssertRelationshipsObject
9
{
10
    /**
11
     * Asserts that a relationships object is valid.
12
     *
13
     * @param array $relationships
14
     * 
15
     * @throws PHPUnit\Framework\ExpectationFailedException
16
     */
17 8
    public static function assertIsValidRelationshipsObject($relationships)
18
    {
19 8
        static::assertIsNotArrayOfObjects($relationships);
20
21 7
        foreach ($relationships as $key => $relationship) {
22 7
            static::assertIsValidMemberName($key);
23 6
            static::assertIsValidRelationshipObject($relationship);
24
        }
25 6
    }
26
27
    /**
28
     * Asserts that a relationship object is valid.
29
     *
30
     * @param array $relationship
31
     * 
32
     * @throws PHPUnit\Framework\ExpectationFailedException
33
     */
34 10
    public static function assertIsValidRelationshipObject($relationship)
35
    {
36 10
        $expected = ['links', 'data', 'meta'];
37 10
        static::assertContainsAtLeastOneMember($expected, $relationship);
38
39 8
        $withPagination = false;
40 8
        if (isset($relationship['data'])) {
41 8
            $data = $relationship['data'];
42 8
            static::assertIsValidResourceLinkage($data);
43 8
            $withPagination = static::isToManyResourceLinkage($data);
44
        }
45
46 8
        if (isset($relationship['links'])) {
47 3
            $links = $relationship['links'];
48 3
            static::assertIsValidRelationshipLinksObject($links, $withPagination);
49
        }
50
51 8
        if (isset($relationship['meta'])) {
52 1
            static::assertIsValidMetaObject($relationship['meta']);
0 ignored issues
show
Bug introduced by
The method assertIsValidMetaObject() does not exist on VGirol\JsonApiAssert\Ass...sertRelationshipsObject. Did you maybe mean assertIsValidRelationshipObject()? ( Ignorable by Annotation )

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

52
            static::/** @scrutinizer ignore-call */ 
53
                    assertIsValidMetaObject($relationship['meta']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
        }
54 8
    }
55
56
    /**
57
     * Asserts that a link object extracted from a relationship object is valid.
58
     *
59
     * @param array     $data
60
     * @param boolean   $withPagination
61
     * 
62
     * @throws PHPUnit\Framework\ExpectationFailedException
63
     */
64 3
    public static function assertIsValidRelationshipLinksObject($data, $withPagination)
65
    {
66 3
        $allowed = ['self', 'related'];
67 3
        if ($withPagination) {
68 1
            $allowed = array_merge($allowed, ['first', 'last', 'next', 'prev']);
69
        }
70 3
        static::assertIsValidLinksObject($data, $allowed);
71 3
    }
72
73
    /**
74
     * Asserts that a resource linkage object is valid.
75
     *
76
     * @param array $data
77
     * 
78
     * @throws PHPUnit\Framework\ExpectationFailedException
79
     */
80 15
    public static function assertIsValidResourceLinkage($data)
81
    {
82
        try {
83 15
            PHPUnit::assertIsArray(
84 15
                $data,
85 15
                Messages::RESOURCE_LINKAGE_NOT_ARRAY
86
            );
87
            try {
88 13
                PHPUnit::assertNotEmpty($data);
89 1
            } catch (ExpectationFailedException $e) {
90 13
                return;
91
            }
92 2
        } catch (ExpectationFailedException $e) {
93 2
            PHPUnit::assertNull(
94 2
                $data,
95 2
                Messages::RESOURCE_LINKAGE_NOT_ARRAY
96
            );
97 1
            return;
98
        }
99
100 12
        if (static::isArrayOfObjects($data)) {
101 3
            foreach ($data as $resource) {
102 3
                static::assertIsValidResourceIdentifierObject($resource);
0 ignored issues
show
Bug introduced by
The method assertIsValidResourceIdentifierObject() does not exist on VGirol\JsonApiAssert\Ass...sertRelationshipsObject. Did you maybe mean assertIsValidResourceLinkage()? ( Ignorable by Annotation )

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

102
                static::/** @scrutinizer ignore-call */ 
103
                        assertIsValidResourceIdentifierObject($resource);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
            }
104
        } else {
105 9
            static::assertIsValidResourceIdentifierObject($data);
106
        }
107 10
    }
108
109 8
    private static function isToManyResourceLinkage($data)
110
    {
111 8
        if (is_null($data)) {
112
            return false;
113
        }
114
115 8
        if (!is_array($data)) {
116
            return false;
117
        }
118
119 8
        if (empty($data)) {
120
            return false;
121
        }
122
123 8
        return static::isArrayOfObjects($data);
124
    }
125
}
126