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\Bundle\CoreBundle\Tests\Controller; |
13
|
|
|
|
14
|
|
|
use Lakion\ApiTestCase\JsonApiTestCase; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Jeroen Thora <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class CountryApiTest extends JsonApiTestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $authorizedHeaderWithContentType = [ |
26
|
|
|
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', |
27
|
|
|
'CONTENT_TYPE' => 'application/json', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private static $authorizedHeaderWithAccept = [ |
34
|
|
|
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', |
35
|
|
|
'ACCEPT' => 'application/json', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
public function testCreateCountryAccessDeniedResponse() |
39
|
|
|
{ |
40
|
|
|
$this->client->request('POST', '/api/countries/'); |
41
|
|
|
|
42
|
|
|
$response = $this->client->getResponse(); |
43
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testCreateCountryValidationFailResponse() |
47
|
|
|
{ |
48
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
49
|
|
|
|
50
|
|
|
$this->client->request('POST', '/api/countries/', [], [], static::$authorizedHeaderWithContentType); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$response = $this->client->getResponse(); |
53
|
|
|
$this->assertResponse($response, 'country/create_validation_fail_response', Response::HTTP_BAD_REQUEST); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testCreateCountryResponse() |
57
|
|
|
{ |
58
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
59
|
|
|
|
60
|
|
|
$data = |
61
|
|
|
<<<EOT |
62
|
|
|
{ |
63
|
|
|
"code": "BE" |
64
|
|
|
} |
65
|
|
|
EOT; |
66
|
|
|
|
67
|
|
|
$this->client->request('POST', '/api/countries/', [], [], static::$authorizedHeaderWithContentType, $data); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$response = $this->client->getResponse(); |
70
|
|
|
$this->assertResponse($response, 'country/create_response', Response::HTTP_CREATED); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testGetSCountryWhichDoesNotExistResponse() |
74
|
|
|
{ |
75
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
76
|
|
|
|
77
|
|
|
$this->client->request('GET', '/api/countries/-1', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$response = $this->client->getResponse(); |
80
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testGetCountriesListResponse() |
84
|
|
|
{ |
85
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
86
|
|
|
$this->loadFixturesFromFile('resources/countries.yml'); |
87
|
|
|
|
88
|
|
|
$this->client->request('GET', '/api/countries/', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$response = $this->client->getResponse(); |
91
|
|
|
$this->assertResponse($response, 'country/index_response', Response::HTTP_OK); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testGetCountryResponse() |
95
|
|
|
{ |
96
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
97
|
|
|
$countries = $this->loadFixturesFromFile('resources/countries.yml'); |
98
|
|
|
|
99
|
|
|
$this->client->request('GET', '/api/countries/'.$countries['country_NL']->getId(), [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
100
|
|
|
|
101
|
|
|
$response = $this->client->getResponse(); |
102
|
|
|
$this->assertResponse($response, 'country/show_response', Response::HTTP_OK); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testGetCountryAccessDeniedResponse() |
106
|
|
|
{ |
107
|
|
|
$this->loadFixturesFromFile('resources/countries.yml'); |
108
|
|
|
$this->client->request('GET', '/api/countries/1'); |
109
|
|
|
|
110
|
|
|
$response = $this->client->getResponse(); |
111
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
public function testDeleteCountryWhichDoesNotExistResponse() |
116
|
|
|
{ |
117
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
118
|
|
|
|
119
|
|
|
$this->client->request('DELETE', '/api/countries/-1', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$response = $this->client->getResponse(); |
122
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testDeleteCountryResponse() |
126
|
|
|
{ |
127
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
128
|
|
|
$countries = $this->loadFixturesFromFile('resources/countries.yml'); |
129
|
|
|
|
130
|
|
|
$this->client->request('DELETE', '/api/countries/' . $countries['country_NL']->getId(), [], [], static::$authorizedHeaderWithContentType, []); |
|
|
|
|
131
|
|
|
|
132
|
|
|
$response = $this->client->getResponse(); |
133
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
134
|
|
|
|
135
|
|
|
$this->client->request('GET', '/api/countries/' . $countries['country_NL']->getId(), [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
136
|
|
|
|
137
|
|
|
$response = $this->client->getResponse(); |
138
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: