Completed
Pull Request — master (#2)
by Mathieu
01:57
created

AssertJsonResponse::seeJsonTypedStructure()   D

Complexity

Conditions 10
Paths 13

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 11.5625

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 15
cts 20
cp 0.75
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 16
nc 13
nop 2
crap 11.5625

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LaravelFr\ApiTesting;
4
5
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests;
6
7
trait AssertJsonResponse
8
{
9
    use AssertArrays, MakesHttpRequests;
10
11
    /**
12
     * Assert that the JSON response has exactly the given structure.
13
     *
14
     * @param  array $structure
15
     * @param  array|null $responseData
16
     *
17
     * @return $this
18
     */
19 1
    public function seeJsonStructureEquals(array $structure, $responseData = null)
20
    {
21 1
        if (!$responseData) {
22 1
            $responseData = $this->decodeResponseJson();
23 1
        }
24
25 1
        return $this->assertArrayStructureEquals($structure, $responseData);
26
    }
27
28
    /**
29
     * Assert that the JSON response has a given typed structure.
30
     *
31
     * @param  array|null  $structure
32
     * @param  array|null  $responseData
33
     * @return $this
34
     */
35 1
    public function seeJsonTypedStructure(array $structure = null, $responseData = null)
36
    {
37 1
        if (is_null($structure)) {
38
            return $this->seeJsonStructure($structure);
39
        }
40
41 1
        if (!$responseData) {
42 1
            $responseData = json_decode($this->response->getContent(), true);
43 1
        }
44
45 1
        foreach ($structure as $key => $type) {
46 1
            if (is_array($type) && $key === '*') {
47
                $this->assertInternalType('array', $responseData);
48
49
                foreach ($responseData as $responseDataItem) {
50
                    $this->seeJsonTypedStructure($structure['*'], $responseDataItem);
51
                }
52 1
            } elseif (is_array($type) && array_key_exists($key, $structure)) {
53 1
                if (is_array($structure[$key])) {
54 1
                    $this->seeJsonTypedStructure($structure[$key], $responseData[$key]);
55 1
                }
56 1
            } else {
57 1
                $this->assertInternalType($type, $responseData[$key]);
58
            }
59 1
        }
60
61 1
        return $this;
62
    }
63
64
    /**
65
     * Return the json response or a part of it as an array.
66
     *
67
     * @param  string $key
68
     *
69
     * @return mixed
70
     */
71 1
    public function jsonResponse($key = null)
72
    {
73 1
        $response = $this->decodeResponseJson();
74
75 1
        return $key ? array_get($response, $key) : $response;
76
    }
77
78 2
    public function decodeResponseJson()
79
    {
80 2
        $decodedResponse = json_decode($this->response->getContent(), true);
81
82 2
        if (is_null($decodedResponse) || $decodedResponse === false) {
83
            $this->fail('Invalid JSON was returned from the route. Perhaps an exception was thrown?');
0 ignored issues
show
Bug introduced by
It seems like fail() 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...
84
        }
85
86 2
        return $decodedResponse;
87
    }
88
}
89