|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Test; |
|
10
|
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\SchemaValidator; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author John Kleijn <[email protected]> |
|
16
|
|
|
* |
|
17
|
|
|
* @property string env |
|
18
|
|
|
* @property array defaultServerVars |
|
19
|
|
|
*/ |
|
20
|
|
|
trait ApiTestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ApiTestClient |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $client; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var SchemaValidator |
|
29
|
|
|
*/ |
|
30
|
|
|
private $validator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->createApiTestClient(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a client, booting the kernel using SYMFONY_ENV = $this->env |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function createApiTestClient() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->client = new ApiTestClient( |
|
45
|
|
|
static::createClient([ |
|
46
|
|
|
'environment' => $this->getEnv(), |
|
47
|
|
|
'debug' => true |
|
48
|
|
|
]) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getDefaultServerVars(): array |
|
56
|
|
|
{ |
|
57
|
|
|
return isset($this->defaultServerVars) ? $this->defaultServerVars : []; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function getEnv(): string |
|
64
|
|
|
{ |
|
65
|
|
|
return isset($this->env) ? $this->env : 'test'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $path |
|
70
|
|
|
* @param array $params |
|
71
|
|
|
* |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
* @throws ApiResponseErrorException |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function get(string $path, array $params = []) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->request($path, 'GET', $params); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $path |
|
82
|
|
|
* @param array $params |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
* @throws ApiResponseErrorException |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function delete(string $path, array $params = []) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->request($path, 'DELETE', $params); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $path |
|
94
|
|
|
* @param array $content |
|
95
|
|
|
* @param array $params |
|
96
|
|
|
* |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
* @throws ApiResponseErrorException |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function patch(string $path, array $content, array $params = []) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->request($path, 'PATCH', $params, $content); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $path |
|
107
|
|
|
* @param array $content |
|
108
|
|
|
* @param array $params |
|
109
|
|
|
* |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
* @throws ApiResponseErrorException |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function post(string $path, array $content, array $params = []) |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->request($path, 'POST', $params, $content); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $path |
|
120
|
|
|
* @param array $content |
|
121
|
|
|
* @param array $params |
|
122
|
|
|
* |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
* @throws ApiResponseErrorException |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function put(string $path, array $content, array $params = []) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->request($path, 'PUT', $params, $content); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $path |
|
133
|
|
|
* @param string $method |
|
134
|
|
|
* @param array $params |
|
135
|
|
|
* @param array|null $content |
|
136
|
|
|
* |
|
137
|
|
|
* @return mixed |
|
138
|
|
|
* @throws ApiResponseErrorException |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function request(string $path, string $method, array $params = [], array $content = null) |
|
141
|
|
|
{ |
|
142
|
|
|
$apiRequest = new ApiRequest($this->assembleUri($path, $params), $method); |
|
143
|
|
|
$apiRequest->setServer(array_merge(['CONTENT_TYPE' => 'application/json'], $this->getDefaultServerVars())); |
|
144
|
|
|
|
|
145
|
|
|
if ($content !== null) { |
|
146
|
|
|
$apiRequest->setContent(json_encode($content)); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$this->client->requestFromRequest($apiRequest); |
|
150
|
|
|
|
|
151
|
|
|
/** @var Response $response */ |
|
152
|
|
|
$response = $this->client->getResponse(); |
|
153
|
|
|
|
|
154
|
|
|
$body = null; |
|
155
|
|
|
$content = null; |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
if (($content = $response->getContent()) && $response->getStatusCode() !== Response::HTTP_NO_CONTENT) { |
|
158
|
|
|
$body = json_decode($content); |
|
159
|
|
|
$this->assertSame( |
|
160
|
|
|
JSON_ERROR_NONE, |
|
161
|
|
|
json_last_error(), |
|
162
|
|
|
"Not valid JSON: " . json_last_error_msg() . "(" . var_export($content, true) . ")" |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
if (substr((string)$response->getStatusCode(), 0, 1) != '2') { |
|
167
|
|
|
// This throws an exception so that tests can catch it when it is expected |
|
168
|
|
|
throw new ApiResponseErrorException($content, $body, $response->getStatusCode()); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $body; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param string $path |
|
176
|
|
|
* @param array $params |
|
177
|
|
|
* |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
|
|
private function assembleUri(string $path, array $params = []) |
|
181
|
|
|
{ |
|
182
|
|
|
$uri = $path; |
|
183
|
|
|
if (count($params)) { |
|
184
|
|
|
$uri = $path . '?' . http_build_query($params); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $uri; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param mixed $expected |
|
192
|
|
|
* @param mixed $actual |
|
193
|
|
|
* @param string $message |
|
194
|
|
|
* |
|
195
|
|
|
* @return mixed |
|
196
|
|
|
*/ |
|
197
|
|
|
abstract public function assertSame($expected, $actual, $message = ''); |
|
198
|
|
|
} |
|
199
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.