1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Tests; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
6
|
|
|
use Doctrine\Common\DataFixtures\ReferenceRepository; |
7
|
|
|
use Liip\FunctionalTestBundle\Test\WebTestCase; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
|
12
|
|
|
abstract class RestTestCase extends WebTestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ReferenceRepository |
16
|
|
|
*/ |
17
|
|
|
protected $referenceRepository; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Client |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
protected function setUp() |
25
|
|
|
{ |
26
|
|
|
/** @var ORMExecutor $executor */ |
27
|
|
|
$executor = $this->loadFixtures($this->getFixtureClasses()); |
28
|
|
|
$this->referenceRepository = $executor->getReferenceRepository(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Response $response |
33
|
|
|
* @param int $statusCode |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
protected function assertJsonResponse(Response $response, $statusCode = 200) |
38
|
|
|
{ |
39
|
|
|
if (Response::HTTP_NO_CONTENT !== $statusCode) { |
40
|
|
|
$this->assertTrue( |
41
|
|
|
$response->headers->contains('Content-Type', 'application/json'), |
42
|
|
|
sprintf('JSON content type missing, given: %s', $response->headers->get('Content-Type')) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$content = $response->getContent(); |
47
|
|
|
|
48
|
|
|
$decodedContent = json_decode($content, true); |
49
|
|
|
if ($statusCode !== $response->getStatusCode()) { |
50
|
|
|
var_dump($decodedContent); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->assertEquals($statusCode, $response->getStatusCode()); |
54
|
|
|
|
55
|
|
|
return $decodedContent; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $data |
60
|
|
|
*/ |
61
|
|
|
protected function assertLinksAndUnset(array &$data) |
62
|
|
|
{ |
63
|
|
|
$this->assertArrayHasKey('_links', $data); |
64
|
|
|
unset($data['_links']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @deprecated |
69
|
|
|
* |
70
|
|
|
* @param string $url |
71
|
|
|
* @param string $method |
72
|
|
|
* @param array $parameters |
73
|
|
|
* @param array $headers |
74
|
|
|
* @param array $files |
75
|
|
|
* |
76
|
|
|
* @return Response |
77
|
|
|
*/ |
78
|
|
|
protected function requestJson( |
79
|
|
|
$url, |
80
|
|
|
$method = 'GET', |
81
|
|
|
array $parameters = [], |
82
|
|
|
array $headers = [], |
83
|
|
|
array $files = [] |
84
|
|
|
) { |
85
|
|
|
$mergedHeaders = [ |
86
|
|
|
'HTTP_ACCEPT' => 'application/json', |
87
|
|
|
]; |
88
|
|
|
foreach ($headers as $key => $value) { |
89
|
|
|
$mergedHeaders['HTTP_' . $key] = $value; |
90
|
|
|
} |
91
|
|
|
$this->client->request($method, $url, $parameters, $files, $mergedHeaders); |
92
|
|
|
|
93
|
|
|
return $this->client->getResponse(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $expected |
98
|
|
|
* @param array $actual |
99
|
|
|
* @param bool $linksExpected |
100
|
|
|
*/ |
101
|
|
|
protected function assertContentEquals(array $expected, array $actual, $linksExpected = true) |
102
|
|
|
{ |
103
|
|
|
if ($linksExpected) { |
104
|
|
|
$this->assertLinksAndUnset($actual); |
105
|
|
|
} |
106
|
|
|
ksort($expected); |
107
|
|
|
ksort($actual); |
108
|
|
|
$this->assertEquals($expected, $actual, 'The content does not match'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $url |
113
|
|
|
* @param array $parameters |
114
|
|
|
* @param array $headers |
115
|
|
|
* |
116
|
|
|
* @return null|Response |
117
|
|
|
*/ |
118
|
|
|
protected function doGetCall($url, array $parameters = [], array $headers = []) |
119
|
|
|
{ |
120
|
|
|
$this->client->request( |
121
|
|
|
Request::METHOD_GET, |
122
|
|
|
$url, |
123
|
|
|
$parameters, |
124
|
|
|
[], |
125
|
|
|
$this->transformHeaders($headers) |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
return $this->client->getResponse(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $url |
133
|
|
|
* @param array $parameters |
134
|
|
|
* @param array $headers |
135
|
|
|
* @param array $content |
136
|
|
|
* @param array $files |
137
|
|
|
* |
138
|
|
|
* @return null|Response |
139
|
|
|
*/ |
140
|
|
|
protected function doPostCall( |
141
|
|
|
$url, |
142
|
|
|
array $parameters = [], |
143
|
|
|
array $headers = [], |
144
|
|
|
array $content = [], |
145
|
|
|
array $files = [] |
146
|
|
|
) { |
147
|
|
|
$this->client->request( |
148
|
|
|
Request::METHOD_POST, |
149
|
|
|
$url, |
150
|
|
|
$parameters, |
151
|
|
|
$files, |
152
|
|
|
$this->transformHeaders($headers), |
153
|
|
|
json_encode($content) |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
return $this->client->getResponse(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $url |
161
|
|
|
* @param array $parameters |
162
|
|
|
* @param array $headers |
163
|
|
|
* @param string|null $content |
164
|
|
|
* |
165
|
|
|
* @return null|Response |
166
|
|
|
*/ |
167
|
|
|
protected function doPutCall($url, array $parameters = [], array $headers = [], array $content = []) |
168
|
|
|
{ |
169
|
|
|
$this->client->request( |
170
|
|
|
Request::METHOD_PUT, |
171
|
|
|
$url, |
172
|
|
|
$parameters, |
173
|
|
|
[], |
174
|
|
|
$this->transformHeaders($headers), |
175
|
|
|
json_encode($content) |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
return $this->client->getResponse(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $url |
183
|
|
|
* @param array $parameters |
184
|
|
|
* @param array $headers |
185
|
|
|
* |
186
|
|
|
* @return null|Response |
187
|
|
|
*/ |
188
|
|
|
protected function doDeleteCall($url, array $parameters = [], array $headers = []) |
189
|
|
|
{ |
190
|
|
|
$this->client->request( |
191
|
|
|
Request::METHOD_DELETE, |
192
|
|
|
$url, |
193
|
|
|
$parameters, |
194
|
|
|
[], |
195
|
|
|
$this->transformHeaders($headers) |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
return $this->client->getResponse(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param array $headers |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
protected function transformHeaders(array $headers) |
207
|
|
|
{ |
208
|
|
|
$transformedHeaders = [ |
209
|
|
|
'HTTP_ACCEPT' => 'application/json', |
210
|
|
|
'CONTENT_TYPE' => 'application/json' |
211
|
|
|
]; |
212
|
|
|
foreach ($headers as $key => $value) { |
213
|
|
|
$transformedHeaders['HTTP_' . $key] = $value; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $transformedHeaders; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param Response $response |
221
|
|
|
* @param int $page |
222
|
|
|
* @param int $perPage |
223
|
|
|
* @param int $totalPages |
224
|
|
|
* @param int $total |
225
|
|
|
*/ |
226
|
|
|
protected function assertPagination($response, $page, $perPage, $totalPages, $total) |
227
|
|
|
{ |
228
|
|
|
$headers = $response->headers; |
229
|
|
|
|
230
|
|
|
$this->assertEquals($page, $headers->get('x-pagination-current-page')); |
231
|
|
|
$this->assertEquals($perPage, $headers->get('x-pagination-per-page')); |
232
|
|
|
$this->assertEquals($totalPages, $headers->get('x-pagination-total-pages')); |
233
|
|
|
$this->assertEquals($total, $headers->get('x-pagination-total')); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return string[] |
238
|
|
|
*/ |
239
|
|
|
abstract protected function getFixtureClasses(); |
240
|
|
|
} |
241
|
|
|
|