1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CurrencyConverter\Test\TestCase\Controller\Component; |
4
|
|
|
|
5
|
|
|
use CurrencyConverter\Controller\Component\CurrencyConverterComponent; |
6
|
|
|
use Cake\Controller\Controller; |
7
|
|
|
use Cake\Controller\ComponentRegistry; |
8
|
|
|
use Cake\Event\Event; |
9
|
|
|
use Cake\Http\ServerRequest; |
10
|
|
|
use Cake\Http\Response; |
11
|
|
|
use Cake\TestSuite\TestCase; |
12
|
|
|
use Cake\ORM\TableRegistry; |
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
|
|
|
$request = new ServerRequest(); |
63
|
|
|
$response = new Response(); |
64
|
|
|
$this->Controller = new Controller($request, $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
|
|
View Code Duplication |
public function testConfig() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, []); |
|
|
|
|
75
|
|
|
$expected = [ |
76
|
|
|
'database' => 2, |
77
|
|
|
'refresh' => 24, |
78
|
|
|
'decimal' => 2 |
79
|
|
|
]; |
80
|
|
|
$this->assertEquals($expected, $this->CurrencyConverter->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 CurrencyConverterComponent($this->Registry, [ |
|
|
|
|
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
|
|
View Code Duplication |
public function testConvertUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$amount = 20.00; |
155
|
|
|
$fromCurrency = 'EUR'; |
156
|
|
|
$toCurrency = 'GBP'; |
157
|
|
|
|
158
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
|
|
|
|
159
|
|
|
'refresh' => 0 |
160
|
|
|
]); |
161
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
162
|
|
|
$rate = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
163
|
|
|
$expected = round(number_format($rate * 20.00, 2), 2); |
164
|
|
|
|
165
|
|
|
$this->assertEquals($expected, $result); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
View Code Duplication |
public function testConvertNotUsingDatabse() |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
$amount = 20.00; |
171
|
|
|
$fromCurrency = 'GBP'; |
172
|
|
|
$toCurrency = 'EUR'; |
173
|
|
|
|
174
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
|
|
|
|
175
|
|
|
'database' => false |
176
|
|
|
]); |
177
|
|
|
$result = $this->CurrencyConverter->convert($amount, $fromCurrency, $toCurrency); |
178
|
|
|
|
179
|
|
|
$this->assertGreaterThan(20, $result); |
180
|
|
|
|
181
|
|
|
$result = count($this->Table->find('all')->toArray()); |
182
|
|
|
$this->assertEquals(1, $result); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
View Code Duplication |
public function testRateSameCurrency() |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
$fromCurrency = 'EUR'; |
188
|
|
|
$toCurrency = 'EUR'; |
189
|
|
|
|
190
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
191
|
|
|
$expected = 1; |
192
|
|
|
$this->assertEquals($expected, $result); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
View Code Duplication |
public function testRateUsingDatabaseWhenRateDoNotExistInDatabase() |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
$fromCurrency = 'EUR'; |
198
|
|
|
$toCurrency = 'USD'; |
199
|
|
|
|
200
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
201
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'USD'])->first()->rate; |
202
|
|
|
|
203
|
|
|
$this->assertEquals($expected, $result); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
View Code Duplication |
public function testRateUsingDatabaseWhenRateExistInDatabaseAndNoNeedToBeUpdated() |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
$fromCurrency = 'EUR'; |
209
|
|
|
$toCurrency = 'GBP'; |
210
|
|
|
|
211
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
212
|
|
|
$expected = 0.8; |
213
|
|
|
|
214
|
|
|
$this->assertEquals($expected, $result); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
View Code Duplication |
public function testRateUsingDatabaseWhenRateExistInDatabaseAndNeedToBeUpdated() |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$fromCurrency = 'EUR'; |
220
|
|
|
$toCurrency = 'GBP'; |
221
|
|
|
|
222
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
|
|
|
|
223
|
|
|
'refresh' => 0 |
224
|
|
|
]); |
225
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
226
|
|
|
$expected = $this->Table->find('all')->where(['from_currency' => 'EUR', 'to_currency' => 'GBP'])->first()->rate; |
227
|
|
|
|
228
|
|
|
$this->assertEquals($expected, $result); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
View Code Duplication |
public function testRateNotUsingDatabse() |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
$fromCurrency = 'GBP'; |
234
|
|
|
$toCurrency = 'EUR'; |
235
|
|
|
|
236
|
|
|
$this->CurrencyConverter = new CurrencyConverterComponent($this->Registry, [ |
|
|
|
|
237
|
|
|
'database' => false |
238
|
|
|
]); |
239
|
|
|
$result = $this->CurrencyConverter->rate($fromCurrency, $toCurrency); |
240
|
|
|
|
241
|
|
|
$this->assertGreaterThan(1, $result); |
242
|
|
|
|
243
|
|
|
$result = count($this->Table->find('all')->toArray()); |
244
|
|
|
$this->assertEquals(1, $result); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function tearDown() |
248
|
|
|
{ |
249
|
|
|
parent::tearDown(); |
250
|
|
|
// Nettoie la Table |
251
|
|
|
TableRegistry::clear(); |
|
|
|
|
252
|
|
|
// Nettoie les variables quand les tests sont finis. |
253
|
|
|
unset($this->CurrencyConverter, $this->Controller); |
254
|
|
|
} |
255
|
|
|
} |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.