Passed
Pull Request — master (#16)
by
unknown
03:37
created

CurrencyConverterComponentTest::testAmountWithPointSavedInDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace CurrencyConverter\Test\TestCase\Controller\Component;
3
4
use CurrencyConverter\Controller\Component\CurrencyConverterComponent;
5
use Cake\Controller\Controller;
6
use Cake\Controller\ComponentRegistry;
7
use Cake\Event\Event;
8
use Cake\Http\ServerRequest;
9
use Cake\Http\Response;
10
use Cake\TestSuite\TestCase;
11
use Cake\ORM\TableRegistry;
12
use Cake\I18n\Time;
13
14
/**
15
 *
16
 *
17
 * BEFORE TESTTING MAKE SURE
18
 * TO WRITE CURRENT DATETIME INTO THE FIRST RECORDS OF CurrencyratesFixture in tests/Fixture/CurrencyratesFixture
19
 *
20
 *
21
 */
22
class CurrencyConverterComponentTest extends TestCase
23
{
24
25
    public $fixtures = ['plugin.CurrencyConverter.Currencyrates'];
26
27
    /**
28
     * Component being tested
29
     *
30
     * @var \CurrencyConverter\Controller\Component\CurrencyConverterComponent
31
     */
32
    public $CurrencyConverter;
33
34
    /**
35
     * @var \Cake\Http\ServerRequest
36
     */
37
    protected $request;
38
39
    /**
40
     * @var \Cake\Http\Response
41
     */
42
    protected $response;
43
44
     /**
45
     * @var \Cake\Controller\Controller
46
     */
47
    protected $controller;
48
49
    /**
50
     * @var \Cake\Controller\ComponentRegistry
51
     */
52
    protected $registry;
53
54
    /**
55
     * @var \Cake\ORM\Table
56
     */
57
    protected $Table;
58
59
    public function setUp()
60
    {
61
        // Configuration de notre component et de notre faux controller de test.
62
        $this->Request = new ServerRequest();
63
        $this->Response = new Response();
64
        $this->Controller = new Controller($this->Request, $this->Response);
65
        $this->Registry = new ComponentRegistry($this->Controller);
66
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, []);
67
68
        $table = TableRegistry::get('Currencyrates');
69
        $this->Table = $table;
70
    }
71
72
    public function testConfig()
73
    {
74
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, []);
75
        $expected = [
76
            'database' => 2,
77
            'refresh' => 24,
78
            'decimal' => 2,
79
            'round' => false
80
        ];
81
        $this->assertEquals($expected, $this->CurrencyConverter->getConfig());
82
    }
83
84
    public function testConvertSameCurrency()
85
    {
86
        $amount = 20.00;
87
        $fromCurrency = 'EUR';
88
        $toCurrency = 'EUR';
89
90
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
91
        $expected = 20.00;
92
        $this->assertEquals($expected, $result);
93
    }
94
95
    public function testConvertWithComma()
96
    {
97
        $amount = '20.00';
98
        $fromCurrency = 'EUR';
99
        $toCurrency = 'EUR';
100
101
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
102
        $expected = 20.00;
103
        $this->assertEquals($expected, $result);
104
    }
105
106
    public function testConvertNumberFormatting()
107
    {
108
        $amount = 20.123456;
109
        $fromCurrency = 'EUR';
110
        $toCurrency = 'EUR';
111
112
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
113
        $expected = 20.12;
114
        $this->assertEquals($expected, $result);
115
116
        $amount = 20.123456;
117
        $fromCurrency = 'EUR';
118
        $toCurrency = 'EUR';
119
120
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
121
            'decimal' => 3
122
        ]);
123
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
124
        $expected = 20.123;
125
        $this->assertEquals($expected, $result);
126
127
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
128
            'round' => 0
129
        ]);
130
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
131
        $expected = 20.12;
132
        $this->assertEquals($expected, $result);
133
134
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
135
            'round' => 4
136
        ]);
137
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
138
        $expected = 20.25;
139
        $this->assertEquals($expected, $result);
140
141
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
142
            'round' => 1
143
        ]);
144
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
145
        $expected = 21;
146
        $this->assertEquals($expected, $result);
147
148
        $amount = 20.00;
149
        $fromCurrency = 'EUR';
