Completed
Push — master ( 213c59...15c2ca )
by Kamil
25:02 queued 13:36
created

it_allows_to_create_channel_with_required_data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 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
declare(strict_types=1);
13
14
namespace Sylius\Tests\Controller;
15
16
use Lakion\ApiTestCase\JsonApiTestCase;
17
use Sylius\Component\Channel\Model\ChannelInterface;
18
use Symfony\Component\HttpFoundation\Response;
19
20
final class ChannelApiTest extends JsonApiTestCase
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $authorizedHeaderWithAccept = [
26
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
27
        'ACCEPT' => 'application/json',
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    private static $authorizedHeaderWithContentType = [
34
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
35
        'CONTENT_TYPE' => 'application/json',
36
    ];
37
38
    /**
39
     * @test
40
     */
41
    public function it_does_not_allow_to_show_channels_list_when_access_is_denied()
42
    {
43
        $this->loadFixturesFromFile('resources/channels.yml');
44
45
        $this->client->request('GET', '/api/v1/channels/');
46
47
        $response = $this->client->getResponse();
48
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function it_denies_creating_channels_for_non_authenticated_user()
55
    {
56
        $this->client->request('POST', '/api/v1/channels/');
57
58
        $response = $this->client->getResponse();
59
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function it_does_not_allow_to_create_channel_without_specifying_required_data()
66
    {
67
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
68
69
        $this->client->request('POST', '/api/v1/channels/', [], [], static::$authorizedHeaderWithContentType);
70
71
        $response = $this->client->getResponse();
72
        $this->assertResponse($response, 'channel/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function it_allows_indexing_channels()
79
    {
80
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
81
        $this->loadFixturesFromFile('resources/channels.yml');
82
83
        $this->client->request('GET', '/api/v1/channels/', [], [], static::$authorizedHeaderWithAccept);
84
85
        $response = $this->client->getResponse();
86
        $this->assertResponse($response, 'channel/index_response', Response::HTTP_OK);
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function it_denies_getting_channel_for_non_authenticated_user()
93
    {
94
        $this->client->request('GET', '/api/v1/channels/none');
95
96
        $response = $this->client->getResponse();
97
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
98
    }
99
100
    /**
101
     * @test
102
     */
103
    public function it_does_not_allow_to_show_channel_when_it_does_not_exist()
104
    {
105
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
106
107
        $this->client->request('GET', '/api/v1/channels/none', [], [], static::$authorizedHeaderWithContentType);
108
109
        $response = $this->client->getResponse();
110
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
111
    }
112
113
    /**
114
     * @test
115
     */
116
    public function it_allows_to_create_channel_with_required_data()
117
    {
118
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
119
        $this->loadFixturesFromFile('resources/locales.yml');
120
        $this->loadFixturesFromFile('resources/currencies.yml');
121
122
        $data =
123
            <<<EOT
124
        {
125
            "code": "mob",
126
            "name": "Channel for mobile",
127
            "taxCalculationStrategy": "order_items_based",
128
            "baseCurrency": "USD",
129
            "defaultLocale": "en_US",
130
            "enabled": true
131
        }
132
EOT;
133
134
        $this->client->request('POST', '/api/v1/channels/', [], [], static::$authorizedHeaderWithContentType, $data);
135
136
        $response = $this->client->getResponse();
137
        $this->assertResponse($response, 'channel/create_response', Response::HTTP_CREATED);
138
    }
139
140
    /**
141
     * @test
142
     */
143
    public function it_allows_to_create_channel_with_extra_data()
144
    {
145
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
146
        $this->loadFixturesFromFile('resources/locales.yml');
147
        $this->loadFixturesFromFile('resources/currencies.yml');
148
        $this->loadFixturesFromFile('resources/zones.yml');
149
150
        $data =
151
            <<<EOT
152
        {
153
            "code": "android",
154
            "name": "Channel for Android client",
155
            "taxCalculationStrategy": "order_items_based",
156
            "baseCurrency": "EUR",
157
            "defaultLocale": "en_US",
158
            "enabled": true,
159
            "description": "For now not required, for future development stages.",
160
            "defaultTaxZone": "EU",
161
            "hostname": "quickmart.eu",
162
            "currencies": [
163
                "GBP"
164
            ],
165
            "color": "ClassicBlue",
166
            "contactEmail": "[email protected]",
167
            "skippingShippingStepAllowed": true,
168
            "skippingPaymentStepAllowed": true,
169
            "accountVerificationRequired": false
170
        }
171
EOT;
172
173
        $this->client->request('POST', '/api/v1/channels/', [], [], static::$authorizedHeaderWithContentType, $data);
174
175
        $response = $this->client->getResponse();
176
        $this->assertResponse($response, 'channel/create_with_extra_response', Response::HTTP_CREATED);
177
    }
178
179
    /**
180
     * @test
181
     */
182
    public function it_denies_updating_channel_for_non_authenticated_user()
183
    {
184
        $this->client->request('PUT', '/api/v1/channels/1');
185
186
        $response = $this->client->getResponse();
187
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
188
    }
189
190
    /**
191
     * @test
192
     */
193
    public function it_does_not_allow_to_update_channel_without_specifying_required_data()
194
    {
195
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
196
        $channels = $this->loadFixturesFromFile('resources/channels.yml');
197
198
        /** @var ChannelInterface $channel */
199
        $channel = $channels['channel_web'];
200
201
        $this->client->request('PUT', $this->getChannelUrl($channel), [], [], static::$authorizedHeaderWithContentType);
202
203
        $response = $this->client->getResponse();
204
        $this->assertResponse($response, 'channel/update_validation_fail_response', Response::HTTP_BAD_REQUEST);
205
    }
206
207
    /**
208
     * @test
209
     */
210
    public function it_allows_to_update_channel()
211
    {
212
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
213
        $channels = $this->loadFixturesFromFile('resources/channels.yml');
214
215
        /** @var ChannelInterface $channel */
216
        $channel = $channels['channel_web'];
217
218
        $data =
219
            <<<EOT
220
        {
221
            "color": "black",
222
            "enabled": false,
223
            "description": "Lorem ipsum",
224
            "name": "Web Channel",
225
            "hostname": "localhost",
226
            "taxCalculationStrategy": "order_items_based"
227
        }
228
EOT;
229
230
        $this->client->request('PUT', $this->getChannelUrl($channel), [], [], static::$authorizedHeaderWithContentType, $data);
231
232
        $response = $this->client->getResponse();
233
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
234
235
        $this->client->request('GET', $this->getChannelUrl($channel), [], [], static::$authorizedHeaderWithAccept);
236
237
        $response = $this->client->getResponse();
238
        $this->assertResponse($response, 'channel/update_response', Response::HTTP_OK);
239
    }
240
241
    /**
242
     * @test
243
     */
244
    public function it_allows_showing_channel()
245
    {
246
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
247
        $channels = $this->loadFixturesFromFile('resources/channels.yml');
248
        /** @var ChannelInterface $channel */
249
        $channel = $channels['channel_web'];
250
251
        $this->client->request('GET', $this->getChannelUrl($channel), [], [], static::$authorizedHeaderWithContentType);
252
253
        $response = $this->client->getResponse();
254
        $this->assertResponse($response, 'channel/show_response', Response::HTTP_OK);
255
    }
256
257
    /**
258
     * @test
259
     */
260
    public function it_returns_not_found_response_when_partially_updating_channel_which_does_not_exist()
261
    {
262
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
263
264
        $this->client->request('PATCH', '/api/v1/channels/-1', [], [], static::$authorizedHeaderWithAccept);
265
266
        $response = $this->client->getResponse();
267
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
268
    }
269
270
    /**
271
     * @test
272
     */
273
    public function it_allows_to_partially_update_channel()
274
    {
275
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
276
        $channels = $this->loadFixturesFromFile('resources/channels.yml');
277
278
        /** @var ChannelInterface $channel */
279
        $channel = $channels['channel_web'];
280
281
        $data =
282
            <<<EOT
283
        {
284
            "color": "black",
285
            "enabled": false
286
        }
287
EOT;
288
289
        $this->client->request('PATCH', $this->getChannelUrl($channel), [], [], static::$authorizedHeaderWithContentType, $data);
290
291
        $response = $this->client->getResponse();
292
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
293
294
        $this->client->request('GET', $this->getChannelUrl($channel), [], [], static::$authorizedHeaderWithAccept);
295
296
        $response = $this->client->getResponse();
297
        $this->assertResponse($response, 'channel/update_response', Response::HTTP_OK);
298
    }
299
300
    /**
301
     * @test
302
     */
303
    public function it_does_not_allow_to_delete_non_existing_channel()
304
    {
305
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
306
307
        $this->client->request('DELETE', '/api/v1/channels/-1', [], [], static::$authorizedHeaderWithAccept);
308
309
        $response = $this->client->getResponse();
310
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
311
    }
312
313
    /**
314
     * @test
315
     */
316
    public function it_allows_to_delete_channel()
317
    {
318
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
319
        $channels = $this->loadFixturesFromFile('resources/channels.yml');
320
        $channel = $channels['channel-mob'];
321
322
        $this->client->request('DELETE', $this->getChannelUrl($channel), [], [], static::$authorizedHeaderWithContentType, []);
323
324
        $response = $this->client->getResponse();
325
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
326
327
        $this->client->request('GET', $this->getChannelUrl($channel), [], [], static::$authorizedHeaderWithAccept);
328
329
        $response = $this->client->getResponse();
330
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
331
    }
332
333
    /**
334
     * @param ChannelInterface $channel
335
     *
336
     * @return string
337
     */
338
    private function getChannelUrl(ChannelInterface $channel)
339
    {
340
        return '/api/v1/channels/' . $channel->getCode();
341
    }
342
}
343
344