Completed
Push — master ( faa240...f1a193 )
by Vincent
23:38 queued 08:32
created

AssertPagination   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
eloc 32
dl 0
loc 72
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A assertResponseHasNoPaginationMeta() 0 13 2
A assertResponseHasPaginationLinks() 0 8 1
A assertResponseHasNoPaginationLinks() 0 13 2
A assertResponseHasPagination() 0 4 1
A assertResponseHasPaginationMeta() 0 14 2
A assertResponseHasNoPagination() 0 7 2
1
<?php
2
3
namespace VGirol\JsonApiAssert\Laravel\Asserts\Content;
4
5
use Illuminate\Foundation\Testing\TestResponse;
6
use PHPUnit\Framework\Assert as PHPUnit;
7
use VGirol\JsonApiAssert\Members;
8
9
trait AssertPagination
10
{
11
    public static function assertResponseHasNoPaginationLinks(TestResponse $response): void
12
    {
13
        // Decode JSON response
14
        $json = $response->json();
0 ignored issues
show
Bug introduced by
The method json() does not exist on Illuminate\Foundation\Testing\TestResponse. ( Ignorable by Annotation )

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

14
        /** @scrutinizer ignore-call */ 
15
        $json = $response->json();

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...
15
16
        if (!isset($json[Members::LINKS])) {
17
            PHPUnit::assertTrue(true);
18
19
            return;
20
        }
21
22
        $links = $json[Members::LINKS];
23
        static::assertHasNoPaginationLinks($links);
24
    }
25
26
    public static function assertResponseHasPaginationLinks(TestResponse $response, $expected)
27
    {
28
        // Decode JSON response
29
        $json = $response->json();
30
31
        static::assertHasLinks($json);
32
        $links = $json[Members::LINKS];
33
        static::assertPaginationLinksEquals($expected, $links);
34
    }
35
36
    public static function assertResponseHasNoPaginationMeta(TestResponse $response): void
37
    {
38
        // Decode JSON response
39
        $json = $response->json();
40
41
        if (!isset($json[Members::META])) {
42
            static::assertNotHasMember(Members::META, $json);
43
44
            return;
45
        }
46
47
        $meta = $json[Members::META];
48
        static::assertHasNoPaginationMeta($meta);
49
    }
50
51
    public static function assertResponseHasPaginationMeta(TestResponse $response, $expected, $path = null)
52
    {
53
        // Decode JSON response
54
        $json = $response->json();
55
56
        if ($path !== null) {
57
            $json = static::getJsonFromPath($json, $path);
58
        }
59
60
        static::assertHasMeta($json);
61
        $meta = $json[Members::META];
62
        static::assertHasMember('pagination', $meta);
63
        $pagination = $meta['pagination'];
64
        PHPUnit::assertEquals($expected, $pagination);
65
    }
66
67
    public static function assertResponseHasNoPagination(TestResponse $response)
68
    {
69
        static::assertResponseHasNoPaginationLinks($response);
70
        static::assertResponseHasNoPaginationMeta($response);
71
        if (isset($json[Members::META])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $json seems to never exist and therefore isset should always be false.
Loading history...
72
            $meta = $json[Members::META];
73
            JsonApiAssert::assertNotHasMember('pagination', $meta);
0 ignored issues
show
Bug introduced by
The type VGirol\JsonApiAssert\Lar...s\Content\JsonApiAssert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
        }
75
    }
76
77
    public static function assertResponseHasPagination(TestResponse $response, $expectedLinks, $expectedMeta)
78
    {
79
        static::assertResponseHasPaginationLinks($response, $expectedLinks);
80
        static::assertResponseHasPaginationMeta($response, $expectedMeta);
81
    }
82
}
83