Completed
Push — locale-everywhere ( 46bdc3...e59a39 )
by Kamil
50:26 queued 26:56
created

CountryApiTest::it_allows_to_get_country()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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\Addressing\Model\CountryInterface;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * @author Jeroen Thora <[email protected]>
20
 */
21
final class CountryApiTest 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_denies_creating_country_for_non_authenticated_user()
43
    {
44
        $this->client->request('POST', '/api/v1/countries/');
45
46
        $response = $this->client->getResponse();
47
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function it_does_not_allow_to_create_country_without_specifying_required_data()
54
    {
55
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
56
57
        $this->client->request('POST', '/api/v1/countries/', [], [], static::$authorizedHeaderWithContentType);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\CountryApiTest 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...
58
59
        $response = $this->client->getResponse();
60
        $this->assertResponse($response, 'country/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function it_allows_to_create_country_with_given_code()
67
    {
68
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
69
70
        $data =
71
<<<EOT
72
        {
73
            "code": "BE"
74
        }
75
EOT;
76
77
        $this->client->request('POST', '/api/v1/countries/', [], [], static::$authorizedHeaderWithContentType, $data);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\CountryApiTest 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...
78
79
        $response = $this->client->getResponse();
80
        $this->assertResponse($response, 'country/create_response', Response::HTTP_CREATED);
81
    }
82
83
    /**
84
     * @test
85
     */
86
    public function it_allows_to_create_country_with_provinces()
87
    {
88
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
89
90
        $data =
91
<<<EOT
92
        {
93
            "code": "BE",
94
            "provinces": [
95
                {
96
                    "code": "BE-LM",
97
                    "name": "Limburg"
98
                }
99
            ]
100
        }
101
EOT;
102
103
        $this->client->request('POST', '/api/v1/countries/', [], [], static::$authorizedHeaderWithContentType, $data);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\CountryApiTest 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...
104
105
        $response = $this->client->getResponse();
106
        $this->assertResponse($response, 'country/create_with_province_response', Response::HTTP_CREATED);
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function it_returns_not_found_response_when_requesting_details_of_a_country_which_does_not_exist()
113
    {
114
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
115
116
        $this->client->request('GET', '/api/v1/countries/-1', [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\CountryApiTest 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...
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_to_get_countries_list()
126
    {
127
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
128
        $this->loadFixturesFromFile('resources/countries.yml');
129
130
        $this->client->request('GET', '/api/v1/countries/', [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\CountryApiTest 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...
131
132
        $response = $this->client->getResponse();
133
        $this->assertResponse($response, 'country/index_response', Response::HTTP_OK);
134
    }
135
136
    /**
137
     * @test
138
     */
139
    public function it_allows_to_get_country()
140
    {
141
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
142
        $countries = $this->loadFixturesFromFile('resources/countries.yml');
143
        $country = $countries['country_NL'];
144
145
        $this->client->request('GET', $this->getCountryUrl($country), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\CountryApiTest 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...
146
147
        $response = $this->client->getResponse();
148
        $this->assertResponse($response, 'country/show_response', Response::HTTP_OK);
149
    }
150
151
    /**
152
     * @test
153
     */
154
    public function it_denies_getting_country_for_non_authenticated_user()
155
    {
156
        $this->loadFixturesFromFile('resources/countries.yml');
157
        $this->client->request('GET', '/api/v1/countries/1');
158
159
        $response = $this->client->getResponse();
160
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
161
    }
162
163
    /**
164
     * @test
165
     */
166
    public function it_returns_not_found_response_when_trying_to_delete_country_which_does_not_exist()
167
    {
168
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
169
170
        $this->client->request('DELETE', '/api/v1/countries/-1', [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\CountryApiTest 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...
171
172
        $response = $this->client->getResponse();
173
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
174
    }
175
176
    /**
177
     * @test
178
     */
179
    public function it_allows_to_delete_country()
180
    {
181
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
182
        $countries = $this->loadFixturesFromFile('resources/countries.yml');
183
        $country = $countries['country_NL'];
184
185
        $this->client->request('DELETE', $this->getCountryUrl($country), [], [], static::$authorizedHeaderWithContentType, []);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\CountryApiTest 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...
Documentation introduced by
array() is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
186
187
        $response = $this->client->getResponse();
188
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
189
190
        $this->client->request('GET', $this->getCountryUrl($country), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Comprehensibility introduced by
Since Sylius\Tests\Controller\CountryApiTest 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...
191
192
        $response = $this->client->getResponse();
193
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
194
    }
195
196
    /**
197
     * @param CountryInterface $country
198
     *
199
     * @return string
200
     */
201
    private function getCountryUrl(CountryInterface $country)
202
    {
203
        return '/api/v1/countries/' . $country->getCode();
204
    }
205
}
206