|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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, []); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|
Late static binding only has effect in subclasses. A
finalclass cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::withself::.To learn more about late static binding, please refer to the PHP core documentation.