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()->modified; |
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
|
|
|
|
198
|
|
|
$this->assertEquals($expected, $result); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() |
202
|
|
|
{ |
203
|
|
|
$amount = 20.00; |
204
|
|
|
$fromCurrency = 'EUR'; |
205
|
|
|
$toCurrency = 'GBP'; |
206
|
|
|
|
207
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
208
|
|
|
'refresh' => 0 |
209
|
|
|
]); |
210
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
211
|
|
|
$rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
212
|
|
|
$expected = round(number_format($rate * 20.00, 2), 2); |
213
|
|
|
$this->assertEquals($expected, $result); |
214
|
|
|
|
215
|
|
|
$result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['rate']; |
216
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
217
|
|
|
$this->assertEquals($expected, $result); |
218
|
|
|
|
219
|
|
|
$result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['modified']; |
220
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->modified; |
221
|
|
|
$this->assertEquals($expected, $result); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testConvertUsingDatabaseWhenRateExistInSession() |
225
|
|
|
{ |
226
|
|
|
$amount = 20.00; |
227
|
|
|
$fromCurrency = 'EUR'; |
228
|
|
|
$toCurrency = 'GBP'; |
229
|
|
|
|
230
|
|
|
$now = Time::now()->i18nFormat('yyyy-MM-dd HH:mm:ss'); |
231
|
|
|
$this->Request->getSession()->write('CurrencyConverter.EUR-GBP', [ |
232
|
|
|
'rate' => 0.15, |
233
|
|
|
'modified' => $now |
234
|
|
|
]); |
235
|
|
|
|
236
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
237
|
|
|
$rate = 0.15; |
238
|
|
|
$expected = number_format($rate * 20.00, 2); |
239
|
|
|
$this->assertEquals($expected, $result); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testConvertUsingDatabaseWhenRateExistInSessionAndNeedToBeUpdated() |
243
|
|
|
{ |
244
|
|
|
$amount = 20.00; |
245
|
|
|
$fromCurrency = 'EUR'; |
246
|
|
|
$toCurrency = 'GBP'; |
247
|
|
|
|
248
|
|
|
$expired = Time::now()->modify('-5 days')->i18nFormat('yyyy-MM-dd HH:mm:ss'); |
249
|
|
|
$this->Request->getSession()->write('CurrencyConverter.EUR-GBP', [ |
250
|
|
|
'rate' => 0.7, |
251
|
|
|
'modified' => $expired |
252
|
|
|
]); |
253
|
|
|
|
254
|
|
|
$entity = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first(); |
255
|
|
|
$entity->set('modified', $expired); |
256
|
|
|
$this->Table->save($entity); |
257
|
|
|
|
258
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
259
|
|
|
$rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
260
|
|
|
$expected = number_format($rate * 20.00, 2); |
261
|
|
|
$this->assertEquals($expected, $result); |
262
|
|
|
|
263
|
|
|
$result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP.rate'); |
264
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
265
|
|
|
$this->assertEquals($expected, $result); |
266
|
|
|
|
267
|
|
|
$result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP.modified'); |
268
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->modified; |
269
|
|
|
$this->assertEquals($expected, $result); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function testConvertNotUsingDatabse() |
273
|
|
|
{ |
274
|
|
|
$amount = 20.00; |
275
|
|
|
$fromCurrency = 'GBP'; |
276
|
|
|
$toCurrency = 'EUR'; |
277
|
|
|
|
278
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
279
|
|
|
'database' => false |
280
|
|
|
]); |
281
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
282
|
|
|
|
283
|
|
|
$this->assertGreaterThan(20, $result); |
284
|
|
|
|
285
|
|
|
$result = count($this->Table->find('all')->toArray()); |
286
|
|
|
$this->assertEquals(1, $result); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
public function testRateUsingDatabaseWhenRateDoNotExistInDatabase() |
291
|
|
|
{ |
292
|
|
|
$fromCurrency = 'EUR'; |
293
|
|
|
$toCurrency = 'USD'; |
294
|
|
|
|
295
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
296
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate; |
297
|
|
|
$this->assertEquals($expected, $result); |
298
|
|
|
|
299
|
|
|
$result = $this->Request->getSession()->read('CurrencyConverter.EUR-USD')['rate']; |
300
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate; |
301
|
|
|
$this->assertEquals($expected, $result); |
302
|
|
|
|
303
|
|
|
$result = $this->Request->getSession()->read('CurrencyConverter.EUR-USD')['modified']; |
304
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->modified; |
305
|
|
|
$this->assertEquals($expected, $result); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function testRateUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated() |
309
|
|
|
{ |
310
|
|
|
$fromCurrency = 'EUR'; |
311
|
|
|
$toCurrency = 'GBP'; |
312
|
|
|
|
313
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
314
|
|
|
$expected = 0.8; |
315
|
|
|
|
316
|
|
|
$this->assertEquals($expected, $result); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
public function testRateUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() |
320
|
|
|
{ |
321
|
|
|
$fromCurrency = 'EUR'; |
322
|
|
|
$toCurrency = 'GBP'; |
323
|
|
|
|
324
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
325
|
|
|
'refresh' => 0 |
326
|
|
|
]); |
327
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
328
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
329
|
|
|
$this->assertEquals($expected, $result); |
330
|
|
|
|
331
|
|
|
$result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['rate']; |
332
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
333
|
|
|
$this->assertEquals($expected, $result); |
334
|
|
|
|
335
|
|
|
$result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP')['modified']; |
336
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->modified; |
337
|
|
|
$this->assertEquals($expected, $result); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
public function testRatetUsingDatabaseWhenRateExistInSession() |
341
|
|
|
{ |
342
|
|
|
$amount = 20.00; |
343
|
|
|
$fromCurrency = 'EUR'; |
344
|
|
|
$toCurrency = 'GBP'; |
345
|
|
|
|
346
|
|
|
$now = Time::now()->i18nFormat('yyyy-MM-dd HH:mm:ss'); |
347
|
|
|
$this->Request->getSession()->write('CurrencyConverter.EUR-GBP', [ |
348
|
|
|
'rate' => 0.15, |
349
|
|
|
'modified' => $now |
350
|
|
|
]); |
351
|
|
|
|
352
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
353
|
|
|
$expected = 0.15; |
354
|
|
|
$this->assertEquals($expected, $result); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
public function testRateUsingDatabaseWhenRateExistInSessionAndNeedToBeUpdated() |
358
|
|
|
{ |
359
|
|
|
$amount = 20.00; |
360
|
|
|
$fromCurrency = 'EUR'; |
361
|
|
|
$toCurrency = 'GBP'; |
362
|
|
|
|
363
|
|
|
$expired = Time::now()->modify('-5 days')->i18nFormat('yyyy-MM-dd HH:mm:ss'); |
364
|
|
|
$this->Request->getSession()->write('CurrencyConverter.EUR-GBP', [ |
365
|
|
|
'rate' => 0.7, |
366
|
|
|
'modified' => $expired |
367
|
|
|
]); |
368
|
|
|
|
369
|
|
|
$entity = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first(); |
370
|
|
|
$entity->set('modified', $expired); |
371
|
|
|
$this->Table->save($entity); |
372
|
|
|
|
373
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
374
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
375
|
|
|
$this->assertEquals($expected, $result); |
376
|
|
|
|
377
|
|
|
$result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP.rate'); |
378
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
379
|
|
|
$this->assertEquals($expected, $result); |
380
|
|
|
|
381
|
|
|
$result = $this->Request->getSession()->read('CurrencyConverter.EUR-GBP.modified'); |
382
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->modified; |
383
|
|
|
$this->assertEquals($expected, $result); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
public function testRateNotUsingDatabse() |
387
|
|
|
{ |
388
|
|
|
$fromCurrency = 'GBP'; |
389
|
|
|
$toCurrency = 'EUR'; |
390
|
|
|
|
391
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
392
|
|
|
'database' => false |
393
|
|
|
]); |
394
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
395
|
|
|
|
396
|
|
|
$this->assertGreaterThan(1, $result); |
397
|
|
|
|
398
|
|
|
$result = count($this->Table->find('all')->toArray()); |
399
|
|
|
$this->assertEquals(1, $result); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
public function tearDown() |
403
|
|
|
{ |
404
|
|
|
parent::tearDown(); |
405
|
|
|
// Nettoie la Table |
406
|
|
|
TableRegistry::clear(); |
407
|
|
|
// Nettoie les variables quand les tests sont finis. |
408
|
|
|
unset($this->Controller, $this->CurrencyConverter); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|