AssertDeleted   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 19
c 1
b 0
f 0
dl 0
loc 47
ccs 16
cts 16
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertIsDeletedResponse() 0 35 2
1
<?php
2
3
namespace VGirol\JsonApiAssert\Laravel\Asserts\Response;
4
5
use Illuminate\Testing\TestResponse;
6
use PHPUnit\Framework\Assert as PHPUnit;
7
use VGirol\JsonApiAssert\Laravel\HttpHeader;
8
use VGirol\JsonApiAssert\Laravel\Messages;
9
use VGirol\JsonApiConstant\Members;
10
11
/**
12
 * This trait adds the ability to test response returned after resource deletion.
13
 */
14
trait AssertDeleted
15
{
16
    /**
17
     * Asserts that a response object is a valid "200 OK" response following a deletion request.
18
     *
19
     * @param TestResponse $response
20
     * @param array|null   $expectedMeta If not null, it is the expected "meta" object.
21
     * @param boolean      $strict       If true, unsafe characters are not allowed when checking members name.
22
     *
23
     * @return void
24
     * @throws \PHPUnit\Framework\AssertionFailedError
25
     */
26 27
    public static function assertIsDeletedResponse(
27
        TestResponse $response,
28
        $expectedMeta,
29
        bool $strict
30
    ) {
31 27
        $response->assertStatus(200);
32 24
        $response->assertHeader(
33 24
            HttpHeader::HEADER_NAME,
34 24
            HttpHeader::MEDIA_TYPE
35
        );
36
37
        // Decode JSON response
38 21
        $json = $response->json();
39
40
        // Checks response structure
41 21
        static::assertHasValidStructure(
42 21
            $json,
43
            $strict
44
        );
45
46 12
        static::assertContainsOnlyAllowedMembers(
47
            [
48 12
                Members::META,
49
                Members::JSONAPI
50
            ],
51
            $json
52
        );
53
54
        // Checks meta object
55 9
        $meta = $json[Members::META];
56 9
        if ($expectedMeta !== null) {
57 9
            PHPUnit::assertEquals(
58 9
                $expectedMeta,
59 9
                $meta,
60 9
                Messages::META_OBJECT_IS_NOT_AS_EXPECTED
61
            );
62
        }
63 6
    }
64
}
65