150
        $toCurrency = 'EUR';
151
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
152
            'round' => 4
153
        ]);
154
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
155
        $expected = 20.00;
156
        $this->assertEquals($expected, $result);
157
158
        $amount = 20.88;
159
        $fromCurrency = 'EUR';
160
        $toCurrency = 'EUR';
161
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
162
            'round' => 4
163
        ]);
164
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
165
        $expected = 21.00;
166
        $this->assertEquals($expected, $result);
167
    }
168
169
    public function testConvertUsingDatabaseWhenRateDoNotExistInDatabase()
170
    {
171
        $amount = 20.00;
172
        $fromCurrency = 'EUR';
173
        $toCurrency = 'USD';
174
175
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
176
        $rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate;
177
        $expected = round(number_format($rate * 20.00, 2), 2);
178
        $this->assertEquals($expected, $result);
179
180
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-USD')['rate'];
181
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate;
182
        $this->assertEquals($expected, $result);
183
184
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-USD')['modified'];
185
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->get('modified')->i18nFormat('yyyy-MM-dd HH:mm:ss');
186
        $this->assertEquals($expected, $result);
187
    }
188
189
    public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated()
190
    {
191
        $amount = 20.00;
192
        $fromCurrency = 'EUR';
193
        $toCurrency = 'GBP';
194
195
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
196
        $expected = number_format(0.8 * 20.00, 2);
197
        $this->assertEquals($expected, $result);
198
199
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['rate'];
200
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
201
        $this->assertEquals($expected, $result);
202
203
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['modified'];
204
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->get('modified')->i18nFormat('yyyy-MM-dd HH:mm:ss');
205
        $this->assertEquals($expected, $result);
206
    }
207
208
    public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated()
209
    {
210
        $amount = 20.00;
211
        $fromCurrency = 'EUR';
212
        $toCurrency = 'GBP';
213
214
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
215
            'refresh' => 0
216
        ]);
217
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
218
        $rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
219
        $expected = round(number_format($rate * 20.00, 2), 2);
220
        $this->assertEquals($expected, $result);
221
222
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['rate'];
223
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
224
        $this->assertEquals($expected, $result);
225
226
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['modified'];
227
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->modified->i18nFormat('yyyy-MM-dd HH:mm:ss');
228
        $this->assertEquals($expected, $result);
229
    }
230
231
    public function testConvertUsingDatabaseWhenRateExistInSession()
232
    {
233
        $amount = 20.00;
234
        $fromCurrency = 'EUR';
235
        $toCurrency = 'GBP';
236
237
        $now = Time::now()->i18nFormat('yyyy-MM-dd HH:mm:ss');
238
        $this->Request->getSession()->write('CurrencyConverter.EUR-GBP', [
239
            'rate' => 0.15,
240
            'modified' => $now
241
        ]);
242
243
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
244
        $expected = number_format(0.15 * 20.00, 2);
245
        $this->assertEquals($expected, $result);
246
    }
247
248
    public function testConvertUsingDatabaseWhenRateExistInSessionAndNeedToBeUpdated()
249
    {
250
        $amount = 20.00;
251
        $fromCurrency = 'EUR';
252
        $toCurrency = 'GBP';
253
254
        $expiredDatetime = Time::now()->modify('-5 days')->i18nFormat('yyyy-MM-dd HH:mm:ss');
255
        $this->Request->getSession()->write('CurrencyConverter.EUR-GBP', [
256
            'rate' => 0.1,
257
            'modified' => $expiredDatetime
258
        ]);
259
260
        $entity = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first();
261
        $entity->set('modified', $expiredDatetime);
262
        $this->Table->save($entity);
263
264
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
265
        $rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
266
        $expected = number_format($rate * 20.00, 2);
267
        $this->assertEquals($expected, $result);
268
269
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP.rate');
270
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
271
        $this->assertEquals($expected, $result);
272
273
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP.modified');
274
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->modified->i18nFormat('yyyy-MM-dd HH:mm:ss');
275
        $this->assertEquals($expected, $result);
276
    }
277
278
    public function testConvertNotUsingDatabse()
279
    {
280
        $amount = 20.00;
281
        $fromCurrency = 'GBP';
282
        $toCurrency = 'EUR';
283
284
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
285
            'database' => false
286
        ]);
