Completed
Push — pull-request/7087 ( f84acd )
by Kamil
24:59
created

it_allows_to_fully_update_coupon()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
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\Core\Model\PromotionCouponInterface;
16
use Sylius\Component\Promotion\Model\PromotionInterface;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * @author Anna Walasek <[email protected]>
21
 */
22
final class PromotionCouponApiTest 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_promotion_coupons_list_when_access_is_denied()
44
    {
45
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
46
        $promotion = $promotions['promotion2'];
47
48
        $this->loadFixturesFromFile('resources/promotion_coupons.yml');
49
50
        $this->client->request('GET', $this->getPromotionCouponsUrl($promotion));
51
52
        $response = $this->client->getResponse();
53
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
54
    }
55
56
    /**
57
     * @test
58
     */
59
    public function it_allows_indexing_promotion_coupons()
60
    {
61
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
62
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
63
        $promotion = $promotions['promotion2'];
64
        $this->loadFixturesFromFile('resources/promotion_coupons.yml');
65
66
        $this->client->request('GET', $this->getPromotionCouponsUrl($promotion), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
67
68
        $response = $this->client->getResponse();
69
        $this->assertResponse($response, 'promotion_coupon/index_response', Response::HTTP_OK);
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function it_does_not_allow_to_create_coupon_for_not_authenticated_users()
76
    {
77
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
78
        $promotion = $promotions['promotion2'];
79
80
        $this->client->request('POST', $this->getPromotionCouponsUrl($promotion));
81
82
        $response = $this->client->getResponse();
83
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function it_does_not_allow_to_create_coupon_without_specifying_required_data()
90
    {
91
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
92
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
93
        $promotion = $promotions['promotion2'];
94
95
        $this->client->request('POST', $this->getPromotionCouponsUrl($promotion), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
96
97
        $response = $this->client->getResponse();
98
        $this->assertResponse($response, 'promotion_coupon/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function it_allows_to_create_coupon_with_given_code()
105
    {
106
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
107
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
108
        $promotion = $promotions['promotion2'];
109
110
        $data =
111
<<<EOT
112
        {
113
            "code": "1234"
114
        }
115
EOT;
116
117
        $this->client->request('POST', $this->getPromotionCouponsUrl($promotion), [], [], static::$authorizedHeaderWithContentType, $data);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
118
119
        $response = $this->client->getResponse();
120
        $this->assertResponse($response, 'promotion_coupon/create_response', Response::HTTP_CREATED);
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function it_allows_to_create_coupon_with_given_code_expiration_date_and_usage_limits()
127
    {
128
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
129
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
130
        $promotion = $promotions['promotion2'];
131
132
        $data =
133
<<<EOT
134
        {
135
            "code": "1234",
136
            "expiresAt": "2020-01-01",
137
            "usageLimit": 10,
138
            "perCustomerUsageLimit": 1
139
        }
140
EOT;
141
142
        $this->client->request('POST', $this->getPromotionCouponsUrl($promotion), [], [], static::$authorizedHeaderWithContentType, $data);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
143
144
        $response = $this->client->getResponse();
145
        $this->assertResponse($response, 'promotion_coupon/create_advanced_response', Response::HTTP_CREATED);
146
    }
147
148
    /**
149
     * @test
150
     */
151
    public function it_denies_getting_coupon_for_non_authenticated_user()
152
    {
153
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
154
        $promotion = $promotions['promotion2'];
155
156
        $this->client->request('GET', sprintf('/api/v1/promotions/%s/coupons/-1', $promotion->getCode()));
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_requesting_details_of_a_coupon_which_does_not_exist()
166
    {
167
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
168
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
169
        $promotion = $promotions['promotion2'];
170
171
        $this->client->request('GET', sprintf('/api/v1/promotions/%s/coupons/1', $promotion->getCode()), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
172
173
        $response = $this->client->getResponse();
174
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
175
    }
176
177
    /**
178
     * @test
179
     */
180
    public function it_allows_to_get_a_coupon()
181
    {
182
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
183
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
184
        $promotion = $promotions['promotion2'];
185
186
        $promotion_coupons = $this->loadFixturesFromFile('resources/promotion_coupons.yml');
0 ignored issues
show
Coding Style introduced by
$promotion_coupons does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
187
        $coupon = $promotion_coupons['promotionCoupon1'];
0 ignored issues
show
Coding Style introduced by
$promotion_coupons does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
188
189
        $this->client->request('GET', $this->getPromotionCouponUrl($promotion, $coupon), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
190
191
        $response = $this->client->getResponse();
192
        $this->assertResponse($response, 'promotion_coupon/show_response', Response::HTTP_OK);
193
    }
194
195
    /**
196
     * @test
197
     */
198
    public function it_denies_coupon_full_update_for_not_authenticated_users()
199
    {
200
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
201
        $promotion = $promotions['promotion2'];
202
203
        $this->client->request('PUT', sprintf('/api/v1/promotions/%s/coupons/1', $promotion->getCode()));
204
205
        $response = $this->client->getResponse();
206
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
207
    }
208
209
    /**
210
     * @test
211
     */
212
    public function it_returns_not_found_response_when_requesting_full_update_of_a_coupon_which_does_not_exist()
213
    {
214
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
215
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
216
        $promotion = $promotions['promotion2'];
217
218
        $this->client->request('PUT', sprintf('/api/v1/promotions/%s/coupons/1', $promotion->getCode()), [], [], static::$authorizedHeaderWithContentType);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
219
220
        $response = $this->client->getResponse();
221
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
222
    }
223
224
    /**
225
     * @test
226
     */
227
    public function it_allows_to_fully_update_coupon()
228
    {
229
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
230
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
231
        $promotionCoupons = $this->loadFixturesFromFile('resources/promotion_coupons.yml');
232
        $promotion = $promotions['promotion2'];
233
        $promotionCoupon = $promotionCoupons['promotionCoupon2'];
234
235
        $data =
236
<<<EOT
237
        {
238
            "usageLimit": 30,
239
            "perCustomerUsageLimit": 2
240
        }
241
EOT;
242
243
        $this->client->request('PUT', $this->getPromotionCouponUrl($promotion, $promotionCoupon), [], [], static::$authorizedHeaderWithContentType, $data);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
244
245
        $response = $this->client->getResponse();
246
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
247
248
        $this->client->request('GET', $this->getPromotionCouponUrl($promotion, $promotionCoupon), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
249
        $response = $this->client->getResponse();
250
251
        $this->assertResponse($response, 'promotion_coupon/update_response', Response::HTTP_OK);
252
    }
253
254
    /**
255
     * @test
256
     */
257
    public function it_denies_coupon_partial_update_for_not_authenticated_users()
258
    {
259
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
260
        $promotion = $promotions['promotion2'];
261
262
        $this->client->request('PATCH', sprintf('/api/v1/promotions/%s/coupons/1', $promotion->getCode()));
263
264
        $response = $this->client->getResponse();
265
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
266
    }
267
268
    /**
269
     * @test
270
     */
271
    public function it_returns_not_found_response_when_requesting_partial_update_of_a_coupon_which_does_not_exist()
272
    {
273
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
274
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
275
        $promotion = $promotions['promotion2'];
276
277
        $this->client->request('PATCH', sprintf('/api/v1/promotions/%s/coupons/1', $promotion->getCode()), [], [], static::$authorizedHeaderWithContentType);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
278
279
        $response = $this->client->getResponse();
280
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
281
    }
282
283
    /**
284
     * @test
285
     */
286
    public function it_allows_to_partially_update_coupon()
287
    {
288
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
289
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
290
        $promotionCoupons = $this->loadFixturesFromFile('resources/promotion_coupons.yml');
291
        $promotion = $promotions['promotion2'];
292
        $promotionCoupon = $promotionCoupons['promotionCoupon2'];
293
294
        $data =
295
<<<EOT
296
        {
297
            "usageLimit": 30
298
        }
299
EOT;
300
301
        $this->client->request('PATCH', $this->getPromotionCouponUrl($promotion, $promotionCoupon), [], [], static::$authorizedHeaderWithContentType, $data);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
302
303
        $response = $this->client->getResponse();
304
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
305
306
        $this->client->request('GET', $this->getPromotionCouponUrl($promotion, $promotionCoupon), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
307
        $response = $this->client->getResponse();
308
309
        $this->assertResponse($response, 'promotion_coupon/partial_update_response', Response::HTTP_OK);
310
    }
311
312
    /**
313
     * @test
314
     */
315
    public function it_denies_coupon_delete_for_not_authenticated_users()
316
    {
317
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
318
        $promotion = $promotions['promotion2'];
319
320
        $this->client->request('DELETE', sprintf('/api/v1/promotions/%s/coupons/1', $promotion->getCode()));
321
322
        $response = $this->client->getResponse();
323
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
324
    }
325
326
    /**
327
     * @test
328
     */
329
    public function it_returns_not_found_response_when_requesting_delete_of_a_coupon_which_does_not_exist()
330
    {
331
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
332
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
333
        $promotion = $promotions['promotion2'];
334
335
        $this->client->request('DELETE', sprintf('/api/v1/promotions/%s/coupons/123123', $promotion->getCode()), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
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_a_coupon()
345
    {
346
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
347
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
348
        $promotionCoupons = $this->loadFixturesFromFile('resources/promotion_coupons.yml');
349
        $promotion = $promotions['promotion2'];
350
        $promotionCoupon = $promotionCoupons['promotionCoupon2'];
351
352
        $this->client->request('DELETE', $this->getPromotionCouponUrl($promotion, $promotionCoupon), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
353
354
        $response = $this->client->getResponse();
355
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
356
357
        $this->client->request('GET', $this->getPromotionCouponUrl($promotion, $promotionCoupon), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\PromotionCouponApiTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
358
359
        $response = $this->client->getResponse();
360
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
361
    }
362
363
    /**
364
     * @param PromotionInterface $promotion
365
     * @param PromotionCouponInterface|null $coupon
366
     *
367
     * @return string
368
     */
369
    private function getPromotionCouponUrl(PromotionInterface $promotion, PromotionCouponInterface $coupon = null)
370
    {
371
        return sprintf('/api/v1/promotions/%s/coupons/%s', $promotion->getCode(), $coupon->getCode());
0 ignored issues
show
Bug introduced by
It seems like $coupon is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
372
    }
373
374
    /**
375
     * @param PromotionInterface $promotion
376
     *
377
     * @return string
378
     */
379
    private function getPromotionCouponsUrl(PromotionInterface $promotion)
380
    {
381
        return sprintf('/api/v1/promotions/%s/coupons/', $promotion->getCode());
382
    }
383
}
384