Completed
Push — master ( 421643...165cff )
by Paweł
34:09 queued 24:31
created

it_allows_to_update_position_of_product_in_a_taxon()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
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\Taxonomy\Model\TaxonInterface;
16
use Symfony\Component\HttpFoundation\File\UploadedFile;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * @author Anna Walasek <[email protected]>
21
 */
22
final class TaxonApiTest 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_does_not_allow_to_show_taxon_list_when_access_is_denied()
44
    {
45
        $this->loadFixturesFromFile('resources/taxons.yml');
46
        $this->client->request('GET', '/api/v1/taxons/');
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_does_not_allow_to_show_taxon_when_it_does_not_exist()
56
    {
57
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
58
59
        $this->client->request('GET', '/api/v1/taxons/-1', [], [], static::$authorizedHeaderWithAccept);
60
61
        $response = $this->client->getResponse();
62
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function it_allows_indexing_taxons()
69
    {
70
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
71
        $this->loadFixturesFromFile('resources/taxons.yml');
72
73
        $this->client->request('GET', '/api/v1/taxons/', [], [], static::$authorizedHeaderWithAccept);
74
75
        $response = $this->client->getResponse();
76
        $this->assertResponse($response, 'taxon/index_response', Response::HTTP_OK);
77
    }
78
79
    /**
80
     * @test
81
     */
82
    public function it_allows_showing_taxon()
83
    {
84
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
85
        $taxons = $this->loadFixturesFromFile('resources/taxons.yml');
86
        $taxon = $taxons['women'];
87
88
        $this->client->request('GET', $this->getTaxonUrl($taxon), [], [], static::$authorizedHeaderWithAccept);
89
90
        $response = $this->client->getResponse();
91
        $this->assertResponse($response, 'taxon/show_response', Response::HTTP_OK);
92
    }
93
94
    /**
95
     * @test
96
     */
97
    public function it_allows_showing_root_taxon()
98
    {
99
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
100
        $taxons = $this->loadFixturesFromFile('resources/taxons.yml');
101
        $taxon = $taxons['category'];
102
103
        $this->client->request('GET', $this->getTaxonUrl($taxon), [], [], static::$authorizedHeaderWithAccept);
104
105
        $response = $this->client->getResponse();
106
        $this->assertResponse($response, 'taxon/show_root_response', Response::HTTP_OK);
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function it_does_not_allow_to_delete_taxon_if_it_does_not_exist()
113
    {
114
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
115
116
        $this->client->request('DELETE', '/api/v1/taxons/-1', [], [], static::$authorizedHeaderWithAccept);
117
118
        $response = $this->client->getResponse();
119
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
120
    }
121
122
    /**
123
     * @test
124
     */
125
    public function it_allows_deleting_taxon()
126
    {
127
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
128
        $taxons = $this->loadFixturesFromFile('resources/taxons.yml');
129
        $taxon = $taxons['men'];
130
131
        $this->client->request('DELETE', $this->getTaxonUrl($taxon), [], [], static::$authorizedHeaderWithContentType);
132
133
        $response = $this->client->getResponse();
134
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
135
136
        $this->client->request('GET', $this->getTaxonUrl($taxon), [], [], static::$authorizedHeaderWithAccept);
137
138
        $response = $this->client->getResponse();
139
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
140
    }
141
142
    /**
143
     * @test
144
     */
145
    public function it_allows_deleting_root_taxon()
146
    {
147
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
148
        $taxons = $this->loadFixturesFromFile('resources/taxons.yml');
149
        $taxon = $taxons['category'];
150
151
        $this->client->request('DELETE', $this->getTaxonUrl($taxon), [], [], static::$authorizedHeaderWithContentType);
152
153
        $response = $this->client->getResponse();
154
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
155
156
        $this->client->request('GET', $this->getTaxonUrl($taxon), [], [], static::$authorizedHeaderWithAccept);
157
158
        $response = $this->client->getResponse();
159
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
160
    }
161
162
    /**
163
     * @test
164
     */
165
    public function it_allows_creating_root_taxon_with_multiple_translations()
166
    {
167
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
168
        $this->loadFixturesFromFile('resources/locales.yml');
169
170
        $data =
171
<<<EOT
172
        {
173
            "code": "fluffy_pets",
174
            "translations": {
175
                "en_US": {
176
                    "name": "Fluffy Pets",
177
                    "slug": "fluffy-pets"
178
                },
179
                "nl_NL": {
180
                    "name": "Pluizige Huisdieren",
181
                    "slug": "pluizige-huisdieren"
182
                }
183
            }
184
        }
185
EOT;
186
187
        $this->client->request('POST', '/api/v1/taxons/', [], [], static::$authorizedHeaderWithContentType, $data);
188
189
        $response = $this->client->getResponse();
190
        $this->assertResponse($response, 'taxon/create_root_with_multiple_translations_response', Response::HTTP_CREATED);
191
    }
192
193
    /**
194
     * @test
195
     */
196
    public function it_allows_creating_root_taxon()
197
    {
198
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
199
200
        $data =
201
<<<EOT
202
        {
203
            "code": "fluffy_pets"
204
        }
205
EOT;
206
207
        $this->client->request('POST', '/api/v1/taxons/', [], [], static::$authorizedHeaderWithContentType, $data);
208
209
        $response = $this->client->getResponse();
210
        $this->assertResponse($response, 'taxon/create_root_response', Response::HTTP_CREATED);
211
    }
212
213
    /**
214
     * @test
215
     */
216
    public function it_allows_creating_taxon_with_multiple_translations()
217
    {
218
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
219
        $this->loadFixturesFromFile('resources/locales.yml');
220
        $this->loadFixturesFromFile('resources/taxons.yml');
221
222
        $data =
223
<<<EOT
224
        {
225
            "code": "fluffy_pets",
226
            "parent": "category",
227
            "translations": {
228
                "en_US": {
229
                    "name": "Fluffy Pets",
230
                    "slug": "fluffy-pets"
231
                },
232
                "nl_NL": {
233
                    "name": "Pluizige Huisdieren",
234
                    "slug": "pluizige-huisdieren"
235
                }
236
            }
237
        }
238
EOT;
239
240
        $this->client->request('POST', '/api/v1/taxons/', [], [], static::$authorizedHeaderWithContentType, $data);
241
242
        $response = $this->client->getResponse();
243
        $this->assertResponse($response, 'taxon/create_with_multiple_translations_response', Response::HTTP_CREATED);
244
    }
245
246
247
    /**
248
     * @test
249
     */
250
    public function it_allows_creating_taxon_with_parent()
251
    {
252
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
253
        $this->loadFixturesFromFile('resources/taxons.yml');
254
255
        $data =
256
<<<EOT
257
        {
258
            "code": "horror",
259
            "parent": "books"
260
        }
261
EOT;
262
263
        $this->client->request('POST', '/api/v1/taxons/', [], [], static::$authorizedHeaderWithContentType, $data);
264
265
        $response = $this->client->getResponse();
266
        $this->assertResponse($response, 'taxon/create_with_parent_response', Response::HTTP_CREATED);
267
    }
268
269
    /**
270
     * @test
271
     */
272
    public function it_does_not_allow_to_create_taxon_without_required_fields()
273
    {
274
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
275
276
        $this->client->request('POST', '/api/v1/taxons/', [], [], static::$authorizedHeaderWithContentType);
277
278
        $response = $this->client->getResponse();
279
        $this->assertResponse($response, 'taxon/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
280
    }
281
282
    /**
283
     * @test
284
     */
285
    public function it_allows_creating_taxon_with_images()
286
    {
287
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
288
289
        $data =
290
<<<EOT
291
        {
292
            "code": "toys",
293
            "images": [
294
                {
295
                    "type": "ford"                
296
                },
297
                {
298
                    "type": "mugs"
299
                }
300
            ]
301
        }
302
EOT;
303
304
        $this->client->request('POST', '/api/v1/taxons/', [], [
305
            'images' => [
306
                ['file' => new UploadedFile(sprintf('%s/../Resources/fixtures/ford.jpg', __DIR__), "ford")],
307
                ['file' => new UploadedFile(sprintf('%s/../Resources/fixtures/mugs.jpg', __DIR__), "mugs")],
308
            ]
309
        ], static::$authorizedHeaderWithContentType, $data);
310
311
        $response = $this->client->getResponse();
312
        $this->assertResponse($response, 'taxon/create_with_images_response', Response::HTTP_CREATED);
313
    }
314
315
    /**
316
     * @test
317
     */
318
    public function it_allows_updating_taxon_with_parent()
319
    {
320
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
321
        $this->loadFixturesFromFile('resources/locales.yml');
322
        $taxons = $this->loadFixturesFromFile('resources/taxons.yml');
323
        $taxon = $taxons["women"];
324
325
        $data =
326
<<<EOT
327
        {
328
            "translations": {
329
                "en_US": {
330
                  "name": "Women",
331
                  "slug": "books/women"
332
                }
333
            },
334
            "parent": "books"
335
        }
336
EOT;
337
        $this->client->request('PUT', $this->getTaxonUrl($taxon), [], [], static::$authorizedHeaderWithContentType, $data);
338
        $response = $this->client->getResponse();
339
340
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
341
    }
342
343
    /**
344
     * @test
345
     */
346
    public function it_allows_updating_root_taxon()
347
    {
348
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
349
        $this->loadFixturesFromFile('resources/locales.yml');
350
        $taxons = $this->loadFixturesFromFile('resources/taxons.yml');
351
        $taxon = $taxons["category"];
352
353
        $data =
354
<<<EOT
355
        {
356
            "translations": {
357
                "en_US": {
358
                  "name": "Categories",
359
                  "slug": "categories"
360
                }
361
            }
362
        }
363
EOT;
364
        $this->client->request('PUT', $this->getTaxonUrl($taxon), [], [], static::$authorizedHeaderWithContentType, $data);
365
        $response = $this->client->getResponse();
366
367
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
368
    }
369
370
    /**
371
     * @test
372
     */
373
    public function it_allows_updating_partial_information_about_taxon()
374
    {
375
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
376
        $this->loadFixturesFromFile('resources/locales.yml');
377
        $taxons = $this->loadFixturesFromFile('resources/taxons.yml');
378
        $taxon = $taxons["women"];
379
380
        $data =
381
<<<EOT
382
        {
383
            "translations": {
384
                "en_US": {
385
                    "name": "Girl",
386
                    "slug": "girl"
387
                }
388
            }
389
        }
390
EOT;
391
        $this->client->request('PATCH', $this->getTaxonUrl($taxon), [], [], static::$authorizedHeaderWithContentType, $data);
392
        $response = $this->client->getResponse();
393
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
394
    }
395
396
    /**
397
     * @test
398
     */
399
    public function it_allows_updating_partial_information_about_root_taxon()
400
    {
401
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
402
        $this->loadFixturesFromFile('resources/locales.yml');
403
        $taxons = $this->loadFixturesFromFile('resources/taxons.yml');
404
        $taxon = $taxons["category"];
405
406
        $data =
407
<<<EOT
408
        {
409
            "translations": {
410
                "en_US": {
411
                    "name": "Category",
412
                    "slug": "category"
413
                }
414
            }
415
        }
416
EOT;
417
        $this->client->request('PATCH', $this->getTaxonUrl($taxon), [], [], static::$authorizedHeaderWithContentType, $data);
418
        $response = $this->client->getResponse();
419
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
420
    }
421
422
    /**
423
     * @test
424
     */
425
    public function it_allows_paginating_the_index_of_taxons()
426
    {
427
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
428
        $this->loadFixturesFromFile('resources/taxons.yml');
429
        $this->loadFixturesFromFile('resources/many_taxons.yml');
430
431
        $this->client->request('GET', '/api/v1/taxons/', ['page' => 2], [], static::$authorizedHeaderWithAccept);
432
        $response = $this->client->getResponse();
433
        $this->assertResponse($response, 'taxon/paginated_index_response');
434
    }
435
436
    /**
437
     * @test
438
     */
439
    public function it_allows_to_update_position_of_product_in_a_taxon()
440
    {
441
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
442
        $this->loadFixturesFromFile('resources/products.yml');
443
        $productTaxons = $this->loadFixturesFromFile('resources/product_taxons.yml');
444
445
        /** @var TaxonInterface $taxon */
446
        $taxon = $productTaxons['mugs'];
447
448
        $data =
449
<<<EOT
450
        {
451
            "products_positions": [
452
                {
453
                    "product_code": "MUG_SW",
454
                    "position": 2
455
                },
456
                {
457
                    "product_code": "MUG_BB",
458
                    "position": 0
459
                }
460
            ]
461
        }
462
EOT;
463
464
        $this->client->request('PUT', $this->getTaxonProductsPositionsChangeUrl($taxon), [], [], static::$authorizedHeaderWithContentType, $data);
465
466
        $response = $this->client->getResponse();
467
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
468
    }
469
470
    /**
471
     * @test
472
     */
473
    public function it_does_not_allows_to_update_position_of_product_in_a_taxon_with_inccorect_data()
474
    {
475
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
476
        $this->loadFixturesFromFile('resources/products.yml');
477
        $productTaxons = $this->loadFixturesFromFile('resources/product_taxons.yml');
478
479
        /** @var TaxonInterface $taxon */
480
        $taxon = $productTaxons['mugs'];
481
482
        $data =
483
<<<EOT
484
        {
485
            "products_positions": [
486
                {
487
                    "product_code": "MUG_SW",
488
                    "position": "second"
489
                }
490
            ]
491
        }
492
EOT;
493
494
        $this->client->request('PUT', $this->getTaxonProductsPositionsChangeUrl($taxon) , [], [], static::$authorizedHeaderWithContentType, $data);
495
496
        $response = $this->client->getResponse();
497
        $this->assertResponse($response, 'taxon/update_validation_fail_response', Response::HTTP_BAD_REQUEST);
498
    }
499
500
    /**
501
     * @param TaxonInterface $taxon
502
     *
503
     * @return string
504
     */
505
    private function getTaxonUrl(TaxonInterface $taxon)
506
    {
507
        return '/api/v1/taxons/' . $taxon->getId();
508
    }
509
510
    /**
511
     * @param TaxonInterface $taxon
512
     *
513
     * @return string
514
     */
515
    private function getTaxonProductsPositionsChangeUrl(TaxonInterface $taxon)
516
    {
517
        return sprintf('/api/v1/taxons/%s/products', $taxon->getCode());
518
    }
519
}
520