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

AssertLinks::assertDocumentLinksObjectEquals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.2559

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 18
ccs 6
cts 10
cp 0.6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.2559
1
<?php
2
3
namespace VGirol\JsonApiAssert\Laravel\Asserts\Content;
4
5
use Illuminate\Foundation\Testing\TestResponse;
6
use VGirol\JsonApiAssert\Members;
7
8
trait AssertLinks
9
{
10
    /**
11
     * Asserts that a links object equals an expected array of links.
12
     *
13
     * @param \Illuminate\Foundation\Testing\TestResponse $response
14
     * @param array $expected
15
     */
16 3
    public static function assertDocumentLinksObjectEquals(TestResponse $response, $expected)
17
    {
18 3
        if (!\is_array($expected)) {
0 ignored issues
show
introduced by
The condition is_array($expected) is always true.
Loading history...
19
            static::invalidArgument(
20
                2,
21
                'array',
22
                $expected
23
            );
24
        }
25
26
        // Decode JSON response
27 3
        $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

27
        /** @scrutinizer ignore-call */ 
28
        $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...
28
29 3
        static::assertHasLinks($json);
30
31 2
        $links = $json[Members::LINKS];
32
33 2
        static::assertLinksObjectEquals($expected, $links);
34 1
    }
35
36
    /**
37
     * Asserts that a links object contains an expected array of links.
38
     *
39
     * @param \Illuminate\Foundation\Testing\TestResponse $response
40
     * @param array $expected
41
     */
42
    public static function assertDocumentLinksObjectContains(TestResponse $response, $expected)
43
    {
44
        if (!\is_array($expected)) {
0 ignored issues
show
introduced by
The condition is_array($expected) is always true.
Loading history...
45
            static::invalidArgument(
46
                2,
47
                'array',
48
                $expected
49
            );
50
        }
51
52
        // Decode JSON response
53
        $json = $response->json();
54
55
        static::assertHasLinks($json);
56
57
        $links = $json[Members::LINKS];
58
59
        foreach ($expected as $name => $value) {
60
            static::assertLinksObjectContains($name, $value, $links);
61
        }
62
    }
63
}
64