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

assertIsValidResourceLinksObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace VGirol\JsonApiAssert\Asserts;
3
4
use PHPUnit\Framework\Assert as PHPUnit;
5
use VGirol\JsonApiAssert\Messages;
6
7
trait AssertResourceObject
8
{
9
    /**
10
     * Asserts that a resource has valid structure.
11
     *
12
     * @param array $resource
13
     *
14
     * @throws PHPUnit\Framework\ExpectationFailedException
15
     */
16 17
    public static function assertIsValidResourceObject($resource)
17
    {
18 17
        static::assertResourceObjectHasValidTopLevelStructure($resource);
19 14
        static::assertResourceIdMember($resource);
20 13
        static::assertResourceTypeMember($resource);
21
22 12
        if (isset($resource['attributes'])) {
23 11
            static::assertIsValidAttributesObject($resource['attributes']);
0 ignored issues
show
Bug introduced by
The method assertIsValidAttributesObject() does not exist on VGirol\JsonApiAssert\Asserts\AssertResourceObject. Did you maybe mean assertIsValidResourceObject()? ( Ignorable by Annotation )

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

23
            static::/** @scrutinizer ignore-call */ 
24
                    assertIsValidAttributesObject($resource['attributes']);

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...
24
        }
25
26 11
        if (isset($resource['relationships'])) {
27 5
            static::assertIsValidRelationshipsObject($resource['relationships']);
0 ignored issues
show
Bug introduced by
The method assertIsValidRelationshipsObject() does not exist on VGirol\JsonApiAssert\Asserts\AssertResourceObject. Did you maybe mean assertIsValidResourceObject()? ( Ignorable by Annotation )

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

27
            static::/** @scrutinizer ignore-call */ 
28
                    assertIsValidRelationshipsObject($resource['relationships']);

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...
28
        }
29
30 11
        if (isset($resource['links'])) {
31 1
            static::assertIsValidResourceLinksObject($resource['links']);
32
        }
33
34 11
        if (isset($resource['meta'])) {
35 1
            static::assertIsValidMetaObject($resource['meta']);
0 ignored issues
show
Bug introduced by
The method assertIsValidMetaObject() does not exist on VGirol\JsonApiAssert\Asserts\AssertResourceObject. Did you maybe mean assertIsValidResourceObject()? ( Ignorable by Annotation )

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

35
            static::/** @scrutinizer ignore-call */ 
36
                    assertIsValidMetaObject($resource['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...
36
        }
37
38 11
        static::assertHasValidFields($resource);
39 8
    }
40
41
    /**
42
     * Asserts that a resource object has a valid top-level structure.
43
     *
44
     * @param array $resource
45
     *
46
     * @throws PHPUnit\Framework\ExpectationFailedException
47
     */
48 23
    public static function assertResourceObjectHasValidTopLevelStructure($resource)
49
    {
50 23
        PHPUnit::assertIsArray(
51 23
            $resource,
52 23
            Messages::RESOURCE_IS_NOT_ARRAY
53
        );
54
55 21
        PHPUnit::assertArrayHasKey(
56 21
            'id',
57 21
            $resource,
58 21
            Messages::RESOURCE_ID_MEMBER_IS_ABSENT
59
        );
60
61 20
        PHPUnit::assertArrayHasKey(
62 20
            'type',
63 20
            $resource,
64 20
            Messages::RESOURCE_TYPE_MEMBER_IS_ABSENT
65
        );
66
67 19
        static::assertContainsAtLeastOneMember(['attributes', 'relationships', 'links', 'meta'], $resource);
68
69 17
        $allowed = ['id', 'type', 'meta', 'attributes', 'links', 'relationships'];
70 17
        static::assertContainsOnlyAllowedMembers($allowed, $resource);
71 15
    }
72
73
    /**
74
     * Asserts that a resource has a valid id member.
75
     *
76
     * @param array $resource
77
     *
78
     * @throws PHPUnit\Framework\ExpectationFailedException
79
     */
80 38
    public static function assertResourceIdMember($resource)
81
    {
82 38
        PHPUnit::assertNotEmpty(
83 38
            $resource['id'],
84 38
            Messages::RESOURCE_ID_MEMBER_IS_EMPTY
85
        );
86
87 37
        PHPUnit::assertIsString(
88 37
            $resource['id'],
89 37
            Messages::RESOURCE_ID_MEMBER_IS_NOT_STRING
90
        );
91 33
    }
92
93
    /**
94
     * Asserts that a resource has a valid type member.
95
     *
96
     * @param array $resource
97
     *
98
     * @throws PHPUnit\Framework\ExpectationFailedException
99
     */
100 35
    public static function assertResourceTypeMember($resource)
101
    {
102 35
        PHPUnit::assertNotEmpty(
103 35
            $resource['type'],
104 35
            Messages::RESOURCE_TYPE_MEMBER_IS_EMPTY
105
        );
106
107 34
        PHPUnit::assertIsString(
108 34
            $resource['type'],
109 34
            Messages::RESOURCE_TYPE_MEMBER_IS_NOT_STRING
110
        );
111
112 31
        static::assertIsValidMemberName($resource['type']);
113 30
    }
114
115
    /**
116
     * Asserts that a links object extracted from a resource is valid.
117
     *
118
     * @param array $data
119
     *
120
     * @throws PHPUnit\Framework\ExpectationFailedException
121
     */
122 1
    public static function assertIsValidResourceLinksObject($data)
123
    {
124 1
        $allowed = ['self'];
125 1
        static::assertIsValidLinksObject($data, $allowed);
0 ignored issues
show
Bug introduced by
The method assertIsValidLinksObject() does not exist on VGirol\JsonApiAssert\Asserts\AssertResourceObject. Did you maybe mean assertIsValidResourceLinksObject()? ( Ignorable by Annotation )

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

125
        static::/** @scrutinizer ignore-call */ 
126
                assertIsValidLinksObject($data, $allowed);

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...
126 1
    }
127
128
    /**
129
     * Asserts that a resource identifier object is valid.
130
     *
131
     * @param array $resource
132
     *
133
     * @throws PHPUnit\Framework\ExpectationFailedException
134
     */
135 28
    public static function assertIsValidResourceIdentifierObject($resource)
136
    {
137 28
        PHPUnit::assertIsArray(
138 28
            $resource,
139 28
            Messages::RESOURCE_IDENTIFIER_IS_NOT_ARRAY
140
        );
141
142 27
        PHPUnit::assertArrayHasKey(
143 27
            'id',
144 27
            $resource,
145 27
            Messages::RESOURCE_ID_MEMBER_IS_ABSENT
146
        );
147 26
        static::assertResourceIdMember($resource);
148
149 24
        PHPUnit::assertArrayHasKey(
150 24
            'type',
151 24
            $resource,
152 24
            Messages::RESOURCE_TYPE_MEMBER_IS_ABSENT
153
        );
154 23
        static::assertResourceTypeMember($resource);
155
156 22
        $allowed = ['id', 'type', 'meta'];
157 22
        static::assertContainsOnlyAllowedMembers($allowed, $resource);
158
159 19
        if (isset($resource['meta'])) {
160 2
            static::assertIsValidMetaObject($resource['meta']);
161
        }
162 18
    }
163
164
    /**
165
     * Asserts that a resource object has valid fields.
166
     *
167
     * @param array $resource
168
     *
169
     * @throws PHPUnit\Framework\ExpectationFailedException
170
     */
171 15
    public static function assertHasValidFields($resource)
172
    {
173 15
        $bHasAttributes = false;
174 15
        if (isset($resource['attributes'])) {
175 14
            $bHasAttributes = true;
176 14
            foreach (array_keys($resource['attributes']) as $name) {
177 14
                static::assertIsNotForbiddenFieldName($name);
178
            }
179
        }
180
181 13
        if (isset($resource['relationships'])) {
182 8
            foreach (array_keys($resource['relationships']) as $name) {
183 8
                static::assertIsNotForbiddenFieldName($name);
184
185 6
                if ($bHasAttributes) {
186 5
                    PHPUnit::assertArrayNotHasKey(
187 5
                        $name,
188 5
                        $resource['attributes'],
189 5
                        Messages::FIELDS_HAVE_SAME_NAME
190
                    );
191
                }
192
            }
193
        }
194 9
    }
195
196
    /**
197
     * Asserts that a field name is not forbidden.
198
     *
199
     * @param string $name
200
     *
201
     * @throws PHPUnit\Framework\ExpectationFailedException
202
     */
203 15
    public static function assertIsNotForbiddenFieldName($name)
204
    {
205 15
        $forbidden = ['type', 'id'];
206 15
        PHPUnit::assertNotContains(
207 15
            $name,
208 15
            $forbidden,
209 15
            Messages::FIELDS_NAME_NOT_ALLOWED
210
        );
211 15
    }
212
}
213