AssertFetched   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertFetchedSingleResourceResponse() 0 33 2
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 fetching response.
11
 */
12
trait AssertFetched
13
{
14
    /**
15
     * Asserts that the response has "200 Ok" status code and valid content.
16
     *
17
     * @param TestResponse $response
18
     * @param array|null   $expected The expected resource object
19
     * @param boolean      $strict   If true, unsafe characters are not allowed when checking members name.
20
     *
21
     * @return void
22
     * @throws \PHPUnit\Framework\AssertionFailedError
23
     */
24 27
    public static function assertFetchedSingleResourceResponse(
25
        TestResponse $response,
26
        $expected,
27
        bool $strict
28
    ) {
29 27
        $response->assertStatus(200);
30 24
        $response->assertHeader(
31 24
            HttpHeader::HEADER_NAME,
32 24
            HttpHeader::MEDIA_TYPE
33
        );
34
35
        // Decode JSON response
36 21
        $json = $response->json();
37
38
        // Checks response structure
39 21
        static::assertHasValidStructure(
40 21
            $json,
41
            $strict
42
        );
43
44
        // Checks data member
45 18
        static::assertHasData($json);
46 12
        $data = $json[Members::DATA];
47
48 12
        if ($data === null) {
49 3
            return;
50
        }
51
52 9
        static::assertIsNotArrayOfObjects($data);
53
54 6
        static::assertResourceObjectEquals(
55 6
            $expected,
56
            $data
57
        );
58 6
    }
59
}
60