287
        $result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency);
288
289
        $this->assertGreaterThan(20, $result);
290
291
        $result = count($this->Table->find('all')->toArray());
292
        $this->assertEquals(1, $result);
293
    }
294
295
296
    public function testRateUsingDatabaseWhenRateDoNotExistInDatabase()
297
    {
298
        $fromCurrency = 'EUR';
299
        $toCurrency = 'USD';
300
301
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
302
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate;
303
        $this->assertEquals($expected, $result);
304
305
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-USD')['rate'];
306
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate;
307
        $this->assertEquals($expected, $result);
308
309
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-USD')['modified'];
310
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->modified->i18nFormat('yyyy-MM-dd HH:mm:ss');
311
        $this->assertEquals($expected, $result);
312
    }
313
314
    public function testRateUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated()
315
    {
316
        $fromCurrency = 'EUR';
317
        $toCurrency = 'GBP';
318
319
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
320
        $expected = 0.8;
321
        $this->assertEquals($expected, $result);
322
323
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['rate'];
324
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
325
        $this->assertEquals($expected, $result);
326
327
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['modified'];
328
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->get('modified')->i18nFormat('yyyy-MM-dd HH:mm:ss');
329
        $this->assertEquals($expected, $result);
330
    }
331
332
    public function testRateUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated()
333
    {
334
        $fromCurrency = 'EUR';
335
        $toCurrency = 'GBP';
336
337
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
338
            'refresh' => 0
339
        ]);
340
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
341
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
342
        $this->assertEquals($expected, $result);
343
344
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['rate'];
345
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
346
        $this->assertEquals($expected, $result);
347
348
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['modified'];
349
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->modified->i18nFormat('yyyy-MM-dd HH:mm:ss');
350
        $this->assertEquals($expected, $result);
351
    }
352
353
    public function testRatetUsingDatabaseWhenRateExistInSession()
354
    {
355
        $amount = 20.00;
356
        $fromCurrency = 'EUR';
357
        $toCurrency = 'GBP';
358
359
        $now = Time::now()->i18nFormat('yyyy-MM-dd HH:mm:ss');
360
        $this->Request->getSession()->write('CurrencyConverter.EUR-GBP', [
361
            'rate' => 0.15,
362
            'modified' => $now
363
        ]);
364
365
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
366
        $expected = 0.15;
367
        $this->assertEquals($expected, $result);
368
    }
369
370
    public function testRateUsingDatabaseWhenRateExistInSessionAndNeedToBeUpdated()
371
    {
372
        $amount = 20.00;
373
        $fromCurrency = 'EUR';
374
        $toCurrency = 'GBP';
375
376
        $expired = Time::now()->modify('-5 days')->i18nFormat('yyyy-MM-dd HH:mm:ss');
377
        $this->Request->getSession()->write('CurrencyConverter.EUR-GBP', [
378
            'rate' => 0.1,
379
            'modified' => $expired
380
        ]);
381
382
        $entity = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first();
383
        $entity->set('modified', $expired);
384
        $this->Table->save($entity);
385
386
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
387
        $rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
388
        $expected = $rate;
389
        $this->assertEquals($expected, $result);
390
391
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP.rate');
392
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate;
393
        $this->assertEquals($expected, $result);
394
395
        $result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP.modified');
396
        $expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->modified->i18nFormat('yyyy-MM-dd HH:mm:ss');
397
        $this->assertEquals($expected, $result);
398
    }
399
400
    public function testRateNotUsingDatabse()
401
    {
402
        $fromCurrency = 'GBP';
403
        $toCurrency = 'EUR';
404
405
        $this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [
406
            'database' => false
407
        ]);
408
        $result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency);
409
410
        $this->assertGreaterThan(1, $result);
411
412
        $result = count($this->Table->find('all')->toArray());
413
        $this->assertEquals(1, $result);
414
    }
415
416
    public function tearDown()
417
    {
418
        parent::tearDown();
419
        // Nettoie la Table
420
        TableRegistry::clear();
421
        // Nettoie les variables quand les tests sont finis.
422
        unset($this->Controller, $this->CurrencyConverter);
423
    }
424
}
425