1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\Currency\Tests\Converter; |
4
|
|
|
|
5
|
|
|
use BenTools\Currency\Converter\CurrencyConverter; |
6
|
|
|
use BenTools\Currency\Model\Currency; |
7
|
|
|
use BenTools\Currency\Model\ExchangeRate; |
8
|
|
|
use BenTools\Currency\Model\ExchangeRateNotFoundException; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class CurrencyConverterTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public function testOneExchangeRateConverter() |
15
|
|
|
{ |
16
|
|
|
$USD = new Currency('USD'); |
17
|
|
|
$EUR = new Currency('EUR'); |
18
|
|
|
$converter = new CurrencyConverter(new ExchangeRate($USD, $EUR, 1.23145)); |
19
|
|
|
$this->assertEquals(2 * 1.23145, $converter->convert(2, $USD, $EUR)); |
20
|
|
|
$this->assertEquals(2 * (1 / 1.23145), $converter->convert(2, $EUR, $USD)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testMultipleExchangeRateConverter() |
24
|
|
|
{ |
25
|
|
|
$USD = new Currency('USD'); |
26
|
|
|
$EUR = new Currency('EUR'); |
27
|
|
|
$GBP = new Currency('GBP'); |
28
|
|
|
$USDtoEUR = new ExchangeRate($USD, $EUR, 1.23145); |
29
|
|
|
$EURtoGBP = new ExchangeRate($EUR, $GBP, 0.870596469); |
30
|
|
|
|
31
|
|
|
$converter = new CurrencyConverter($USDtoEUR, $EURtoGBP); |
32
|
|
|
$this->assertEquals(2 * 1.23145, $converter->convert(2, $USD, $EUR)); |
33
|
|
|
$this->assertEquals(2 * (1 / 1.23145), $converter->convert(2, $EUR, $USD)); |
34
|
|
|
$this->assertEquals(2 * 0.870596469, $converter->convert(2, $EUR, $GBP)); |
35
|
|
|
$this->assertEquals(2 * (1 / 0.870596469), $converter->convert(2, $GBP, $EUR)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @expectedException \BenTools\Currency\Model\ExchangeRateNotFoundException |
40
|
|
|
*/ |
41
|
|
|
public function testExchangeRateNotFound() |
42
|
|
|
{ |
43
|
|
|
$USD = new Currency('USD'); |
44
|
|
|
$EUR = new Currency('EUR'); |
45
|
|
|
$GBP = new Currency('GBP'); |
46
|
|
|
$USDtoEUR = new ExchangeRate($USD, $EUR, 1.23145); |
47
|
|
|
$EURtoGBP = new ExchangeRate($EUR, $GBP, 0.870596469); |
48
|
|
|
|
49
|
|
|
$converter = new CurrencyConverter($USDtoEUR, $EURtoGBP); |
50
|
|
|
$converter->convert(2, $USD, $GBP); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|