Completed
Push — master ( 1b8ca6...90d75c )
by Mahmoud
03:54
created

TestsResponseHelperTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 3
dl 0
loc 117
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseObject() 0 4 1
A responseToArray() 0 12 3
A assertValidationErrorContain() 0 8 2
A assertResponseContainKeys() 0 10 3
A assertResponseContainValues() 0 10 3
A assertResponseContainKeyValue() 0 12 2
A formatToKeyValueToString() 0 14 3
1
<?php
2
3
namespace App\Ship\Features\Tests\PhpUnit;
4
5
use Dingo\Api\Http\Response as DingoAPIResponse;
6
use Illuminate\Http\Response;
7
use Illuminate\Support\Arr as LaravelArr;
8
use Illuminate\Support\Str as LaravelStr;
9
10
/**
11
 * Class TestsResponseHelperTrait
12
 *
13
 * Tests helper for making formatting and asserting http responses.
14
 *
15
 * @author  Mahmoud Zalt  <[email protected]>
16
 */
17
trait TestsResponseHelperTrait
18
{
19
    /**
20
     * get response object, get the string content from it and convert it to an std object
21
     * making it easier to read
22
     *
23
     * @param $httpResponse
24
     *
25
     * @return  mixed
26
     */
27
    public function getResponseObject(Response $httpResponse)
28
    {
29
        return json_decode($httpResponse->getContent());
30
    }
31
32
    /**
33
     * @param $httpResponse
34
     *
35
     * @return  mixed
36
     */
37
    private function responseToArray($httpResponse)
38
    {
39
        if ($httpResponse instanceof \Illuminate\Http\Response) {
40
            $httpResponse = json_decode($httpResponse->getContent(), true);
41
        }
42
43
        if (array_key_exists('data', $httpResponse)) {
44
            $httpResponse = $httpResponse['data'];
45
        }
46
47
        return $httpResponse;
48
    }
49
50
    /**
51
     * @param \Dingo\Api\Http\Response $httpResponse
52
     * @param array                    $messages
53
     */
54
    public function assertValidationErrorContain(DingoAPIResponse $httpResponse, array $messages)
55
    {
56
        $arrayResponse = json_decode($httpResponse->getContent());
57
58
        foreach ($messages as $key => $value) {
59
            $this->assertEquals($arrayResponse->errors->{$key}[0], $value);
0 ignored issues
show
Bug introduced by
It seems like assertEquals() 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...
60
        }
61
    }
62
63
    /**
64
     * @param $keys
65
     * @param $httpResponse
66
     */
67
    public function assertResponseContainKeys($keys, $httpResponse)
68
    {
69
        if (!is_array($keys)) {
70
            $keys = (array)$keys;
71
        }
72
73
        foreach ($keys as $key) {
74
            $this->assertTrue(array_key_exists($key, $this->responseToArray($httpResponse)));
0 ignored issues
show
Bug introduced by
It seems like assertTrue() 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...
75
        }
76
    }
77
78
    /**
79
     * @param $values
80
     * @param $httpResponse
81
     */
82
    public function assertResponseContainValues($values, $httpResponse)
83
    {
84
        if (!is_array($values)) {
85
            $values = (array)$values;
86
        }
87
88
        foreach ($values as $value) {
89
            $this->assertTrue(in_array($value, $this->responseToArray($httpResponse)));
0 ignored issues
show
Bug introduced by
It seems like assertTrue() 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...
90
        }
91
    }
92
93
    /**
94
     * @param $data
95
     * @param $httpResponse
96
     */
97
    public function assertResponseContainKeyValue($data, $httpResponse)
98
    {
99
        $httpResponse = json_encode(LaravelArr::sortRecursive(
100
            (array)$this->responseToArray($httpResponse)
101
        ));
102
103
        foreach (LaravelArr::sortRecursive($data) as $key => $value) {
104
            $expected = $this->formatToExpectedJson($key, $value);
0 ignored issues
show
Bug introduced by
It seems like formatToExpectedJson() 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...
105
            $this->assertTrue(LaravelStr::contains($httpResponse, $expected),
0 ignored issues
show
Bug introduced by
It seems like assertTrue() 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...
106
                "The JSON fragment [ {$expected} ] does not exist in the response [ {$httpResponse} ].");
107
        }
108
    }
109
110
    /**
111
     * Format the given key and value into a JSON string for expectation checks.
112
     *
113
     * @param string $key
114
     * @param mixed  $value
115
     *
116
     * @return string
117
     */
118
    private function formatToKeyValueToString($key, $value)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
119
    {
120
        $expected = json_encode([$key => $value]);
121
122
        if (LaravelStr::startsWith($expected, '{')) {
123
            $expected = substr($expected, 1);
124
        }
125
126
        if (LaravelStr::endsWith($expected, '}')) {
127
            $expected = substr($expected, 0, -1);
128
        }
129
130
        return $expected;
131
    }
132
133
}
134