assertFetchedResourceCollectionResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 9.8333
cc 1
nc 1
nop 3
crap 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 fetching collection response.
11
 */
12
trait AssertFetchedCollection
13
{
14
    /**
15
     * Asserts that the response has "200 Ok" status code and valid content.
16
     *
17
     * @param TestResponse $response
18
     * @param array        $expected The expected collection of resource objects.
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 24
    public static function assertFetchedResourceCollectionResponse(TestResponse $response, $expected, bool $strict)
25
    {
26 24
        $response->assertStatus(200);
27 21
        $response->assertHeader(
28 21
            HttpHeader::HEADER_NAME,
29 21
            HttpHeader::MEDIA_TYPE
30
        );
31
32
        // Decode JSON response
33 18
        $json = $response->json();
34
35
        // Checks response structure
36 18
        static::assertHasValidStructure(
37 18
            $json,
38
            $strict
39
        );
40
41
        // Checks data member
42 15
        static::assertHasData($json);
43 9
        $data = $json[Members::DATA];
44 9
        static::assertResourceCollectionEquals(
45 9
            $expected,
46
            $data
47
        );
48 6
    }
49
}
50