|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BenTools\Currency\Tests\Model; |
|
4
|
|
|
|
|
5
|
|
|
use BenTools\Currency\Model\Currency; |
|
6
|
|
|
use BenTools\Currency\Model\ExchangeRate; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class ExchangeRateTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
public function testConstruct() |
|
13
|
|
|
{ |
|
14
|
|
|
$sourceCurrency = new Currency('EUR'); |
|
15
|
|
|
$targetCurrency = new Currency('USD'); |
|
16
|
|
|
$ratio = 1.12; |
|
17
|
|
|
$exchangeRate = new ExchangeRate( |
|
18
|
|
|
$sourceCurrency, |
|
19
|
|
|
$targetCurrency, |
|
20
|
|
|
$ratio |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertSame($sourceCurrency, $exchangeRate->getSourceCurrency()); |
|
24
|
|
|
$this->assertSame($targetCurrency, $exchangeRate->getTargetCurrency()); |
|
25
|
|
|
$this->assertSame($ratio, $exchangeRate->getRatio()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @expectedException \InvalidArgumentException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testRatioZero() |
|
32
|
|
|
{ |
|
33
|
|
|
$sourceCurrency = new Currency('EUR'); |
|
34
|
|
|
$targetCurrency = new Currency('USD'); |
|
35
|
|
|
$ratio = 0; |
|
36
|
|
|
$exchangeRate = new ExchangeRate( |
|
|
|
|
|
|
37
|
|
|
$sourceCurrency, |
|
38
|
|
|
$targetCurrency, |
|
39
|
|
|
$ratio |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @expectedException \InvalidArgumentException |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testNegativeRatio() |
|
47
|
|
|
{ |
|
48
|
|
|
$sourceCurrency = new Currency('EUR'); |
|
49
|
|
|
$targetCurrency = new Currency('USD'); |
|
50
|
|
|
$ratio = -1; |
|
51
|
|
|
$exchangeRate = new ExchangeRate( |
|
|
|
|
|
|
52
|
|
|
$sourceCurrency, |
|
53
|
|
|
$targetCurrency, |
|
54
|
|
|
$ratio |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testSwapCurrencies() |
|
59
|
|
|
{ |
|
60
|
|
|
$sourceCurrency = new Currency('EUR'); |
|
61
|
|
|
$targetCurrency = new Currency('USD'); |
|
62
|
|
|
$ratio = 1.12; |
|
63
|
|
|
$exchangeRate = new ExchangeRate( |
|
64
|
|
|
$sourceCurrency, |
|
65
|
|
|
$targetCurrency, |
|
66
|
|
|
$ratio |
|
67
|
|
|
); |
|
68
|
|
|
$revertedExchangeRate = $exchangeRate->swapCurrencies(); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertNotSame($revertedExchangeRate, $exchangeRate); |
|
71
|
|
|
$this->assertSame($targetCurrency, $revertedExchangeRate->getSourceCurrency()); |
|
72
|
|
|
$this->assertSame($sourceCurrency, $revertedExchangeRate->getTargetCurrency()); |
|
73
|
|
|
$this->assertSame(1 / $ratio, $revertedExchangeRate->getRatio()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.