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