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
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* |
16
|
|
|
* BEFORE TESTTING MAKE SURE |
17
|
|
|
* TO WRITE CURRENT DATETIME INTO THE FIRST RECORDS OF CurrencyratesFixture in tests/Fixture/CurrencyratesFixture |
18
|
|
|
* |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
class CurrencyConverterComponentTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
public $fixtures = ['plugin.CurrencyConverter.Currencyrates']; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Component being tested |
28
|
|
|
* |
29
|
|
|
* @var \CurrencyConverter\Controller\Component\CurrencyConverterComponent |
30
|
|
|
*/ |
31
|
|
|
public $CurrencyConverter; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Cake\Http\ServerRequest |
35
|
|
|
*/ |
36
|
|
|
protected $request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Cake\Http\Response |
40
|
|
|
*/ |
41
|
|
|
protected $response; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \Cake\Controller\Controller |
45
|
|
|
*/ |
46
|
|
|
protected $controller; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \Cake\Controller\ComponentRegistry |
50
|
|
|
*/ |
51
|
|
|
protected $registry; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \Cake\ORM\Table |
55
|
|
|
*/ |
56
|
|
|
protected $Table; |
57
|
|
|
|
58
|
|
|
public function setUp() |
59
|
|
|
{ |
60
|
|
|
// Configuration de notre component et de notre faux controller de test. |
61
|
|
|
$request = new ServerRequest(); |
62
|
|
|
$response = new Response(); |
63
|
|
|
$this->Controller = new Controller($request, $response); |
64
|
|
|
$this->Registry = new ComponentRegistry($this->Controller); |
65
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, []); |
66
|
|
|
|
67
|
|
|
$table = TableRegistry::get('Currencyrates'); |
68
|
|
|
$this->Table = $table; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testConfig() |
72
|
|
|
{ |
73
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, []); |
74
|
|
|
$expected = [ |
75
|
|
|
'database' => 2, |
76
|
|
|
'refresh' => 24, |
77
|
|
|
'decimal' => 2 |
78
|
|
|
]; |
79
|
|
|
$this->assertEquals($expected, $this->CurrencyConverter->getConfig()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testConvertSameCurrency() |
83
|
|
|
{ |
84
|
|
|
$amount = 20.00; |
85
|
|
|
$fromCurrency = 'EUR'; |
86
|
|
|
$toCurrency = 'EUR'; |
87
|
|
|
|
88
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
89
|
|
|
$expected = 20.00; |
90
|
|
|
$this->assertEquals($expected, $result); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testConvertWithComma() |
94
|
|
|
{ |
95
|
|
|
$amount = '20.00'; |
96
|
|
|
$fromCurrency = 'EUR'; |
97
|
|
|
$toCurrency = 'EUR'; |
98
|
|
|
|
99
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
100
|
|
|
$expected = 20.00; |
101
|
|
|
$this->assertEquals($expected, $result); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testConvertNumberFormatting() |
105
|
|
|
{ |
106
|
|
|
$amount = 20.123456; |
107
|
|
|
$fromCurrency = 'EUR'; |
108
|
|
|
$toCurrency = 'EUR'; |
109
|
|
|
|
110
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
111
|
|
|
$expected = 20.12; |
112
|
|
|
$this->assertEquals($expected, $result); |
113
|
|
|
|
114
|
|
|
$amount = 20.123456; |
115
|
|
|
$fromCurrency = 'EUR'; |
116
|
|
|
$toCurrency = 'EUR'; |
117
|
|
|
|
118
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
119
|
|
|
'decimal' => 3 |
120
|
|
|
]); |
121
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
122
|
|
|
$expected = 20.123; |
123
|
|
|
$this->assertEquals($expected, $result); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testConvertUsingDatabaseWhenRateDoNotExistInDatabase() |
127
|
|
|
{ |
128
|
|
|
$amount = 20.00; |
129
|
|
|
$fromCurrency = 'EUR'; |
130
|
|
|
$toCurrency = 'USD'; |
131
|
|
|
|
132
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
133
|
|
|
$rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate; |
134
|
|
|
$expected = round(number_format($rate * 20.00, 2), 2); |
135
|
|
|
|
136
|
|
|
$this->assertEquals($expected, $result); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated() |
140
|
|
|
{ |
141
|
|
|
$amount = 20.00; |
142
|
|
|
$fromCurrency = 'EUR'; |
143
|
|
|
$toCurrency = 'GBP'; |
144
|
|
|
|
145
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
146
|
|
|
$expected = round(number_format(0.8 * 20.00, 2), 2); |
147
|
|
|
|
148
|
|
|
$this->assertEquals($expected, $result); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() |
152
|
|
|
{ |
153
|
|
|
$amount = 20.00; |
154
|
|
|
$fromCurrency = 'EUR'; |
155
|
|
|
$toCurrency = 'GBP'; |
156
|
|
|
|
157
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
158
|
|
|
'refresh' => 0 |
159
|
|
|
]); |
160
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
161
|
|
|
$rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
162
|
|
|
$expected = round(number_format($rate * 20.00, 2), 2); |
163
|
|
|
|
164
|
|
|
$this->assertEquals($expected, $result); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testConvertNotUsingDatabse() |
168
|
|
|
{ |
169
|
|
|
$amount = 20.00; |
170
|
|
|
$fromCurrency = 'GBP'; |
171
|
|
|
$toCurrency = 'EUR'; |
172
|
|
|
|
173
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
174
|
|
|
'database' => false |
175
|
|
|
]); |
176
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
177
|
|
|
|
178
|
|
|
$this->assertGreaterThan(20, $result); |
179
|
|
|
|
180
|
|
|
$result = count($this->Table->find('all')->toArray()); |
181
|
|
|
$this->assertEquals(1, $result); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testRateSameCurrency() |
185
|
|
|
{ |
186
|
|
|
$fromCurrency = 'EUR'; |
187
|
|
|
$toCurrency = 'EUR'; |
188
|
|
|
|
189
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
190
|
|
|
$expected = 1; |
191
|
|
|
$this->assertEquals($expected, $result); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testRateUsingDatabaseWhenRateDoNotExistInDatabase() |
195
|
|
|
{ |
196
|
|
|
$fromCurrency = 'EUR'; |
197
|
|
|
$toCurrency = 'USD'; |
198
|
|
|
|
199
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
200
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate; |
201
|
|
|
|
202
|
|
|
$this->assertEquals($expected, $result); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testRateUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated() |
206
|
|
|
{ |
207
|
|
|
$fromCurrency = 'EUR'; |
208
|
|
|
$toCurrency = 'GBP'; |
209
|
|
|
|
210
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
211
|
|
|
$expected = 0.8; |
212
|
|
|
|
213
|
|
|
$this->assertEquals($expected, $result); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testRateUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() |
217
|
|
|
{ |
218
|
|
|
$fromCurrency = 'EUR'; |
219
|
|
|
$toCurrency = 'GBP'; |
220
|
|
|
|
221
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
222
|
|
|
'refresh' => 0 |
223
|
|
|
]); |
224
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
225
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
226
|
|
|
|
227
|
|
|
$this->assertEquals($expected, $result); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testRateNotUsingDatabse() |
231
|
|
|
{ |
232
|
|
|
$fromCurrency = 'GBP'; |
233
|
|
|
$toCurrency = 'EUR'; |
234
|
|
|
|
235
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
236
|
|
|
'database' => false |
237
|
|
|
]); |
238
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
239
|
|
|
|
240
|
|
|
$this->assertGreaterThan(1, $result); |
241
|
|
|
|
242
|
|
|
$result = count($this->Table->find('all')->toArray()); |
243
|
|
|
$this->assertEquals(1, $result); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function tearDown() |
247
|
|
|
{ |
248
|
|
|
parent::tearDown(); |
249
|
|
|
// Nettoie la Table |
250
|
|
|
TableRegistry::clear(); |
251
|
|
|
// Nettoie les variables quand les tests sont finis. |
252
|
|
|
unset($this->CurrencyConverter, $this->Controller); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|