Completed
Push — master ( 6fa789...1e4339 )
by Mathieu
06:26
created

AssertJsonResponse::addJsonResponseMacros()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 4
cts 8
cp 0.5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 1.125
1
<?php
2
3
namespace LaravelFr\ApiTesting;
4
5
use Illuminate\Foundation\Testing\TestResponse;
6
7
trait AssertJsonResponse
8
{
9
    use AssertArrays;
10
11
    public function __construct()
12
    {
13
        if (class_exists(TestResponse::class)) {
14
            $this->addJsonResponseMacros();
15
        }
16
    }
17
18
    /**
19
     * Add the functions to the TestResponse objects.
20
     */
21 1
    public function addJsonResponseMacros()
22
    {
23
        $trait = $this;
24
25
        TestResponse::macro('seeJsonStructureEquals', function ($structure) use ($trait) {
26 1
            return $trait->seeJsonStructureEquals($structure, $this->decodeResponseJson());
1 ignored issue
show
Bug introduced by
It seems like decodeResponseJson() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
        });
28
29 1
        TestResponse::macro('jsonResponse', function ($key = null) use ($trait) {
30 1
            return $trait->jsonResponse($key, $this->decodeResponseJson());
1 ignored issue
show
Bug introduced by
It seems like decodeResponseJson() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
31
        });
32
    }
33
34
    /**
35
     * Assert that the JSON response has exactly the given structure.
36
     *
37
     * @param  array $structure
38
     * @param  array|null $responseData
39
     *
40
     * @return $this
41
     */
42 2
    public function seeJsonStructureEquals($structure, $responseData = null)
43
    {
44 2
        if (!$responseData) {
45 1
            $responseData = $this->decodeResponseJson();
1 ignored issue
show
Bug introduced by
It seems like decodeResponseJson() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
46
        }
47
48 2
        return $this->assertArrayStructureEquals($structure, $responseData);
49
    }
50
51
    /**
52
     * Return the json response or a part of it as an array.
53
     *
54
     * @param  string $key
55
     * @param  array|null $responseData
56
     *
57
     * @return mixed
58
     */
59 2
    public function jsonResponse($key = null, $responseData = null)
60
    {
61 2
        if (!$responseData) {
62 1
            $responseData = $this->decodeResponseJson();
1 ignored issue
show
Bug introduced by
It seems like decodeResponseJson() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63
        }
64
65 2
        return $key ? array_get($responseData, $key) : $responseData;
66
    }
67
}
68