1
|
|
|
<?php |
2
|
|
|
namespace App\Test\TestCase\Controller\Component; |
3
|
|
|
|
4
|
|
|
use CurrencyConverter\Controller\Component\CurrencyConverterComponent; |
5
|
|
|
use Cake\Controller\Controller; |
6
|
|
|
use Cake\Controller\ComponentRegistry; |
7
|
|
|
use Cake\Network\Request; |
8
|
|
|
use Cake\Network\Response; |
9
|
|
|
use Cake\TestSuite\TestCase; |
10
|
|
|
use Cake\ORM\TableRegistry; |
11
|
|
|
|
12
|
|
|
class CurrencyConverterComponentTest extends TestCase { |
13
|
|
|
public $fixtures = ['app.currencyconverter']; |
14
|
|
|
public $CurrencyConverter = null; |
15
|
|
|
public $controller = null; |
16
|
|
|
|
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
|
21
|
|
|
$this->currencyConverter = new CurrencyConverterComponent(new ComponentRegistry(new Controller)); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
View Code Duplication |
public function testAmountWithComma() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$fromCurrency = 'EUR'; |
27
|
|
|
$toCurrency = 'GBP'; |
28
|
|
|
$amount = '20,00'; |
29
|
|
|
$saveIntoDb = 0; |
30
|
|
|
$hourDifference = 0; |
31
|
|
|
$dataSource = 'test'; |
32
|
|
|
|
33
|
|
|
$result = $this->currencyConverter->convert($fromCurrency, $toCurrency, $amount, $saveIntoDb, $hourDifference, $dataSource); |
|
|
|
|
34
|
|
|
$this->assertGreaterThan((float)$result, (float)$amount); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function testAmountWithPoint() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$fromCurrency = 'EUR'; |
40
|
|
|
$toCurrency = 'GBP'; |
41
|
|
|
$amount = '20.00'; |
42
|
|
|
$saveIntoDb = 0; |
43
|
|
|
$hourDifference = 0; |
44
|
|
|
$dataSource = 'test'; |
45
|
|
|
|
46
|
|
|
$result = $this->currencyConverter->convert($fromCurrency, $toCurrency, $amount, $saveIntoDb, $hourDifference, $dataSource); |
|
|
|
|
47
|
|
|
$this->assertGreaterThan($result, $amount); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function testAmountWithCommaSavedInDatabase() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$fromCurrency = 'EUR'; |
53
|
|
|
$toCurrency = 'GBP'; |
54
|
|
|
$amount = '20,00'; |
55
|
|
|
$saveIntoDb = true; |
56
|
|
|
$hourDifference = 1; |
57
|
|
|
$dataSource = 'test'; |
58
|
|
|
|
59
|
|
|
$result = $this->currencyConverter->convert($fromCurrency, $toCurrency, $amount, $saveIntoDb, $hourDifference, $dataSource); |
|
|
|
|
60
|
|
|
$this->assertGreaterThan($result, $amount); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testAmountWithPointSavedInDatabase() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$fromCurrency = 'EUR'; |
66
|
|
|
$toCurrency = 'GBP'; |
67
|
|
|
$amount = '20.00'; |
68
|
|
|
$saveIntoDb = 1; |
69
|
|
|
$hourDifference = 1; |
70
|
|
|
$dataSource = 'test'; |
71
|
|
|
|
72
|
|
|
$result = $this->currencyConverter->convert($fromCurrency, $toCurrency, $amount, $saveIntoDb, $hourDifference, $dataSource); |
|
|
|
|
73
|
|
|
$this->assertGreaterThan($result, $amount); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function testConvertToPDS() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$fromCurrency = 'EUR'; |
79
|
|
|
$toCurrency = 'PDS'; |
80
|
|
|
$amount = '20.00'; |
81
|
|
|
$saveIntoDb = 1; |
82
|
|
|
$hourDifference = 1; |
83
|
|
|
$dataSource = 'test'; |
84
|
|
|
|
85
|
|
|
$result = $this->currencyConverter->convert($fromCurrency, $toCurrency, $amount, $saveIntoDb, $hourDifference, $dataSource); |
|
|
|
|
86
|
|
|
$this->assertGreaterThan($result, $amount); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function testConvertFromPDS() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$fromCurrency = 'PDS'; |
92
|
|
|
$toCurrency = 'EUR'; |
93
|
|
|
$amount = '20.00'; |
94
|
|
|
$saveIntoDb = 1; |
95
|
|
|
$hourDifference = 1; |
96
|
|
|
$dataSource = 'test'; |
97
|
|
|
|
98
|
|
|
$result = $this->currencyConverter->convert($fromCurrency, $toCurrency, $amount, $saveIntoDb, $hourDifference, $dataSource); |
|
|
|
|
99
|
|
|
$this->assertGreaterThan($amount, $result); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function testConvertSameCurrency() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$fromCurrency = 'EUR'; |
105
|
|
|
$toCurrency = 'EUR'; |
106
|
|
|
$amount = '20.00'; |
107
|
|
|
$saveIntoDb = 1; |
108
|
|
|
$hourDifference = 1; |
109
|
|
|
$dataSource = 'test'; |
110
|
|
|
|
111
|
|
|
$result = $this->currencyConverter->convert($fromCurrency, $toCurrency, $amount, $saveIntoDb, $hourDifference, $dataSource); |
|
|
|
|
112
|
|
|
$this->assertEquals($result, $amount); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testInsertCurrency() |
116
|
|
|
{ |
117
|
|
|
$currencyTable = TableRegistry::get('CurrencyConverter', [ |
118
|
|
|
'className' => 'CurrencyConverter\Model\Table\CurrencyConvertersTable', |
119
|
|
|
'table' => 'currency_converter' |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
$query = $currencyTable->find('all'); |
123
|
|
|
|
124
|
|
|
$query->hydrate(false); |
|
|
|
|
125
|
|
|
$result = $query->toArray(); |
126
|
|
|
|
127
|
|
|
foreach ($result as $row){ |
128
|
|
|
$currencyTable->deleteAll(['id' => $row['id']]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$fromCurrency = 'EUR'; |
132
|
|
|
$toCurrency = 'GBP'; |
133
|
|
|
$amount = '20.00'; |
134
|
|
|
$saveIntoDb = 1; |
135
|
|
|
$hourDifference = -1; |
136
|
|
|
$dataSource = 'test'; |
137
|
|
|
|
138
|
|
|
$resultConverted = $this->currencyConverter->convert($fromCurrency, $toCurrency, $amount, $saveIntoDb, $hourDifference, $dataSource); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$currencyTable = TableRegistry::get('CurrencyConverter'); |
141
|
|
|
$query = $currencyTable->find('all'); |
142
|
|
|
|
143
|
|
|
$query->hydrate(false); |
|
|
|
|
144
|
|
|
$result = $query->toArray(); |
145
|
|
|
|
146
|
|
|
$this->assertEquals(1, count($result)); |
147
|
|
|
$this->assertGreaterThan($resultConverted, $amount); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function tearDown() |
151
|
|
|
{ |
152
|
|
|
parent::tearDown(); |
153
|
|
|
unset($this->currencyConverter, $this->controller); |
154
|
|
|
} |
155
|
|
|
} |
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.