AssertErrorResponse   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 15
c 1
b 0
f 0
dl 0
loc 41
ccs 12
cts 12
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertIsErrorResponse() 0 28 1
1
<?php
2
3
namespace VGirol\JsonApiAssert\Laravel\Asserts\Response;
4
5
use Illuminate\Testing\TestResponse;
6
use VGirol\JsonApiAssert\Laravel\HttpHeader;
7
use VGirol\JsonApiConstant\Members;
8
9
/**
10
 * This trait adds the ability to test error response.
11
 */
12
trait AssertErrorResponse
13
{
14
    /**
15
     * Asserts that an error response (status code 4xx) is valid.
16
     *
17
     * @param TestResponse $response
18
     * @param integer $expectedStatusCode
19
     * @param array   $expectedErrors     An array of the expected error objects.
20
     * @param boolean $strict             If true, unsafe characters are not allowed when checking members name.
21
     *
22
     * @return void
23
     * @throws \PHPUnit\Framework\AssertionFailedError
24
     */
25 72
    public static function assertIsErrorResponse(
26
        TestResponse $response,
27
        int $expectedStatusCode,
28
        $expectedErrors,
29
        bool $strict
30
    ) {
31 72
        $response->assertStatus($expectedStatusCode);
32 51
        $response->assertHeader(
33 51
            HttpHeader::HEADER_NAME,
34 51
            HttpHeader::MEDIA_TYPE
35
        );
36
37
        // Decode JSON response
38 48
        $json = $response->json();
39
40
        // Checks response structure
41 48
        static::assertHasValidStructure(
42 48
            $json,
43
            $strict
44
        );
45
46
        // Checks errors member
47 42
        static::assertHasErrors($json);
48 39
        $errors = $json[Members::ERRORS];
49 39
        static::assertErrorsContains(
50 39
            $expectedErrors,
51
            $errors,
52
            $strict
53
        );
54 24
    }
55
}
56