1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2010, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CurrencyConverter\Test\TestCase\View\Helper; |
13
|
|
|
|
14
|
|
|
use Cake\Controller\Controller; |
15
|
|
|
use Cake\Network\Request; |
16
|
|
|
use Cake\Routing\Router; |
17
|
|
|
use Cake\TestSuite\TestCase; |
18
|
|
|
use Cake\View\View; |
19
|
|
|
use CurrencyConverter\View\Helper\CurrencyConverterHelper; |
20
|
|
|
use Cake\ORM\TableRegistry; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* |
25
|
|
|
* BEFORE TESTTING MAKE SURE |
26
|
|
|
* TO WRITE CURRENT DATETIME INTO THE FIRST RECORDS OF CurrencyratesFixture in tests/Fixture/CurrencyratesFixture |
27
|
|
|
* |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
class CurrencyConverterHelperTest extends TestCase { |
31
|
|
|
|
32
|
|
|
public $fixtures = ['plugin.CurrencyConverter.Currencyrates']; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Helper being tested |
36
|
|
|
* |
37
|
|
|
* @var \Ratings\View\Helper\CurrencyConverter |
38
|
|
|
*/ |
39
|
|
|
public $CurrencyConverter; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Cake\Http\ServerRequest |
43
|
|
|
*/ |
44
|
|
|
protected $request; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \Cake\View\View |
48
|
|
|
*/ |
49
|
|
|
protected $View; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \Cake\ORM\Table |
53
|
|
|
*/ |
54
|
|
|
protected $Table; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* (non-PHPdoc) |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function setUp() { |
62
|
|
|
parent::setUp(); |
63
|
|
|
$this->request = new Request(); |
|
|
|
|
64
|
|
|
$this->Controller = new Controller(); |
|
|
|
|
65
|
|
|
$this->View = new View($this->request); |
66
|
|
|
$this->CurrencyConverter = new CurrencyConverterHelper($this->View); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$table = TableRegistry::get('Currencyrates'); |
|
|
|
|
69
|
|
|
$this->Table = $table; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function testConfig() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$this->Component = new CurrencyConverterHelper($this->View, []); |
|
|
|
|
75
|
|
|
$expected = [ |
76
|
|
|
'database' => 2, |
77
|
|
|
'refresh' => 24, |
78
|
|
|
'decimal' => 2 |
79
|
|
|
]; |
80
|
|
|
$this->assertEquals($expected, $this->Component->getConfig()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function testConvertSameCurrency() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$amount = 20.00; |
86
|
|
|
$fromCurrency = 'EUR'; |
87
|
|
|
$toCurrency = 'EUR'; |
88
|
|
|
|
89
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
90
|
|
|
$expected = 20.00; |
91
|
|
|
$this->assertEquals($expected, $result); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function testConvertWithComma() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$amount = '20.00'; |
97
|
|
|
$fromCurrency = 'EUR'; |
98
|
|
|
$toCurrency = 'EUR'; |
99
|
|
|
|
100
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
101
|
|
|
$expected = 20.00; |
102
|
|
|
$this->assertEquals($expected, $result); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
public function testConvertNumberFormatting() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$amount = 20.123456; |
108
|
|
|
$fromCurrency = 'EUR'; |
109
|
|
|
$toCurrency = 'EUR'; |
110
|
|
|
|
111
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
112
|
|
|
$expected = 20.12; |
113
|
|
|
$this->assertEquals($expected, $result); |
114
|
|
|
|
115
|
|
|
$amount = 20.123456; |
116
|
|
|
$fromCurrency = 'EUR'; |
117
|
|
|
$toCurrency = 'EUR'; |
118
|
|
|
|
119
|
|
|
$this->CurrencyConverter = new CurrencyConverterHelper($this->View, [ |
|
|
|
|
120
|
|
|
'decimal' => 3 |
121
|
|
|
]); |
122
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
123
|
|
|
$expected = 20.123; |
124
|
|
|
$this->assertEquals($expected, $result); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
View Code Duplication |
public function testConvertUsingDatabaseWhenRateDoNotExistInDatabase() |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$amount = 20.00; |
130
|
|
|
$fromCurrency = 'EUR'; |
131
|
|
|
$toCurrency = 'USD'; |
132
|
|
|
|
133
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
134
|
|
|
$rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate; |
135
|
|
|
$expected = round(number_format($rate * 20.00, 2), 2); |
136
|
|
|
|
137
|
|
|
$this->assertEquals($expected, $result); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$amount = 20.00; |
143
|
|
|
$fromCurrency = 'EUR'; |
144
|
|
|
$toCurrency = 'GBP'; |
145
|
|
|
|
146
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
147
|
|
|
$expected = round(number_format(0.8 * 20.00, 2), 2); |
148
|
|
|
|
149
|
|
|
$this->assertEquals($expected, $result); |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
View Code Duplication |
public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$amount = 20.00; |
156
|
|
|
$fromCurrency = 'EUR'; |
157
|
|
|
$toCurrency = 'GBP'; |
158
|
|
|
|
159
|
|
|
$this->CurrencyConverter = new CurrencyConverterHelper($this->View, [ |
|
|
|
|
160
|
|
|
'refresh' => 0 |
161
|
|
|
]); |
162
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
163
|
|
|
$rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
164
|
|
|
$expected = round(number_format($rate * 20.00, 2), 2); |
165
|
|
|
|
166
|
|
|
$this->assertEquals($expected, $result); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
View Code Duplication |
public function testConvertNotUsingDatabse() |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$amount = 20.00; |
172
|
|
|
$fromCurrency = 'GBP'; |
173
|
|
|
$toCurrency = 'EUR'; |
174
|
|
|
|
175
|
|
|
$this->CurrencyConverter = new CurrencyConverterHelper($this->View, [ |
|
|
|
|
176
|
|
|
'database' => false |
177
|
|
|
]); |
178
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
179
|
|
|
|
180
|
|
|
$this->assertGreaterThan(20, $result); |
181
|
|
|
|
182
|
|
|
$result = count($this->Table->find('all')->toArray()); |
183
|
|
|
$this->assertEquals(1, $result); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
View Code Duplication |
public function testRateSameCurrency() |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$fromCurrency = 'EUR'; |
189
|
|
|
$toCurrency = 'EUR'; |
190
|
|
|
|
191
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
192
|
|
|
$expected = 1; |
193
|
|
|
$this->assertEquals($expected, $result); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
View Code Duplication |
public function testRateUsingDatabaseWhenRateDoNotExistInDatabase() |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
$fromCurrency = 'EUR'; |
199
|
|
|
$toCurrency = 'USD'; |
200
|
|
|
|
201
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
202
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate; |
203
|
|
|
|
204
|
|
|
$this->assertEquals($expected, $result); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
View Code Duplication |
public function testRateUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated() |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
$fromCurrency = 'EUR'; |
210
|
|
|
$toCurrency = 'GBP'; |
211
|
|
|
|
212
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
213
|
|
|
$expected = 0.8; |
214
|
|
|
|
215
|
|
|
$this->assertEquals($expected, $result); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
View Code Duplication |
public function testRateUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
$fromCurrency = 'EUR'; |
221
|
|
|
$toCurrency = 'GBP'; |
222
|
|
|
|
223
|
|
|
$this->CurrencyConverter = new CurrencyConverterHelper($this->View, [ |
|
|
|
|
224
|
|
|
'refresh' => 0 |
225
|
|
|
]); |
226
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
227
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
228
|
|
|
|
229
|
|
|
$this->assertEquals($expected, $result); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
View Code Duplication |
public function testRateNotUsingDatabse() |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
$fromCurrency = 'GBP'; |
235
|
|
|
$toCurrency = 'EUR'; |
236
|
|
|
|
237
|
|
|
$this->CurrencyConverter = new CurrencyConverterHelper($this->View, [ |
|
|
|
|
238
|
|
|
'database' => false |
239
|
|
|
]); |
240
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
241
|
|
|
|
242
|
|
|
$this->assertGreaterThan(1, $result); |
243
|
|
|
|
244
|
|
|
$result = count($this->Table->find('all')->toArray()); |
245
|
|
|
$this->assertEquals(1, $result); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function tearDown() |
249
|
|
|
{ |
250
|
|
|
parent::tearDown(); |
251
|
|
|
// Nettoie la Table |
252
|
|
|
TableRegistry::clear(); |
|
|
|
|
253
|
|
|
// Nettoie les variables quand les tests sont finis. |
254
|
|
|
unset($this->CurrencyConverter); |
255
|
|
|
} |
256
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..