1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apiato\Core\Traits\TestsTraits\PhpUnit; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr as LaravelArr; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Illuminate\Support\Str as LaravelStr; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class TestsResponseHelperTrait |
11
|
|
|
* |
12
|
|
|
* Tests helper for making formatting and asserting http responses. |
13
|
|
|
* |
14
|
|
|
* @author Mahmoud Zalt <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
trait TestsResponseHelperTrait |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param $keys |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
public function assertResponseContainKeys($keys) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
if (!is_array($keys)) { |
25
|
|
|
$keys = (array)$keys; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$arrayResponse = $this->removeDataKeyFromResponse($this->getResponseContentArray()); |
|
|
|
|
29
|
|
|
|
30
|
|
|
foreach ($keys as $key) { |
31
|
|
|
$this->assertTrue(array_key_exists($key, $arrayResponse)); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $values |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function assertResponseContainValues($values) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
if (!is_array($values)) { |
41
|
|
|
$values = (array)$values; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$arrayResponse = $this->removeDataKeyFromResponse($this->getResponseContentArray()); |
|
|
|
|
45
|
|
|
|
46
|
|
|
foreach ($values as $value) { |
47
|
|
|
$this->assertTrue(in_array($value, $arrayResponse)); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $data |
53
|
|
|
*/ |
54
|
|
|
public function assertResponseContainKeyValue($data) |
55
|
|
|
{ |
56
|
|
|
// `responseContentToArray` will remove the `data` node |
57
|
|
|
$httpResponse = json_encode(LaravelArr::sortRecursive((array)$this->getResponseContentArray())); |
58
|
|
|
|
59
|
|
|
foreach (LaravelArr::sortRecursive($data) as $key => $value) { |
60
|
|
|
$expected = $this->formatToExpectedJson($key, $value); |
61
|
|
|
$this->assertTrue(LaravelStr::contains($httpResponse, $expected), |
|
|
|
|
62
|
|
|
"The JSON fragment [ {$expected} ] does not exist in the response [ {$httpResponse} ]."); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $messages |
68
|
|
|
*/ |
69
|
|
|
public function assertValidationErrorContain(array $messages) |
70
|
|
|
{ |
71
|
|
|
$responseContent = $this->getResponseContentObject(); |
|
|
|
|
72
|
|
|
|
73
|
|
|
foreach ($messages as $key => $value) { |
74
|
|
|
$this->assertEquals($responseContent->errors->{$key}[0], $value); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $key |
80
|
|
|
* @param $value |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function formatToExpectedJson($key, $value) |
85
|
|
|
{ |
86
|
|
|
$expected = json_encode([$key => $value]); |
87
|
|
|
|
88
|
|
|
if (Str::startsWith($expected, '{')) { |
89
|
|
|
$expected = substr($expected, 1); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (Str::endsWith($expected, '}')) { |
93
|
|
|
$expected = substr($expected, 0, -1); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return trim($expected); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array $responseContent |
101
|
|
|
* |
102
|
|
|
* @return array|mixed |
103
|
|
|
*/ |
104
|
|
|
private function removeDataKeyFromResponse(array $responseContent) |
105
|
|
|
{ |
106
|
|
|
if (array_key_exists('data', $responseContent)) { |
107
|
|
|
return $responseContent['data']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $responseContent; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.