Completed
Push — master ( a787fa...dc9af5 )
by Vincent
05:35
created

assertIsValidErrorLinksObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
declare (strict_types = 1);
3
4
namespace VGirol\JsonApiAssert\Asserts\Structure;
5
6
use PHPUnit\Framework\Assert as PHPUnit;
7
use VGirol\JsonApiAssert\Members;
8
use VGirol\JsonApiAssert\Messages;
9
10
/**
11
 * Assertions relating to the errors object
12
 */
13
trait AssertErrorsObject
14
{
15
    /**
16
     * Asserts that a json fragment is a valid errors object.
17
     *
18
     * @param array     $json
19
     * @param boolean   $strict         If true, unsafe characters are not allowed when checking members name.
20
     * @return void
21
     * @throws \PHPUnit\Framework\ExpectationFailedException
22
     */
23
    public static function assertIsValidErrorsObject($json, bool $strict): void
24
    {
25
        static::assertIsArrayOfObjects(
26
            $json,
27
            Messages::ERRORS_OBJECT_NOT_ARRAY
28
        );
29
30
        foreach ($json as $error) {
31
            static::assertIsValidErrorObject($error, $strict);
32
        }
33
    }
34
35
    /**
36
     * Asserts that a json fragment is a valid error object.
37
     *
38
     * @param array     $json
39
     * @param boolean   $strict         If true, unsafe characters are not allowed when checking members name.
40
     * @return void
41
     * @throws \PHPUnit\Framework\ExpectationFailedException
42
     */
43
    public static function assertIsValidErrorObject($json, bool $strict): void
44
    {
45
        PHPUnit::assertIsArray(
46
            $json,
47
            Messages::ERROR_OBJECT_NOT_ARRAY
48
        );
49
50
        PHPUnit::assertNotEmpty(
51
            $json,
52
            Messages::ERROR_OBJECT_NOT_EMPTY
53
        );
54
55
        $allowed = [
56
            Members::ID,
57
            Members::LINKS,
58
            Members::STATUS,
59
            Members::CODE,
60
            Members::TITLE,
61
            Members::DETAILS,
62
            Members::SOURCE,
63
            Members::META
64
        ];
65
        static::assertContainsOnlyAllowedMembers($allowed, $json);
66
67
        $checks = [
68
            Members::STATUS => Messages::ERROR_STATUS_IS_NOT_STRING,
69
            Members::CODE => Messages::ERROR_CODE_IS_NOT_STRING,
70
            Members::TITLE => Messages::ERROR_TITLE_IS_NOT_STRING,
71
            Members::DETAILS => Messages::ERROR_DETAILS_IS_NOT_STRING
72
        ];
73
74
        foreach ($checks as $member => $failureMsg) {
75
            if (isset($json[$member])) {
76
                PHPUnit::assertIsString($json[$member], $failureMsg);
77
            }
78
        }
79
80
        if (isset($json[Members::SOURCE])) {
81
            static::assertIsValidErrorSourceObject($json[Members::SOURCE]);
82
        }
83
84
        if (isset($json[Members::LINKS])) {
85
            static::assertIsValidErrorLinksObject($json[Members::LINKS], $strict);
86
        }
87
88
        if (isset($json[Members::META])) {
89
            static::assertIsValidMetaObject($json[Members::META], $strict);
0 ignored issues
show
Bug introduced by
The method assertIsValidMetaObject() does not exist on VGirol\JsonApiAssert\Ass...ture\AssertErrorsObject. Did you maybe mean assertIsValidErrorObject()? ( Ignorable by Annotation )

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

89
            static::/** @scrutinizer ignore-call */ 
90
                    assertIsValidMetaObject($json[Members::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...
90
        }
91
    }
92
93
    /**
94
     * Asserts that a json fragment is a valid error links object.
95
     *
96
     * @param array     $json
97
     * @param boolean   $strict         If true, unsafe characters are not allowed when checking members name.
98
     * @return void
99
     * @throws \PHPUnit\Framework\ExpectationFailedException
100
     */
101
    public static function assertIsValidErrorLinksObject($json, bool $strict): void
102
    {
103
        $allowed = [Members::ABOUT];
104
        static::assertIsValidLinksObject($json, $allowed, $strict);
0 ignored issues
show
Bug introduced by
The method assertIsValidLinksObject() does not exist on VGirol\JsonApiAssert\Ass...ture\AssertErrorsObject. Did you maybe mean assertIsValidErrorLinksObject()? ( Ignorable by Annotation )

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

104
        static::/** @scrutinizer ignore-call */ 
105
                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...
105
    }
106
107
    /**
108
     * Asserts that a json fragment is a valid error source object.
109
     *
110
     * @param array $json
111
     * @return void
112
     * @throws \PHPUnit\Framework\ExpectationFailedException
113
     */
114
    public static function assertIsValidErrorSourceObject($json): void
115
    {
116
        PHPUnit::assertIsArray(
117
            $json,
118
            Messages::ERROR_SOURCE_OBJECT_NOT_ARRAY
119
        );
120
121
        if (isset($json[Members::POINTER])) {
122
            PHPUnit::assertIsString(
123
                $json[Members::POINTER],
124
                Messages::ERROR_SOURCE_POINTER_IS_NOT_STRING
125
            );
126
            PHPUnit::assertStringStartsWith(
127
                '/',
128
                $json[Members::POINTER],
129
                Messages::ERROR_SOURCE_POINTER_START
130
            );
131
        }
132
133
        if (isset($json[Members::PARAMETER])) {
134
            PHPUnit::assertIsString(
135
                $json[Members::PARAMETER],
136
                Messages::ERROR_SOURCE_PARAMETER_IS_NOT_STRING
137
            );
138
        }
139
    }
140
}
141