1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sylius\Tests\Controller; |
15
|
|
|
|
16
|
|
|
use Lakion\ApiTestCase\JsonApiTestCase; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Łukasz Chruściel <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class CustomerApiTest extends JsonApiTestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $authorizedHeaderWithContentType = [ |
28
|
|
|
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', |
29
|
|
|
'CONTENT_TYPE' => 'application/json', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private static $authorizedHeaderWithAccept = [ |
36
|
|
|
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', |
37
|
|
|
'ACCEPT' => 'application/json', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @test |
42
|
|
|
*/ |
43
|
|
|
public function it_denies_customer_creation_for_not_authenticated_users() |
44
|
|
|
{ |
45
|
|
|
$this->client->request('POST', '/api/v1/customers/'); |
46
|
|
|
|
47
|
|
|
$response = $this->client->getResponse(); |
48
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
|
public function it_does_not_allow_to_create_customer_without_specifying_required_data() |
55
|
|
|
{ |
56
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
57
|
|
|
|
58
|
|
|
$this->client->request('POST', '/api/v1/customers/', [], [], static::$authorizedHeaderWithContentType); |
59
|
|
|
|
60
|
|
|
$response = $this->client->getResponse(); |
61
|
|
|
$this->assertResponse($response, 'customer/create_validation_fail_response', Response::HTTP_BAD_REQUEST); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
*/ |
67
|
|
|
public function it_does_not_allow_to_create_customer_with_user_without_specifying_required_data() |
68
|
|
|
{ |
69
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
70
|
|
|
|
71
|
|
|
$data = |
72
|
|
|
<<<EOT |
73
|
|
|
{ |
74
|
|
|
"user": { |
75
|
|
|
"enabled": "true" |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
EOT; |
79
|
|
|
|
80
|
|
|
$this->client->request('POST', '/api/v1/customers/', [], [], static::$authorizedHeaderWithContentType, $data); |
81
|
|
|
|
82
|
|
|
$response = $this->client->getResponse(); |
83
|
|
|
$this->assertResponse($response, 'customer/create_with_user_validation_fail_response', Response::HTTP_BAD_REQUEST); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @test |
88
|
|
|
*/ |
89
|
|
|
public function it_allows_to_create_customer_without_user_account() |
90
|
|
|
{ |
91
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
92
|
|
|
|
93
|
|
|
$data = |
94
|
|
|
<<<EOT |
95
|
|
|
{ |
96
|
|
|
"firstName": "John", |
97
|
|
|
"lastName": "Diggle", |
98
|
|
|
"email": "[email protected]", |
99
|
|
|
"gender": "m" |
100
|
|
|
} |
101
|
|
|
EOT; |
102
|
|
|
|
103
|
|
|
$this->client->request('POST', '/api/v1/customers/', [], [], static::$authorizedHeaderWithContentType, $data); |
104
|
|
|
|
105
|
|
|
$response = $this->client->getResponse(); |
106
|
|
|
$this->assertResponse($response, 'customer/create_response', Response::HTTP_CREATED); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @test |
111
|
|
|
*/ |
112
|
|
|
public function it_allows_to_create_customer_with_user_account() |
113
|
|
|
{ |
114
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
115
|
|
|
|
116
|
|
|
$data = |
117
|
|
|
<<<EOT |
118
|
|
|
{ |
119
|
|
|
"firstName": "John", |
120
|
|
|
"lastName": "Diggle", |
121
|
|
|
"email": "[email protected]", |
122
|
|
|
"gender": "m", |
123
|
|
|
"user": { |
124
|
|
|
"plainPassword" : "testPassword" |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
EOT; |
128
|
|
|
|
129
|
|
|
$this->client->request('POST', '/api/v1/customers/', [], [], static::$authorizedHeaderWithContentType, $data); |
130
|
|
|
|
131
|
|
|
$response = $this->client->getResponse(); |
132
|
|
|
$this->assertResponse($response, 'customer/create_with_user_response', Response::HTTP_CREATED); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @test |
137
|
|
|
*/ |
138
|
|
|
public function it_denies_access_to_customers_list_for_not_authenticated_users() |
139
|
|
|
{ |
140
|
|
|
$this->client->request('GET', '/api/v1/customers/'); |
141
|
|
|
|
142
|
|
|
$response = $this->client->getResponse(); |
143
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @test |
148
|
|
|
*/ |
149
|
|
|
public function it_allows_to_get_customers_list() |
150
|
|
|
{ |
151
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
152
|
|
|
$this->loadFixturesFromFile('resources/customers.yml'); |
153
|
|
|
|
154
|
|
|
$this->client->request('GET', '/api/v1/customers/', [], [], static::$authorizedHeaderWithAccept); |
155
|
|
|
|
156
|
|
|
$response = $this->client->getResponse(); |
157
|
|
|
$this->assertResponse($response, 'customer/index_response', Response::HTTP_OK); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @test |
162
|
|
|
*/ |
163
|
|
|
public function it_returns_page_not_found_if_limit_is_set_to_0() |
164
|
|
|
{ |
165
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
166
|
|
|
$this->loadFixturesFromFile('resources/customers.yml'); |
167
|
|
|
|
168
|
|
|
$this->client->request('GET', '/api/v1/customers/?limit=0', [], [], static::$authorizedHeaderWithAccept); |
169
|
|
|
|
170
|
|
|
$response = $this->client->getResponse(); |
171
|
|
|
$this->assertResponse($response, 'customer/page_not_found_response', Response::HTTP_NOT_FOUND); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @test |
176
|
|
|
*/ |
177
|
|
|
public function it_denies_access_to_customer_details_for_not_authenticated_users() |
178
|
|
|
{ |
179
|
|
|
$this->client->request('GET', '/api/v1/customers/1'); |
180
|
|
|
|
181
|
|
|
$response = $this->client->getResponse(); |
182
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @test |
187
|
|
|
*/ |
188
|
|
|
public function it_returns_not_found_response_when_requesting_details_of_a_customer_which_does_not_exist() |
189
|
|
|
{ |
190
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
191
|
|
|
|
192
|
|
|
$this->client->request('GET', '/api/v1/customers/-1', [], [], static::$authorizedHeaderWithAccept); |
193
|
|
|
|
194
|
|
|
$response = $this->client->getResponse(); |
195
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @test |
200
|
|
|
*/ |
201
|
|
|
public function it_returns_only_customer_details_if_no_user_account_is_connected() |
202
|
|
|
{ |
203
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
204
|
|
|
$customers = $this->loadFixturesFromFile('resources/customers.yml'); |
205
|
|
|
|
206
|
|
|
$this->client->request('GET', '/api/v1/customers/'.$customers['customer_Barry']->getId(), [], [], static::$authorizedHeaderWithAccept); |
207
|
|
|
|
208
|
|
|
$response = $this->client->getResponse(); |
209
|
|
|
$this->assertResponse($response, 'customer/show_response', Response::HTTP_OK); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @test |
214
|
|
|
*/ |
215
|
|
|
public function it_shows_customer_and_user_details() |
216
|
|
|
{ |
217
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
218
|
|
|
$customers = $this->loadFixturesFromFile('resources/customers.yml'); |
219
|
|
|
|
220
|
|
|
$this->client->request('GET', '/api/v1/customers/'.$customers['customer_Roy']->getId(), [], [], static::$authorizedHeaderWithAccept); |
221
|
|
|
|
222
|
|
|
$response = $this->client->getResponse(); |
223
|
|
|
$this->assertResponse($response, 'customer/show_with_user_response', Response::HTTP_OK); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @test |
228
|
|
|
*/ |
229
|
|
|
public function it_denies_full_customer_update_for_not_authenticated_users() |
230
|
|
|
{ |
231
|
|
|
$this->client->request('PUT', '/api/v1/customers/1'); |
232
|
|
|
|
233
|
|
|
$response = $this->client->getResponse(); |
234
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @test |
239
|
|
|
*/ |
240
|
|
|
public function it_returns_not_found_response_when_requesting_full_update_of_a_customer_which_does_not_exist() |
241
|
|
|
{ |
242
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
243
|
|
|
|
244
|
|
|
$this->client->request('PUT', '/api/v1/customers/-1', [], [], static::$authorizedHeaderWithAccept); |
245
|
|
|
|
246
|
|
|
$response = $this->client->getResponse(); |
247
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @test |
252
|
|
|
*/ |
253
|
|
|
public function it_does_not_allow_to_update_customer_fully_without_specifying_required_data() |
254
|
|
|
{ |
255
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
256
|
|
|
$customers = $this->loadFixturesFromFile('resources/customers.yml'); |
257
|
|
|
|
258
|
|
|
$this->client->request('PUT', '/api/v1/customers/'.$customers['customer_Oliver']->getId(), [], [], static::$authorizedHeaderWithContentType); |
259
|
|
|
|
260
|
|
|
$response = $this->client->getResponse(); |
261
|
|
|
$this->assertResponse($response, 'customer/update_validation_fail_response', Response::HTTP_BAD_REQUEST); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @test |
266
|
|
|
*/ |
267
|
|
|
public function it_allows_to_update_customer_fully() |
268
|
|
|
{ |
269
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
270
|
|
|
$customers = $this->loadFixturesFromFile('resources/customers.yml'); |
271
|
|
|
|
272
|
|
|
$data = |
273
|
|
|
<<<EOT |
274
|
|
|
{ |
275
|
|
|
"firstName": "John", |
276
|
|
|
"lastName": "Diggle", |
277
|
|
|
"email": "[email protected]", |
278
|
|
|
"gender": "m" |
279
|
|
|
} |
280
|
|
|
EOT; |
281
|
|
|
|
282
|
|
|
$this->client->request('PUT', '/api/v1/customers/'.$customers['customer_Oliver']->getId(), [], [], static::$authorizedHeaderWithContentType, $data); |
283
|
|
|
|
284
|
|
|
$response = $this->client->getResponse(); |
285
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
286
|
|
|
|
287
|
|
|
$this->client->request('GET', '/api/v1/customers/'.$customers['customer_Oliver']->getId(), [], [], static::$authorizedHeaderWithAccept); |
288
|
|
|
|
289
|
|
|
$response = $this->client->getResponse(); |
290
|
|
|
$this->assertResponse($response, 'customer/update_response', Response::HTTP_OK); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @test |
295
|
|
|
*/ |
296
|
|
|
public function it_returns_not_found_response_when_requesting_partial_update_of_a_customer_which_does_not_exist() |
297
|
|
|
{ |
298
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
299
|
|
|
|
300
|
|
|
$this->client->request('PATCH', '/api/v1/customers/-1', [], [], static::$authorizedHeaderWithAccept); |
301
|
|
|
|
302
|
|
|
$response = $this->client->getResponse(); |
303
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @test |
308
|
|
|
*/ |
309
|
|
|
public function it_allows_to_update_customer_partially() |
310
|
|
|
{ |
311
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
312
|
|
|
$customers = $this->loadFixturesFromFile('resources/customers.yml'); |
313
|
|
|
|
314
|
|
|
$data = |
315
|
|
|
<<<EOT |
316
|
|
|
{ |
317
|
|
|
"firstName": "John", |
318
|
|
|
"lastName": "Doe" |
319
|
|
|
} |
320
|
|
|
EOT; |
321
|
|
|
|
322
|
|
|
$this->client->request('PATCH', '/api/v1/customers/'.$customers['customer_Oliver']->getId(), [], [], static::$authorizedHeaderWithContentType, $data); |
323
|
|
|
|
324
|
|
|
$response = $this->client->getResponse(); |
325
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
326
|
|
|
|
327
|
|
|
$this->client->request('GET', '/api/v1/customers/'.$customers['customer_Oliver']->getId(), [], [], static::$authorizedHeaderWithAccept); |
328
|
|
|
|
329
|
|
|
$response = $this->client->getResponse(); |
330
|
|
|
$this->assertResponse($response, 'customer/partial_update_response', Response::HTTP_OK); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @test |
335
|
|
|
*/ |
336
|
|
|
public function it_denies_customer_deletion_for_not_authenticated_users() |
337
|
|
|
{ |
338
|
|
|
$this->client->request('DELETE', '/api/v1/customers/1'); |
339
|
|
|
|
340
|
|
|
$response = $this->client->getResponse(); |
341
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @test |
346
|
|
|
*/ |
347
|
|
|
public function it_returns_not_found_response_when_requesting_deletion_of_a_customer_which_does_not_exist() |
348
|
|
|
{ |
349
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
350
|
|
|
|
351
|
|
|
$this->client->request('DELETE', '/api/v1/customers/-1', [], [], static::$authorizedHeaderWithAccept); |
352
|
|
|
|
353
|
|
|
$response = $this->client->getResponse(); |
354
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @test |
359
|
|
|
*/ |
360
|
|
|
public function it_allows_to_delete_customer() |
361
|
|
|
{ |
362
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
363
|
|
|
$customers = $this->loadFixturesFromFile('resources/customers.yml'); |
364
|
|
|
|
365
|
|
|
$this->client->request('DELETE', '/api/v1/customers/'.$customers['customer_Oliver']->getId(), [], [], static::$authorizedHeaderWithContentType); |
366
|
|
|
|
367
|
|
|
$response = $this->client->getResponse(); |
368
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
369
|
|
|
|
370
|
|
|
$this->client->request('GET', '/api/v1/customers/'.$customers['customer_Oliver']->getId(), [], [], static::$authorizedHeaderWithAccept); |
371
|
|
|
|
372
|
|
|
$response = $this->client->getResponse(); |
373
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|