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); |
|
|
|
|
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))); |
|
|
|
|
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))); |
|
|
|
|
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); |
|
|
|
|
105
|
|
|
$this->assertTrue(LaravelStr::contains($httpResponse, $expected), |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.