Completed
Push — master ( afc521...1cce3a )
by Vincent
03:19
created

assertIsValidResourceObject()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 16
nop 2
dl 0
loc 23
ccs 13
cts 13
cp 1
crap 5
rs 9.5555
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 json fragment is a valid resource.
11
     *
12
     * @param array     $json
13
     * @param boolean   $strict         If true, unsafe characters are not allowed when checking members name.
14
     *
15
     * @throws PHPUnit\Framework\ExpectationFailedException
16 27
     */
17
    public static function assertIsValidResourceObject($json, $strict)
18 27
    {
19 24
        static::assertResourceObjectHasValidTopLevelStructure($json);
20 22
        static::assertResourceIdMember($json);
21
        static::assertResourceTypeMember($json, $strict);
22 21
23 20
        if (isset($json['attributes'])) {
24
            static::assertIsValidAttributesObject($json['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

24
            static::/** @scrutinizer ignore-call */ 
25
                    assertIsValidAttributesObject($json['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...
25
        }
26 20
27 5
        if (isset($json['relationships'])) {
28
            static::assertIsValidRelationshipsObject($json['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

28
            static::/** @scrutinizer ignore-call */ 
29
                    assertIsValidRelationshipsObject($json['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...
29
        }
30 19
31 2
        if (isset($json['links'])) {
32
            static::assertIsValidResourceLinksObject($json['links'], $strict);
33
        }
34 18
35 4
        if (isset($json['meta'])) {
36
            static::assertIsValidMetaObject($json['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

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

128
        static::/** @scrutinizer ignore-call */ 
129
                assertIsValidLinksObject($json, $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...
129
    }
130
131
    /**
132
     * Asserts that a resource object has valid fields.
133
     *
134
     * @param array $resource
135
     *
136
     * @throws PHPUnit\Framework\ExpectationFailedException
137
     */
138 33
    public static function assertHasValidFields($resource)
139
    {
140 33
        if (isset($resource['attributes'])) {
141 33
            foreach (array_keys($resource['attributes']) as $name) {
142 33
                static::assertIsNotForbiddenResourceFieldName($name);
143
            }
144
        }
145 32
146 32
        if (isset($resource['relationships'])) {
147 32
            foreach (array_keys($resource['relationships']) as $name) {
148 32
                static::assertIsNotForbiddenResourceFieldName($name);
149
150 31
                if (isset($resource['attributes'])) {
151
                    PHPUnit::assertArrayNotHasKey(
152 29
                        $name,
153 29
                        $resource['attributes'],
154 29
                        Messages::FIELDS_HAVE_SAME_NAME
155 29
                    );
156
                }
157 28
            }
158
        }
159 27
    }
160 27
161
    /**
162 23
     * Asserts that a field name is not forbidden.
163 4
     *
164
     * @param string $name
165 20
     *
166
     * @throws PHPUnit\Framework\ExpectationFailedException
167
     */
168
    public static function assertIsNotForbiddenResourceFieldName($name)
169
    {
170
        $forbidden = ['type', 'id'];
171
        PHPUnit::assertNotContains(
172
            $name,
173
            $forbidden,
174 19
            Messages::FIELDS_NAME_NOT_ALLOWED
175
        );
176 19
    }
177
}
178