Completed
Push — master ( e3540e...96a5d6 )
by Kamil
25s
created

it_allows_to_create_promotion_with_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 67
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 9.2815
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Promotion\Model\PromotionInterface;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * @author Anna Walasek <[email protected]>
20
 */
21
final class PromotionApiTest 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_promotions_list_when_access_is_denied()
43
    {
44
        $this->loadFixturesFromFile('resources/promotions.yml');
45
46
        $this->client->request('GET', '/api/v1/promotions/');
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_promotions()
56
    {
57
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
58
        $this->loadFixturesFromFile('resources/promotions.yml');
59
60
        $this->client->request('GET', '/api/v1/promotions/', [], [], static::$authorizedHeaderWithAccept);
61
62
        $response = $this->client->getResponse();
63
        $this->assertResponse($response, 'promotion/index_response', Response::HTTP_OK);
64
    }
65
66
    /**
67
     * @test
68
     */
69
    public function it_allows_sorting_the_index_of_promotions_by_code()
70
    {
71
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
72
        $this->loadFixturesFromFile('resources/promotions.yml');
73
        $this->loadFixturesFromFile('resources/many_promotions.yml');
74
75
        $this->client->request('GET', '/api/v1/promotions/', ['sorting' => ['code' => 'asc']], [], static::$authorizedHeaderWithAccept);
76
77
        $response = $this->client->getResponse();
78
        $this->assertResponse($response, 'promotion/sorted_by_code_index_response');
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function it_allows_sorting_the_index_of_promotions_by_name()
85
    {
86
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
87
        $this->loadFixturesFromFile('resources/promotions.yml');
88
        $this->loadFixturesFromFile('resources/many_promotions.yml');
89
90
        $this->client->request('GET', '/api/v1/promotions/', ['sorting' => ['name' => 'desc']], [], static::$authorizedHeaderWithAccept);
91
92
        $response = $this->client->getResponse();
93
        $this->assertResponse($response, 'promotion/sorted_by_name_index_response');
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function it_allows_sorting_the_index_of_promotions_by_priority()
100
    {
101
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
102
        $this->loadFixturesFromFile('resources/promotions.yml');
103
        $this->loadFixturesFromFile('resources/many_promotions.yml');
104
105
        $this->client->request('GET', '/api/v1/promotions/', ['sorting' => ['priority' => 'asc']], [], static::$authorizedHeaderWithAccept);
106
107
        $response = $this->client->getResponse();
108
        $this->assertResponse($response, 'promotion/sorted_by_priority_index_response');
109
    }
110
111
    /**
112
     * @test
113
     */
114
    public function it_allows_to_get_promotions_list_filtered_by_name_and_code()
115
    {
116
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
117
        $this->loadFixturesFromFile('resources/promotions.yml');
118
        $this->loadFixturesFromFile('resources/many_promotions.yml');
119
120
        $this->client->request('GET', '/api/v1/promotions/', ['criteria' => ['search' => 'promo']], [], [
121
            'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
122
        ]);
123
124
        $response = $this->client->getResponse();
125
        $this->assertResponse($response, 'promotion/filtered_by_code_and_name_index_response', Response::HTTP_OK);
126
    }
127
128
    /**
129
     * @test
130
     */
131
    public function it_allows_to_get_list_of_coupon_based_promotions()
132
    {
133
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
134
        $this->loadFixturesFromFile('resources/promotions.yml');
135
        $this->loadFixturesFromFile('resources/many_promotions.yml');
136
137
        $this->client->request('GET', '/api/v1/promotions/', ['criteria' => ['couponBased' => 'true']], [], [
138
            'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
139
        ]);
140
141
        $response = $this->client->getResponse();
142
        $this->assertResponse($response, 'promotion/only_coupon_based_index_response', Response::HTTP_OK);
143
    }
144
145
    /**
146
     * @test
147
     */
148
    public function it_does_not_allow_to_show_promotion_when_it_does_not_exist()
149
    {
150
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
151
152
        $this->client->request('GET', '/api/v1/promotions/-1', [], [], static::$authorizedHeaderWithAccept);
153
154
        $response = $this->client->getResponse();
155
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
156
    }
157
158
    /**
159
     * @test
160
     */
161
    public function it_allows_showing_promotion()
162
    {
163
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
164
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
165
        $promotion = $promotions['promotion2'];
166
167
        $this->client->request('GET', $this->getPromotionUrl($promotion), [], [], static::$authorizedHeaderWithAccept);
168
169
        $response = $this->client->getResponse();
170
        $this->assertResponse($response, 'promotion/show_response', Response::HTTP_OK);
171
    }
172
173
    /**
174
     * @test
175
     */
176
    public function it_does_not_allow_to_delete_promotion_if_it_does_not_exist()
177
    {
178
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
179
180
        $this->client->request('DELETE', '/api/v1/promotions/-1', [], [], static::$authorizedHeaderWithAccept);
181
182
        $response = $this->client->getResponse();
183
184
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
185
    }
186
187
    /**
188
     * @test
189
     */
190
    public function it_allows_to_delete_promotion()
191
    {
192
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
193
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
194
        $promotion = $promotions['promotion1'];
195
196
        $this->client->request('DELETE', $this->getPromotionUrl($promotion), [], [], static::$authorizedHeaderWithContentType, []);
197
198
        $response = $this->client->getResponse();
199
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
200
201
        $this->client->request('GET', $this->getPromotionUrl($promotion), [], [], static::$authorizedHeaderWithAccept);
202
203
        $response = $this->client->getResponse();
204
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
205
    }
206
207
    /**
208
     * @test
209
     */
210
    public function it_does_not_allow_to_create_promotion_without_required_fields()
211
    {
212
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
213
214
        $this->client->request('POST', '/api/v1/promotions/', [], [], static::$authorizedHeaderWithContentType, []);
215
216
        $response = $this->client->getResponse();
217
        $this->assertResponse($response, 'promotion/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
218
    }
219
220
    /**
221
     * @test
222
     */
223
    public function it_allows_to_create_promotion()
224
    {
225
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
226
227
        $data =
228
<<<EOT
229
        {
230
            "code": "christmas-promotion",
231
            "name": "Christmas Promotion"
232
        }
233
EOT;
234
        $this->client->request('POST', '/api/v1/promotions/', [], [], static::$authorizedHeaderWithContentType, $data);
235
236
        $response = $this->client->getResponse();
237
        $this->assertResponse($response, 'promotion/create_response', Response::HTTP_CREATED);
238
    }
239
240
    /**
241
     * @test
242
     */
243
    public function it_allows_to_create_promotion_with_channels()
244
    {
245
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
246
        $this->loadFixturesFromFile('resources/channels.yml');
247
248
        $data =
249
<<<EOT
250
        {
251
            "code": "christmas-promotion",
252
            "name": "Christmas Promotion",
253
            "channels": [
254
                "WEB",
255
                "MOB"
256
            ]
257
        }
258
EOT;
259
        $this->client->request('POST', '/api/v1/promotions/', [], [], static::$authorizedHeaderWithContentType, $data);
260
261
        $response = $this->client->getResponse();
262
        $this->assertResponse($response, 'promotion/create_response_with_channels', Response::HTTP_CREATED);
263
    }
264
265
    /**
266
     * @test
267
     */
268
    public function it_allows_to_create_promotion_with_time_of_duration()
269
    {
270
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
271
272
        $data =
273
<<<EOT
274
        {
275
            "code": "christmas-promotion",
276
            "name": "Christmas Promotion",
277
            "startsAt": {
278
                "date": "2017-12-05",
279
                "time": "11:00"
280
            },
281
            "endsAt": {
282
                "date": "2017-12-31",
283
                "time": "11:00"
284
            }
285
        }
286
EOT;
287
        $this->client->request('POST', '/api/v1/promotions/', [], [], static::$authorizedHeaderWithContentType, $data);
288
289
        $response = $this->client->getResponse();
290
        $this->assertResponse($response, 'promotion/create_response_with_time_of_duration', Response::HTTP_CREATED);
291
    }
292
293
    /**
294
     * @test
295
     */
296
    public function it_allows_to_create_promotion_with_rules()
297
    {
298
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
299
        $this->loadFixturesFromFile('resources/channels.yml');
300
301
        $data =
302
<<<EOT
303
        {
304
            "code": "christmas-promotion",
305
            "name": "Christmas Promotion",
306
            "rules": [
307
                {
308
                    "type": "nth_order",
309
                    "configuration": {
310
                        "nth": 3
311
                    }
312
                },
313
                {
314
                    "type": "item_total",
315
                    "configuration": {
316
                        "WEB": {
317
                            "amount": 12
318
                        },
319
                        "MOB": {
320
                            "amount": 15
321
                        }
322
                    }
323
                }
324
            ]
325
        }
326
EOT;
327
        $this->client->request('POST', '/api/v1/promotions/', [], [], static::$authorizedHeaderWithContentType, $data);
328
329
        $response = $this->client->getResponse();
330
        $this->assertResponse($response, 'promotion/create_response_with_rules', Response::HTTP_CREATED);
331
    }
332
333
    /**
334
     * @test
335
     */
336
    public function it_allows_to_create_promotion_with_actions()
337
    {
338
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
339
        $this->loadFixturesFromFile('resources/channels.yml');
340
        $this->loadFixturesFromFile('resources/products.yml');
341
        $this->loadFixturesFromFile('resources/product_taxons.yml');
342
343
        $data =
344
<<<EOT
345
        {
346
            "code": "christmas-promotion",
347
            "name": "Christmas Promotion",
348
            "actions": [
349
                {
350
                    "type": "unit_percentage_discount",
351
                    "configuration": {
352
                        "WEB": {
353
                            "percentage": 15,
354
                            "filters": {
355
                                "price_range_filter": {
356
                                    "min": 1,
357
                                    "max": 12000
358
                                },
359
                                "taxons_filter": {
360
                                    "taxons": [
361
                                        "mugs"
362
                                    ]
363
                                },
364
                                "products_filter": {
365
                                    "products": [
366
                                        "MUG_SW",
367
                                        "MUG_LOTR"
368
                                    ]
369
                                }
370
                            }
371
                        },
372
                        "MOB": {
373
                            "percentage": 20,
374
                            "filters": {
375
                                "products_filter": {
376
                                    "products": [
377
                                        "MUG_SW"
378
                                    ]
379
                                }
380
                            }
381
                        }
382
                    }
383
                },
384
                {
385
                    "type": "order_fixed_discount",
386
                    "configuration": {
387
                        "WEB": {
388
                            "amount": 12
389
                        },
390
                        "MOB": {
391
                            "amount": 15
392
                        }
393
                    }
394
                }
395
            ]
396
        }
397
EOT;
398
        $this->client->request('POST', '/api/v1/promotions/', [], [], static::$authorizedHeaderWithContentType, $data);
399
400
        $response = $this->client->getResponse();
401
        $this->assertResponse($response, 'promotion/create_response_with_actions', Response::HTTP_CREATED);
402
    }
403
404
405
    /**
406
     * @test
407
     */
408
    public function it_allows_to_create_coupon_based_promotion()
409
    {
410
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
411
412
        $data =
413
<<<EOT
414
        {
415
            "code": "christmas-promotion",
416
            "name": "Christmas Promotion",
417
            "couponBased": true
418
        }
419
EOT;
420
        $this->client->request('POST', '/api/v1/promotions/', [], [], static::$authorizedHeaderWithContentType, $data);
421
422
        $response = $this->client->getResponse();
423
        $this->assertResponse($response, 'promotion/create_coupon_based_response', Response::HTTP_CREATED);
424
    }
425
426
    /**
427
     * @test
428
     */
429
    public function it_allows_to_create_exclusive_promotion()
430
    {
431
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
432
433
        $data =
434
<<<EOT
435
        {
436
            "code": "christmas-promotion",
437
            "name": "Christmas Promotion",
438
            "exclusive": true,
439
            "priority": 0
440
        }
441
EOT;
442
        $this->client->request('POST', '/api/v1/promotions/', [], [], static::$authorizedHeaderWithContentType, $data);
443
444
        $response = $this->client->getResponse();
445
        $this->assertResponse($response, 'promotion/create_exclusive_response', Response::HTTP_CREATED);
446
    }
447
448
    /**
449
     * @test
450
     */
451
    public function it_allows_updating_promotion()
452
    {
453
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
454
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
455
        $promotion = $promotions["promotion1"];
456
457
        $data =
458
<<<EOT
459
        {
460
            "name": "Monday promotion"
461
        }
462
EOT;
463
        $this->client->request('PUT', $this->getPromotionUrl($promotion), [], [], static::$authorizedHeaderWithContentType, $data);
464
        $response = $this->client->getResponse();
465
466
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
467
468
        $this->client->request('GET', $this->getPromotionUrl($promotion), [], [], static::$authorizedHeaderWithAccept);
469
470
        $response = $this->client->getResponse();
471
        $this->assertResponse($response, 'promotion/show_response_after_update', Response::HTTP_OK);
472
    }
473
474
    /**
475
     * @test
476
     */
477
    public function it_allows_updating_partial_information_about_promotion()
478
    {
479
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
480
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
481
        $promotion = $promotions["promotion1"];
482
483
        $data =
484
<<<EOT
485
        {
486
            "exclusive": true,
487
            "priority": 0
488
        }
489
EOT;
490
        $this->client->request('PATCH', $this->getPromotionUrl($promotion), [], [], static::$authorizedHeaderWithContentType, $data);
491
        $response = $this->client->getResponse();
492
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
493
494
        $this->client->request('GET', $this->getPromotionUrl($promotion), [], [], static::$authorizedHeaderWithAccept);
495
496
        $response = $this->client->getResponse();
497
        $this->assertResponse($response, 'promotion/show_response_after_partial_update', Response::HTTP_OK);
498
    }
499
500
    /**
501
     * @param PromotionInterface $promotion
502
     *
503
     * @return string
504
     */
505
    private function getPromotionUrl(PromotionInterface $promotion)
506
    {
507
        return '/api/v1/promotions/' . $promotion->getCode();
508
    }
509
}
510