Completed
Push — master ( 2af63f...bfd93b )
by Paweł
76:32 queued 64:10
created

it_does_not_allow_to_update_exchange_rate_if_ratio_is_not_a_number()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
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\Currency\Model\ExchangeRateInterface;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * @author Łukasz Chruściel <[email protected]>
20
 */
21
final class ExchangeRateApiTest 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_exchange_rate_for_non_authenticated_user()
43
    {
44
        $this->client->request('POST', '/api/v1/exchange-rates/');
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_exchange_rate_without_specifying_required_data()
54
    {
55
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
56
57
        $this->client->request('POST', '/api/v1/exchange-rates/', [], [], static::$authorizedHeaderWithContentType);
58
59
        $response = $this->client->getResponse();
60
        $this->assertResponse($response, 'exchange_rate/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function it_allows_to_create_exchange_rate()
67
    {
68
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
69
        $this->loadFixturesFromFile('resources/currencies.yml');
70
71
        $data =
72
<<<EOT
73
        {
74
            "ratio": "0,8515706",
75
            "source_currency": "EUR",
76
            "target_currency": "GBP"
77
        }
78
EOT;
79
80
        $this->client->request('POST', '/api/v1/exchange-rates/', [], [], static::$authorizedHeaderWithContentType, $data);
81
82
        $response = $this->client->getResponse();
83
        $this->assertResponse($response, 'exchange_rate/create_response', Response::HTTP_CREATED);
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function it_does_not_allow_to_create_exchange_rate_with_duplicated_currency_pair()
90
    {
91
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
92
        $this->loadFixturesFromFile('resources/exchange_rates.yml');
93
94
        $data =
95
<<<EOT
96
        {
97
            "ratio": "0,8515706",
98
            "source_currency": "EUR",
99
            "target_currency": "GBP"
100
        }
101
EOT;
102
103
        $this->client->request('POST', '/api/v1/exchange-rates/', [], [], static::$authorizedHeaderWithContentType, $data);
104
105
        $response = $this->client->getResponse();
106
        $this->assertResponse($response, 'exchange_rate/non_unique_pair_validation_fail_response', Response::HTTP_BAD_REQUEST);
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function it_denies_getting_exchange_rates_for_non_authenticated_user()
113
    {
114
        $this->client->request('GET', '/api/v1/exchange-rates/');
115
116
        $response = $this->client->getResponse();
117
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
118
    }
119
120
    /**
121
     * @test
122
     */
123
    public function it_allows_to_get_exchange_rates_list()
124
    {
125
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
126
        $this->loadFixturesFromFile('resources/exchange_rates.yml');
127
128
        $this->client->request('GET', '/api/v1/exchange-rates/', [], [], static::$authorizedHeaderWithContentType);
129
130
        $response = $this->client->getResponse();
131
        $this->assertResponse($response, 'exchange_rate/index_response', Response::HTTP_OK);
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function it_denies_getting_exchange_rate_for_non_authenticated_user()
138
    {
139
        $this->client->request('GET', '/api/v1/exchange-rates/EUR-USD');
140
141
        $response = $this->client->getResponse();
142
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
143
    }
144
145
    /**
146
     * @test
147
     */
148
    public function it_returns_not_found_response_when_requesting_details_of_a_exchange_rate_which_does_not_exist()
149
    {
150
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
151
152
        $this->client->request('GET', '/api/v1/exchange-rates/EUR-USD', [], [], static::$authorizedHeaderWithAccept);
153
154
        $response = $this->client->getResponse();
155
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
156
    }
157
158
    /**
159
     * @test
160
     */
161
    public function it_allows_to_get_exchange_rate()
162
    {
163
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
164
        $exchangeRates = $this->loadFixturesFromFile('resources/exchange_rates.yml');
165
166
        /** @var ExchangeRateInterface $exchangeRate */
167
        $exchangeRate = $exchangeRates['eur_gbp_exchange_rate'];
168
169
        $this->client->request('GET', $this->getExchangeRateUrl($exchangeRate), [], [], static::$authorizedHeaderWithAccept);
170
171
        $response = $this->client->getResponse();
172
        $this->assertResponse($response, 'exchange_rate/show_response', Response::HTTP_OK);
173
    }
174
175
    /**
176
     * @test
177
     */
178
    public function it_denies_updating_exchange_rate_for_non_authenticated_user()
179
    {
180
        $this->client->request('PUT', '/api/v1/exchange-rates/EUR-USD');
181
182
        $response = $this->client->getResponse();
183
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
184
    }
185
186
    /**
187
     * @test
188
     */
189
    public function it_returns_not_found_response_when_updating_exchange_rate_which_does_not_exist()
190
    {
191
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
192
193
        $this->client->request('PUT', '/api/v1/exchange-rates/EUR-USD', [], [], static::$authorizedHeaderWithAccept);
194
195
        $response = $this->client->getResponse();
196
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
197
    }
198
199
    /**
200
     * @test
201
     */
202
    public function it_does_not_allow_to_update_exchange_rate_without_specifying_required_data()
203
    {
204
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
205
        $exchangeRates = $this->loadFixturesFromFile('resources/exchange_rates.yml');
206
207
        /** @var ExchangeRateInterface $exchangeRate */
208
        $exchangeRate = $exchangeRates['eur_usd_exchange_rate'];
209
210
        $this->client->request('PUT', $this->getExchangeRateUrl($exchangeRate), [], [], static::$authorizedHeaderWithContentType);
211
212
        $response = $this->client->getResponse();
213
        $this->assertResponse($response, 'exchange_rate/update_validation_fail_response', Response::HTTP_BAD_REQUEST);
214
    }
215
216
    /**
217
     * @test
218
     */
219
    public function it_allows_to_update_exchange_rate()
220
    {
221
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
222
        $exchangeRates = $this->loadFixturesFromFile('resources/exchange_rates.yml');
223
224
        /** @var ExchangeRateInterface $exchangeRate */
225
        $exchangeRate = $exchangeRates['eur_usd_exchange_rate'];
226
227
        $data =
228
<<<EOT
229
        {
230
            "ratio": "0.84"
231
        }
232
EOT;
233
234
        $this->client->request('PUT', $this->getExchangeRateUrl($exchangeRate), [], [], static::$authorizedHeaderWithContentType, $data);
235
236
        $response = $this->client->getResponse();
237
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
238
239
        $this->client->request('GET', $this->getExchangeRateUrl($exchangeRate), [], [], static::$authorizedHeaderWithAccept);
240
241
        $response = $this->client->getResponse();
242
        $this->assertResponse($response, 'exchange_rate/update_response', Response::HTTP_OK);
243
    }
244
245
    /**
246
     * @test
247
     */
248
    public function it_does_not_allow_to_update_exchange_rate_if_ratio_is_not_a_number()
249
    {
250
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
251
        $exchangeRates = $this->loadFixturesFromFile('resources/exchange_rates.yml');
252
253
        /** @var ExchangeRateInterface $exchangeRate */
254
        $exchangeRate = $exchangeRates['eur_usd_exchange_rate'];
255
256
        $data =
257
<<<EOT
258
        {
259
            "ratio": "its-a-trap"
260
        }
261
EOT;
262
263
        $this->client->request('PUT', $this->getExchangeRateUrl($exchangeRate), [], [], static::$authorizedHeaderWithContentType, $data);
264
265
        $response = $this->client->getResponse();
266
        $this->assertResponse($response, 'exchange_rate/update_ratio_with_string_validation_fail_response', Response::HTTP_BAD_REQUEST);
267
    }
268
269
    /**
270
     * @test
271
     */
272
    public function it_returns_not_found_response_when_trying_to_delete_exchange_rate_which_does_not_exist()
273
    {
274
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
275
276
        $this->client->request('DELETE', '/api/v1/exchange-rates/EUR-USD', [], [], static::$authorizedHeaderWithAccept);
277
278
        $response = $this->client->getResponse();
279
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
280
    }
281
282
    /**
283
     * @test
284
     */
285
    public function it_allows_to_delete_exchange_rate()
286
    {
287
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
288
        $exchangeRates = $this->loadFixturesFromFile('resources/exchange_rates.yml');
289
290
        /** @var ExchangeRateInterface $exchangeRate */
291
        $exchangeRate = $exchangeRates['eur_gbp_exchange_rate'];
292
293
        $this->client->request('DELETE', $this->getExchangeRateUrl($exchangeRate), [], [], static::$authorizedHeaderWithContentType, []);
294
295
        $response = $this->client->getResponse();
296
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
297
298
        $this->client->request('GET', $this->getExchangeRateUrl($exchangeRate), [], [], static::$authorizedHeaderWithAccept);
299
300
        $response = $this->client->getResponse();
301
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
302
    }
303
304
    /**
305
     * @param ExchangeRateInterface $exchangeRate
306
     *
307
     * @return string
308
     */
309
    private function getExchangeRateUrl(ExchangeRateInterface $exchangeRate)
310
    {
311
        return sprintf('/api/v1/exchange-rates/%s-%s', $exchangeRate->getSourceCurrency()->getCode(), $exchangeRate->getTargetCurrency()->getCode());
312
    }
313
}
314