Passed
Push — master ( 1e6084...cf392e )
by Vincent
02:40
created

assertIsNotForbiddenResourceFieldName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
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 27
    public static function assertIsValidResourceObject($resource, $strict)
17
    {
18 27
        static::assertResourceObjectHasValidTopLevelStructure($resource);
19 24
        static::assertResourceIdMember($resource);
20 22
        static::assertResourceTypeMember($resource, $strict);
21
22 21
        if (isset($resource['attributes'])) {
23 20
            static::assertIsValidAttributesObject($resource['attributes'], $strict);
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'], $strict);

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 20
        if (isset($resource['relationships'])) {
27 5
            static::assertIsValidRelationshipsObject($resource['relationships'], $strict);
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'], $strict);

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 19
        if (isset($resource['links'])) {
31 2
            static::assertIsValidResourceLinksObject($resource['links'], $strict);
32
        }
33
34 18
        if (isset($resource['meta'])) {
35 4
            static::assertIsValidMetaObject($resource['meta'], $strict);
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'], $strict);

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 15
        static::assertHasValidFields($resource);
39 13
    }
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 33
    public static function assertResourceObjectHasValidTopLevelStructure($resource)
49
    {
50 33
        PHPUnit::assertIsArray(
51 33
            $resource,
52 33
            Messages::RESOURCE_IS_NOT_ARRAY
53
        );
54
55 31
        PHPUnit::assertArrayHasKey(
56 31
            'id',
57 31
            $resource,
58 31
            Messages::RESOURCE_ID_MEMBER_IS_ABSENT
59
        );
60
61 30
        PHPUnit::assertArrayHasKey(
62 30
            'type',
63 30
            $resource,
64 30
            Messages::RESOURCE_TYPE_MEMBER_IS_ABSENT
65
        );
66
67 29
        static::assertContainsAtLeastOneMember(['attributes', 'relationships', 'links', 'meta'], $resource);
68
69 27
        $allowed = ['id', 'type', 'meta', 'attributes', 'links', 'relationships'];
70 27
        static::assertContainsOnlyAllowedMembers($allowed, $resource);
71 25
    }
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 53
    public static function assertResourceIdMember($resource)
81
    {
82 53
        PHPUnit::assertNotEmpty(
83 53
            $resource['id'],
84 53
            Messages::RESOURCE_ID_MEMBER_IS_EMPTY
85
        );
86
87 52
        PHPUnit::assertIsString(
88 52
            $resource['id'],
89 52
            Messages::RESOURCE_ID_MEMBER_IS_NOT_STRING
90
        );
91 47
    }
92
93
    /**
94
     * Asserts that a resource has a valid type member.
95
     *
96
     * @param array     $resource
97
     * @param boolean   $strict         If true, excludes not safe characters when checking members name
98
     *
99
     * @throws PHPUnit\Framework\ExpectationFailedException
100
     */
101 50
    public static function assertResourceTypeMember($resource, $strict)
102
    {
103 50
        PHPUnit::assertNotEmpty(
104 50
            $resource['type'],
105 50
            Messages::RESOURCE_TYPE_MEMBER_IS_EMPTY
106
        );
107
108 49
        PHPUnit::assertIsString(
109 49
            $resource['type'],
110 49
            Messages::RESOURCE_TYPE_MEMBER_IS_NOT_STRING
111
        );
112
113 46
        static::assertIsValidMemberName($resource['type'], $strict);
114 44
    }
115
116
    /**
117
     * Asserts that a links object extracted from a resource is valid.
118
     *
119
     * @param array     $data
120
     * @param boolean   $strict         If true, excludes not safe characters when checking members name
121
     *
122
     * @throws PHPUnit\Framework\ExpectationFailedException
123
     */
124 4
    public static function assertIsValidResourceLinksObject($data, $strict)
125
    {
126 4
        $allowed = ['self'];
127 4
        static::assertIsValidLinksObject($data, $allowed, $strict);
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

127
        static::/** @scrutinizer ignore-call */ 
128
                assertIsValidLinksObject($data, $allowed, $strict);

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