-
Notifications
You must be signed in to change notification settings - Fork 3
/
ApiHelpersTrait.php
294 lines (254 loc) · 7.21 KB
/
ApiHelpersTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
namespace AllanSimon\TestHelpers;
use Symfony\Component\HttpFoundation\File\File;
trait ApiHelpersTrait
{
use ClosesConnectionsAfterTestTrait;
use ReflectsAndCleansPropertiesAfterTestTrait;
protected $entity = null;
protected $entityName = null;
protected $responseJson = null;
protected $response = null;
/**
* Perform an HTTP request on the HTTP, by default with Accept set to json
* and an empty body
*/
protected function performClientRequest(
$method,
$urlPath,
$headers = ['HTTP_ACCEPT' => 'application/json'],
$rawRequestBody = null
) {
$this->client->request(
$method,
$urlPath,
[],
[],
$headers,
$rawRequestBody
);
return $this->client->getResponse();
}
//TODO duplicated code between PUT/POST/PATCH
protected function performPATCH($uri, array $data)
{
$jsonHeaders = ['CONTENT_TYPE' => 'application/json'];
$jsonBody = json_encode($data);
$this->response = $this->performClientRequest(
'PATCH',
$uri,
$jsonHeaders,
$jsonBody
);
$this->assignJsonFromResponse();
}
protected function performPUT($uri, array $data)
{
$jsonHeaders = ['CONTENT_TYPE' => 'application/json'];
$jsonBody = json_encode($data);
$this->response = $this->performClientRequest(
'PUT',
$uri,
$jsonHeaders,
$jsonBody
);
$this->assignJsonFromResponse();
}
protected function performPOST($uri, array $data)
{
$jsonHeaders = ['CONTENT_TYPE' => 'application/json'];
$jsonBody = json_encode($data);
$this->response = $this->performClientRequest(
'POST',
$uri,
$jsonHeaders,
$jsonBody
);
$this->assignJsonFromResponse();
}
protected function performGET($uri)
{
$this->response = $this->performClientRequest(
'GET',
$uri
);
$this->assignJsonFromResponse();
}
protected function performDELETE($uri)
{
$this->response = $this->performClientRequest(
'DELETE',
$uri
);
$this->assignJsonFromResponse();
}
/**
* Perform an HTTP request with a Bearer token in the HTTP authorization header.
*/
protected function performAuthenticatedClientRequest($method, $urlPath, $username = null)
{
$username = $username ?: $this->authAsUser;
$this->client = static::createClient(
array(),
array('HTTP_Authorization' => "Bearer {$username}")
);
return $this->performClientRequest($method, $urlPath);
}
protected function assertSameVideo($expectedVideo, $actualVideoData)
{
$this->assertEquals(
$expectedVideo->getId(),
$actualVideoData['id'],
'match ID of search result'
);
$this->assertEquals(
$expectedVideo->getTitle(),
$actualVideoData['title'],
'match title of search result'
);
}
protected function assertJsonResponse(
$response,
$statusCode = 200,
$checkValidJson = true,
$contentType = 'application/json'
) {
$this->assertEquals(
$statusCode,
$response->getStatusCode(),
$response->getContent()
);
if ($checkValidJson) {
$this->assertTrue(
$response->headers->contains('Content-Type', $contentType),
$response->headers
);
$decode = json_decode($response->getContent());
$this->assertTrue(
($decode !== null && $decode !== false),
'is response valid json: ['.$response->getContent().']'
);
}
}
protected function givenLoggedInAs($username)
{
$this->currentUser = $username;
$this->client = static::createClient(
[],
[
'HTTP_Authorization' => "Bearer {$username}",
'HTTP_ACCEPT' => 'application/json',
]
);
}
/**
* Used to load a fixture's entity using its reference name
*/
protected function given($fixtureName)
{
$this->entity = $this->fixtures->getReference($fixtureName);
$this->entityName = $fixtureName;
}
/**
*
*/
protected function refreshEntity()
{
$this->em->refresh($this->entity);
}
protected function assignJsonFromResponse()
{
$this->responseJson = json_decode(
$this->response->getContent(),
true
);
}
protected function assertListElementsHaveFields(array $fields)
{
foreach ($this->responseJson as $object) {
$this->assertArrayHasKeys(
$fields,
$object
);
}
}
protected function assertResponseHasFields(array $fields)
{
$this->assertArrayHasKeys(
$fields,
$this->responseJson
);
}
protected function assertEmptyList()
{
$this->assertCount(
0,
$this->responseJson,
'The list should be empty'
);
}
protected function assertNotEmptyList()
{
$this->assertNotCount(
0,
$this->responseJson,
'The list should not be empty'
);
}
protected function assertArrayHasKeys(
array $needles,
array $haystack
) {
foreach ($needles as $oneNeedle) {
$this->assertArrayHasKey(
$oneNeedle,
$haystack,
'should have property: '.$oneNeedle
);
}
}
protected function assertPermissionError()
{
$this->assertJsonResponse($this->response, 401, false);
}
protected function assertPermissionDenied()
{
$this->assertJsonResponse($this->response, 403, false);
}
protected function assertNotFoundError()
{
$this->assertJsonResponse($this->response, 404, false);
}
protected function assertResponseUnprocessableEntity()
{
$this->assertJsonResponse($this->response, 422, false);
}
protected function assertNoContentResponse()
{
$this->assertJsonResponse($this->response, 204, false);
}
protected function assertBadRequestError()
{
$this->assertJsonResponse($this->response, 400, false);
}
protected function assertCreatedSuccess()
{
$this->assertJsonResponse($this->response, 201, false);
}
protected function assertOkSuccess()
{
$this->assertJsonResponse($this->response, 200, false);
}
protected function assertOutputIsImage(File $image)
{
$this->assertEquals(200, $this->response->getStatusCode());
$this->expectOutputString(
file_get_contents($image),
'defaults to default image'
);
$this->assertEquals(
$image->getMimeType(),
$this->response->headers->get('content-type')
);
}
}