|
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\Tests\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Lakion\ApiTestCase\JsonApiTestCase; |
|
15
|
|
|
use Sylius\Component\Shipping\Model\ShippingCategoryInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Łukasz Chruściel <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class ShippingCategoryApiTest extends JsonApiTestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private static $authorizedHeaderWithContentType = [ |
|
27
|
|
|
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', |
|
28
|
|
|
'CONTENT_TYPE' => 'application/json', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private static $authorizedHeaderWithAccept = [ |
|
35
|
|
|
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', |
|
36
|
|
|
'Accept' => 'application/json', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @test |
|
41
|
|
|
*/ |
|
42
|
|
|
public function it_denies_creating_shipping_category_for_non_authenticated_user() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->client->request('POST', '/api/v1/shipping-categories/'); |
|
45
|
|
|
|
|
46
|
|
|
$response = $this->client->getResponse(); |
|
47
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @test |
|
52
|
|
|
*/ |
|
53
|
|
|
public function it_does_not_allow_to_create_shipping_category_without_specifying_required_data() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
56
|
|
|
|
|
57
|
|
|
$this->client->request('POST', '/api/v1/shipping-categories/', [], [], static::$authorizedHeaderWithContentType); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$response = $this->client->getResponse(); |
|
60
|
|
|
$this->assertResponse($response, 'shipping_category/create_validation_fail_response', Response::HTTP_BAD_REQUEST); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @test |
|
65
|
|
|
*/ |
|
66
|
|
|
public function it_allows_to_create_shipping_category() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
69
|
|
|
|
|
70
|
|
|
$data = |
|
71
|
|
|
<<<EOT |
|
72
|
|
|
{ |
|
73
|
|
|
"code": "reg", |
|
74
|
|
|
"name": "Regular", |
|
75
|
|
|
"description": "Regular weight items" |
|
76
|
|
|
} |
|
77
|
|
|
EOT; |
|
78
|
|
|
|
|
79
|
|
|
$this->client->request('POST', '/api/v1/shipping-categories/', [], [], static::$authorizedHeaderWithContentType, $data); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$response = $this->client->getResponse(); |
|
82
|
|
|
$this->assertResponse($response, 'shipping_category/create_response', Response::HTTP_CREATED); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @test |
|
87
|
|
|
*/ |
|
88
|
|
|
public function it_denies_getting_shipping_categories_for_non_authenticated_user() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->client->request('GET', '/api/v1/shipping-categories/'); |
|
91
|
|
|
|
|
92
|
|
|
$response = $this->client->getResponse(); |
|
93
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @test |
|
98
|
|
|
*/ |
|
99
|
|
|
public function it_allows_to_get_shipping_categories_list() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
102
|
|
|
$this->loadFixturesFromFile('resources/shipping_categories.yml'); |
|
103
|
|
|
|
|
104
|
|
|
$this->client->request('GET', '/api/v1/shipping-categories/', [], [], static::$authorizedHeaderWithContentType); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$response = $this->client->getResponse(); |
|
107
|
|
|
$this->assertResponse($response, 'shipping_category/index_response', Response::HTTP_OK); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @test |
|
112
|
|
|
*/ |
|
113
|
|
|
public function it_denies_getting_shipping_category_for_non_authenticated_user() |
|
114
|
|
|
{ |
|
115
|
|
|
$this->client->request('GET', '/api/v1/shipping-categories/1'); |
|
116
|
|
|
|
|
117
|
|
|
$response = $this->client->getResponse(); |
|
118
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @test |
|
123
|
|
|
*/ |
|
124
|
|
|
public function it_returns_not_found_response_when_requesting_details_of_a_shipping_category_which_does_not_exist() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
127
|
|
|
|
|
128
|
|
|
$this->client->request('GET', '/api/v1/shipping-categories/-1', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
$response = $this->client->getResponse(); |
|
131
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @test |
|
136
|
|
|
*/ |
|
137
|
|
|
public function it_allows_to_get_shipping_category() |
|
138
|
|
|
{ |
|
139
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
140
|
|
|
$shippingCategories = $this->loadFixturesFromFile('resources/shipping_categories.yml'); |
|
141
|
|
|
|
|
142
|
|
|
/** @var ShippingCategoryInterface $shippingCategory */ |
|
143
|
|
|
$shippingCategory = $shippingCategories['shipping_category_2']; |
|
144
|
|
|
|
|
145
|
|
|
$this->client->request('GET', $this->getShippingCategoryUrl($shippingCategory), [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
$response = $this->client->getResponse(); |
|
148
|
|
|
$this->assertResponse($response, 'shipping_category/show_response', Response::HTTP_OK); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @test |
|
153
|
|
|
*/ |
|
154
|
|
|
public function it_denies_updating_shipping_category_for_non_authenticated_user() |
|
155
|
|
|
{ |
|
156
|
|
|
$this->client->request('PUT', '/api/v1/shipping-categories/1'); |
|
157
|
|
|
|
|
158
|
|
|
$response = $this->client->getResponse(); |
|
159
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @test |
|
164
|
|
|
*/ |
|
165
|
|
|
public function it_returns_not_found_response_when_updating_shipping_category_which_does_not_exist() |
|
166
|
|
|
{ |
|
167
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
168
|
|
|
|
|
169
|
|
|
$this->client->request('PUT', '/api/v1/shipping-categories/-1', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
$response = $this->client->getResponse(); |
|
172
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @test |
|
177
|
|
|
*/ |
|
178
|
|
|
public function it_does_not_allow_to_update_shipping_category_without_specifying_required_data() |
|
179
|
|
|
{ |
|
180
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
181
|
|
|
$shippingCategories = $this->loadFixturesFromFile('resources/shipping_categories.yml'); |
|
182
|
|
|
|
|
183
|
|
|
/** @var ShippingCategoryInterface $shippingCategory */ |
|
184
|
|
|
$shippingCategory = $shippingCategories['shipping_category_1']; |
|
185
|
|
|
|
|
186
|
|
|
$this->client->request('PUT', $this->getShippingCategoryUrl($shippingCategory), [], [], static::$authorizedHeaderWithContentType); |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
$response = $this->client->getResponse(); |
|
189
|
|
|
$this->assertResponse($response, 'shipping_category/update_validation_fail_response', Response::HTTP_BAD_REQUEST); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @test |
|
194
|
|
|
*/ |
|
195
|
|
|
public function it_allows_to_update_shipping_category() |
|
196
|
|
|
{ |
|
197
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
198
|
|
|
$shippingCategories = $this->loadFixturesFromFile('resources/shipping_categories.yml'); |
|
199
|
|
|
|
|
200
|
|
|
/** @var ShippingCategoryInterface $shippingCategory */ |
|
201
|
|
|
$shippingCategory = $shippingCategories['shipping_category_1']; |
|
202
|
|
|
|
|
203
|
|
|
$data = |
|
204
|
|
|
<<<EOT |
|
205
|
|
|
{ |
|
206
|
|
|
"name": "Light", |
|
207
|
|
|
"description": "Light weight items" |
|
208
|
|
|
} |
|
209
|
|
|
EOT; |
|
210
|
|
|
|
|
211
|
|
|
$this->client->request('PUT', $this->getShippingCategoryUrl($shippingCategory), [], [], static::$authorizedHeaderWithContentType, $data); |
|
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
$response = $this->client->getResponse(); |
|
214
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
|
215
|
|
|
|
|
216
|
|
|
$this->client->request('GET', $this->getShippingCategoryUrl($shippingCategory), [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
$response = $this->client->getResponse(); |
|
219
|
|
|
$this->assertResponse($response, 'shipping_category/update_response', Response::HTTP_OK); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @test |
|
224
|
|
|
*/ |
|
225
|
|
|
public function it_returns_not_found_response_when_partially_updating_shipping_category_which_does_not_exist() |
|
226
|
|
|
{ |
|
227
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
228
|
|
|
|
|
229
|
|
|
$this->client->request('PATCH', '/api/v1/shipping-categories/-1', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
$response = $this->client->getResponse(); |
|
232
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @test |
|
237
|
|
|
*/ |
|
238
|
|
|
public function it_allows_to_partially_update_shipping_category() |
|
239
|
|
|
{ |
|
240
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
241
|
|
|
$shippingCategories = $this->loadFixturesFromFile('resources/shipping_categories.yml'); |
|
242
|
|
|
|
|
243
|
|
|
/** @var ShippingCategoryInterface $shippingCategory */ |
|
244
|
|
|
$shippingCategory = $shippingCategories['shipping_category_1']; |
|
245
|
|
|
|
|
246
|
|
|
$data = |
|
247
|
|
|
<<<EOT |
|
248
|
|
|
{ |
|
249
|
|
|
"name": "Light", |
|
250
|
|
|
"description": "Light weight items" |
|
251
|
|
|
} |
|
252
|
|
|
EOT; |
|
253
|
|
|
|
|
254
|
|
|
$this->client->request('PATCH', $this->getShippingCategoryUrl($shippingCategory), [], [], static::$authorizedHeaderWithContentType, $data); |
|
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
$response = $this->client->getResponse(); |
|
257
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
|
258
|
|
|
|
|
259
|
|
|
$this->client->request('GET', $this->getShippingCategoryUrl($shippingCategory), [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
$response = $this->client->getResponse(); |
|
262
|
|
|
$this->assertResponse($response, 'shipping_category/update_response', Response::HTTP_OK); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @test |
|
267
|
|
|
*/ |
|
268
|
|
|
public function it_returns_not_found_response_when_trying_to_delete_shipping_category_which_does_not_exist() |
|
269
|
|
|
{ |
|
270
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
271
|
|
|
|
|
272
|
|
|
$this->client->request('DELETE', '/api/v1/shipping-categories/-1', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
$response = $this->client->getResponse(); |
|
275
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @test |
|
280
|
|
|
*/ |
|
281
|
|
|
public function it_allows_to_delete_shipping_category() |
|
282
|
|
|
{ |
|
283
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
284
|
|
|
$shippingCategories = $this->loadFixturesFromFile('resources/shipping_categories.yml'); |
|
285
|
|
|
|
|
286
|
|
|
/** @var ShippingCategoryInterface $shippingCategory */ |
|
287
|
|
|
$shippingCategory = $shippingCategories['shipping_category_2']; |
|
288
|
|
|
|
|
289
|
|
|
$this->client->request('DELETE', $this->getShippingCategoryUrl($shippingCategory), [], [], static::$authorizedHeaderWithContentType, []); |
|
|
|
|
|
|
290
|
|
|
|
|
291
|
|
|
$response = $this->client->getResponse(); |
|
292
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
|
293
|
|
|
|
|
294
|
|
|
$this->client->request('GET', $this->getShippingCategoryUrl($shippingCategory), [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
$response = $this->client->getResponse(); |
|
297
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @param ShippingCategoryInterface $shippingCategory |
|
302
|
|
|
* |
|
303
|
|
|
* @return string |
|
304
|
|
|
*/ |
|
305
|
|
|
private function getShippingCategoryUrl(ShippingCategoryInterface $shippingCategory) |
|
306
|
|
|
{ |
|
307
|
|
|
return '/api/v1/shipping-categories/' . $shippingCategory->getCode(); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|
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
SomeClassto useselfinstead: