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 PHPUnit\Framework\Assert; |
18
|
|
|
use Sylius\Component\Product\Model\ProductAttributeInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
|
21
|
|
|
final class ProductAttributeApiTest 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_does_not_allow_to_show_product_attributes_list_when_access_is_denied() |
43
|
|
|
{ |
44
|
|
|
$this->loadFixturesFromFile('resources/product_attributes.yml'); |
45
|
|
|
|
46
|
|
|
$this->client->request('GET', '/api/v1/product-attributes/'); |
47
|
|
|
|
48
|
|
|
$response = $this->client->getResponse(); |
49
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
*/ |
55
|
|
|
public function it_allows_indexing_product_attributes() |
56
|
|
|
{ |
57
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
58
|
|
|
$this->loadFixturesFromFile('resources/product_attributes.yml'); |
59
|
|
|
|
60
|
|
|
$this->client->request('GET', '/api/v1/product-attributes/', [], [], static::$authorizedHeaderWithAccept); |
61
|
|
|
|
62
|
|
|
$response = $this->client->getResponse(); |
63
|
|
|
$this->assertResponse($response, 'product_attribute/index_response', Response::HTTP_OK); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
*/ |
69
|
|
|
public function it_does_not_allow_to_show_product_attribute_when_it_does_not_exist() |
70
|
|
|
{ |
71
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
72
|
|
|
|
73
|
|
|
$this->client->request('GET', '/api/v1/product-attributes/-1', [], [], static::$authorizedHeaderWithAccept); |
74
|
|
|
|
75
|
|
|
$response = $this->client->getResponse(); |
76
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @test |
81
|
|
|
*/ |
82
|
|
|
public function it_allows_showing_product_attribute() |
83
|
|
|
{ |
84
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
85
|
|
|
$productAttributes = $this->loadFixturesFromFile('resources/product_attributes.yml'); |
86
|
|
|
$productAttribute = $productAttributes['productAttribute1']; |
87
|
|
|
|
88
|
|
|
$this->client->request('GET', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithAccept); |
89
|
|
|
|
90
|
|
|
$response = $this->client->getResponse(); |
91
|
|
|
$this->assertResponse($response, 'product_attribute/show_response', Response::HTTP_OK); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @test |
96
|
|
|
*/ |
97
|
|
|
public function it_does_not_allow_to_delete_product_attribute_if_it_does_not_exist() |
98
|
|
|
{ |
99
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
100
|
|
|
|
101
|
|
|
$this->client->request('DELETE', '/api/v1/product-attributes/-1', [], [], static::$authorizedHeaderWithContentType); |
102
|
|
|
|
103
|
|
|
$response = $this->client->getResponse(); |
104
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
*/ |
110
|
|
|
public function it_allows_to_delete_product_attribute() |
111
|
|
|
{ |
112
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
113
|
|
|
$productAttributes = $this->loadFixturesFromFile('resources/product_attributes.yml'); |
114
|
|
|
$productAttribute = $productAttributes['productAttribute1']; |
115
|
|
|
|
116
|
|
|
$this->client->request('DELETE', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithContentType); |
117
|
|
|
|
118
|
|
|
$response = $this->client->getResponse(); |
119
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
120
|
|
|
|
121
|
|
|
$this->client->request('GET', '/api/v1/product-attributes/', [], [], static::$authorizedHeaderWithAccept); |
122
|
|
|
|
123
|
|
|
$response = $this->client->getResponse(); |
124
|
|
|
$this->assertResponse($response, 'product_attribute/index_response_after_delete', Response::HTTP_OK); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @test |
129
|
|
|
*/ |
130
|
|
|
public function it_allows_to_create_product_attribute() |
131
|
|
|
{ |
132
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
133
|
|
|
$this->loadFixturesFromFile('resources/locales.yml'); |
134
|
|
|
|
135
|
|
|
$data = |
136
|
|
|
<<<EOT |
137
|
|
|
{ |
138
|
|
|
"code": "mug_material", |
139
|
|
|
"translations": { |
140
|
|
|
"de_CH": { |
141
|
|
|
"name": "Becher Material" |
142
|
|
|
}, |
143
|
|
|
"en_US": { |
144
|
|
|
"name": "Mug material" |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
EOT; |
149
|
|
|
|
150
|
|
|
$this->client->request('POST', '/api/v1/product-attributes/text', [], [], static::$authorizedHeaderWithContentType, $data); |
151
|
|
|
|
152
|
|
|
$response = $this->client->getResponse(); |
153
|
|
|
$this->assertResponse($response, 'product_attribute/create_response', Response::HTTP_CREATED); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @test |
158
|
|
|
*/ |
159
|
|
|
public function it_does_not_allow_to_create_product_attribute_without_required_fields() |
160
|
|
|
{ |
161
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
162
|
|
|
|
163
|
|
|
$this->client->request('POST', '/api/v1/product-attributes/text', [], [], static::$authorizedHeaderWithContentType, []); |
164
|
|
|
|
165
|
|
|
$response = $this->client->getResponse(); |
166
|
|
|
$this->assertResponse($response, 'product_attribute/create_validation_fail_response', Response::HTTP_BAD_REQUEST); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @test |
171
|
|
|
*/ |
172
|
|
|
public function it_does_not_allow_to_create_product_attribute_without_type() |
173
|
|
|
{ |
174
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
175
|
|
|
|
176
|
|
|
$this->client->request('POST', '/api/v1/product-attributes', [], [], static::$authorizedHeaderWithContentType, []); |
177
|
|
|
|
178
|
|
|
$response = $this->client->getResponse(); |
179
|
|
|
$this->assertResponseCodeOneOf($response, [Response::HTTP_NOT_FOUND, Response::HTTP_METHOD_NOT_ALLOWED]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @test |
184
|
|
|
*/ |
185
|
|
|
public function it_allows_to_update_product_attribute() |
186
|
|
|
{ |
187
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
188
|
|
|
$this->loadFixturesFromFile('resources/locales.yml'); |
189
|
|
|
$productAttributes = $this->loadFixturesFromFile('resources/product_attributes.yml'); |
190
|
|
|
$productAttribute = $productAttributes['productAttribute1']; |
191
|
|
|
|
192
|
|
|
$data = |
193
|
|
|
<<<EOT |
194
|
|
|
{ |
195
|
|
|
"position": 2, |
196
|
|
|
"translations": { |
197
|
|
|
"de_CH": { |
198
|
|
|
"name": "Becher Material" |
199
|
|
|
}, |
200
|
|
|
"en_US": { |
201
|
|
|
"name": "Mug material" |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
EOT; |
206
|
|
|
|
207
|
|
|
$this->client->request('PUT', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithContentType, $data); |
208
|
|
|
|
209
|
|
|
$response = $this->client->getResponse(); |
210
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
211
|
|
|
|
212
|
|
|
$this->client->request('GET', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithAccept); |
213
|
|
|
|
214
|
|
|
$response = $this->client->getResponse(); |
215
|
|
|
$this->assertResponse($response, 'product_attribute/show_response_after_update', Response::HTTP_OK); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @test |
220
|
|
|
*/ |
221
|
|
|
public function it_allows_to_partially_update_product_attribute() |
222
|
|
|
{ |
223
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
224
|
|
|
$this->loadFixturesFromFile('resources/locales.yml'); |
225
|
|
|
$productAttributes = $this->loadFixturesFromFile('resources/product_attributes.yml'); |
226
|
|
|
$productAttribute = $productAttributes['productAttribute1']; |
227
|
|
|
|
228
|
|
|
$data = |
229
|
|
|
<<<EOT |
230
|
|
|
{ |
231
|
|
|
"translations": { |
232
|
|
|
"de_CH": { |
233
|
|
|
"name": "Becher Material" |
234
|
|
|
}, |
235
|
|
|
"en_US": { |
236
|
|
|
"name": "Mug material" |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
EOT; |
241
|
|
|
|
242
|
|
|
$this->client->request('PATCH', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithContentType, $data); |
243
|
|
|
|
244
|
|
|
$response = $this->client->getResponse(); |
245
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
246
|
|
|
|
247
|
|
|
$this->client->request('GET', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithAccept); |
248
|
|
|
|
249
|
|
|
$response = $this->client->getResponse(); |
250
|
|
|
$this->assertResponse($response, 'product_attribute/show_response_after_partial_update', Response::HTTP_OK); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @test |
255
|
|
|
*/ |
256
|
|
|
public function it_does_not_allow_to_change_product_attribute_type() |
257
|
|
|
{ |
258
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
259
|
|
|
$this->loadFixturesFromFile('resources/locales.yml'); |
260
|
|
|
$productAttributes = $this->loadFixturesFromFile('resources/product_attributes.yml'); |
261
|
|
|
$productAttribute = $productAttributes['productAttribute1']; |
262
|
|
|
|
263
|
|
|
$data = |
264
|
|
|
<<<EOT |
265
|
|
|
{ |
266
|
|
|
"type": "integer" |
267
|
|
|
} |
268
|
|
|
EOT; |
269
|
|
|
|
270
|
|
|
$this->client->request('PATCH', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithContentType, $data); |
271
|
|
|
|
272
|
|
|
$response = $this->client->getResponse(); |
273
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
274
|
|
|
|
275
|
|
|
$this->client->request('GET', $this->getProductAttributeUrl($productAttribute), [], [], static::$authorizedHeaderWithAccept); |
276
|
|
|
|
277
|
|
|
$response = $this->client->getResponse(); |
278
|
|
|
$this->assertResponse($response, 'product_attribute/show_response', Response::HTTP_OK); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @test |
283
|
|
|
*/ |
284
|
|
|
public function it_allows_to_create_select_product_attribute() |
285
|
|
|
{ |
286
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
287
|
|
|
$this->loadFixturesFromFile('resources/locales.yml'); |
288
|
|
|
|
289
|
|
|
$data = |
290
|
|
|
<<<EOT |
291
|
|
|
{ |
292
|
|
|
"code": "mug_color", |
293
|
|
|
"configuration": { |
294
|
|
|
"choices": [ |
295
|
|
|
{"en_US": "yellow", "fr_FR": "jaune"}, |
296
|
|
|
{"en_US": "green"}, |
297
|
|
|
{"en_US": "black"} |
298
|
|
|
], |
299
|
|
|
"multiple": true, |
300
|
|
|
"min": 1, |
301
|
|
|
"max": 2 |
302
|
|
|
}, |
303
|
|
|
"translations": { |
304
|
|
|
"de_CH": { |
305
|
|
|
"name": "Becher Farbe" |
306
|
|
|
}, |
307
|
|
|
"en_US": { |
308
|
|
|
"name": "Mug color" |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
EOT; |
313
|
|
|
|
314
|
|
|
$this->client->request('POST', '/api/v1/product-attributes/select', [], [], static::$authorizedHeaderWithContentType, $data); |
315
|
|
|
|
316
|
|
|
$response = $this->client->getResponse(); |
317
|
|
|
$this->assertResponse($response, 'product_attribute/create_select_response', Response::HTTP_CREATED); |
318
|
|
|
|
319
|
|
|
$expectedChoiceValues = [ |
320
|
|
|
['en_US' => 'yellow', 'fr_FR' => 'jaune'], |
321
|
|
|
['en_US' => 'green'], |
322
|
|
|
['en_US' => 'black'], |
323
|
|
|
]; |
324
|
|
|
$this->assertSelectChoicesInResponse($response, $expectedChoiceValues); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @param ProductAttributeInterface $productAttribute |
329
|
|
|
* |
330
|
|
|
* @return string |
331
|
|
|
*/ |
332
|
|
|
private function getProductAttributeUrl(ProductAttributeInterface $productAttribute) |
333
|
|
|
{ |
334
|
|
|
return '/api/v1/product-attributes/' . $productAttribute->getCode(); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @param Response $response |
339
|
|
|
* @param array|string[] $expectedChoiceValues |
340
|
|
|
*/ |
341
|
|
|
private function assertSelectChoicesInResponse(Response $response, array $expectedChoiceValues): void |
342
|
|
|
{ |
343
|
|
|
$responseContent = json_decode($response->getContent(), true); |
344
|
|
|
Assert::assertArrayHasKey('configuration', $responseContent); |
345
|
|
|
|
346
|
|
|
$configuration = $responseContent['configuration']; |
347
|
|
|
Assert::assertArrayHasKey('choices', $configuration); |
348
|
|
|
|
349
|
|
|
$choices = $configuration['choices']; |
350
|
|
|
Assert::assertCount(count($expectedChoiceValues), $choices); |
351
|
|
|
|
352
|
|
|
foreach ($expectedChoiceValues as $expectedChoiceValue) { |
353
|
|
|
Assert::assertContains($expectedChoiceValue, $choices); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
foreach ($choices as $choiceKey => $choiceValue) { |
357
|
|
|
Assert::assertRegExp('/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i', $choiceKey); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
private function assertResponseCodeOneOf(Response $response, array $statusCodes): void |
362
|
|
|
{ |
363
|
|
|
self::assertContains($response->getStatusCode(), $statusCodes, $response->getContent()); |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|