Completed
Push — checkout-optimisation ( 40d0de )
by Kamil
21:30
created

PromotionCouponApiTest::getPromotionCouponUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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 $authorizedHeaderWithAccept = [
28
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
29
        'ACCEPT' => 'application/json',
30
    ];
31
32
    /**
33
     * @test
34
     */
35
    public function it_does_not_allow_to_show_promotion_coupons_list_when_access_is_denied()
36
    {
37
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
38
        $promotion = $promotions['promotion2'];
39
40
        $this->loadFixturesFromFile('resources/promotion_coupons.yml');
41
42
        $this->client->request('GET', $this->getPromotionCouponsUrl($promotion));
43
44
        $response = $this->client->getResponse();
45
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function it_allows_indexing_promotion_coupons()
52
    {
53
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
54
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
55
        $promotion = $promotions['promotion2'];
56
        $this->loadFixturesFromFile('resources/promotion_coupons.yml');
57
58
        $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...
59
60
        $response = $this->client->getResponse();
61
        $this->assertResponse($response, 'promotion_coupon/index_response', Response::HTTP_OK);
62
    }
63
64
    /**
65
     * @test
66
     */
67
    public function it_does_not_allow_to_show_promotion_when_it_does_not_exist()
68
    {
69
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
70
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
71
        $promotion = $promotions['promotion2'];
72
73
        $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...
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_promotion_coupon()
83
    {
84
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
85
        $promotions = $this->loadFixturesFromFile('resources/promotions.yml');
86
        $promotion = $promotions['promotion2'];
87
88
        $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...
89
        $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...
90
91
        $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...
92
93
        $response = $this->client->getResponse();
94
        $this->assertResponse($response, 'promotion_coupon/show_response', Response::HTTP_OK);
95
    }
96
97
    /**
98
     * @param PromotionInterface $promotion
99
     * @param PromotionCouponInterface|null $coupon
100
     *
101
     * @return string
102
     */
103
    private function getPromotionCouponUrl(PromotionInterface $promotion, PromotionCouponInterface $coupon = null)
104
    {
105
        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...
106
    }
107
108
    /**
109
     * @param PromotionInterface $promotion
110
     *
111
     * @return string
112
     */
113
    private function getPromotionCouponsUrl(PromotionInterface $promotion)
114
    {
115
        return sprintf('/api/v1/promotions/%s/coupons/', $promotion->getCode());
116
    }
117
}
118