|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BenTools\Currency\Tests\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use BenTools\Currency\Model\Currency; |
|
6
|
|
|
use BenTools\Currency\Model\ExchangeRateInterface; |
|
7
|
|
|
use BenTools\Currency\Provider\EuropeanCentralBankProvider; |
|
8
|
|
|
use BenTools\Currency\Tests\ClientMockTrait; |
|
9
|
|
|
use BenTools\Currency\Tests\Tests; |
|
10
|
|
|
use Http\Mock\Client; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
|
|
13
|
|
|
class EuropeanCentralBankProviderTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
use ClientMockTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var EuropeanCentralBankProvider |
|
20
|
|
|
*/ |
|
21
|
|
|
private $provider; |
|
22
|
|
|
|
|
23
|
|
|
public function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->client = new Client(); |
|
26
|
|
|
$this->provider = new EuropeanCentralBankProvider($this->client); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testGetExchangeRate() |
|
30
|
|
|
{ |
|
31
|
|
|
$EUR = new Currency('EUR'); |
|
32
|
|
|
$USD = new Currency('USD'); |
|
33
|
|
|
|
|
34
|
|
|
$this->mockResponse(Tests::loadFixtureFile('EuropeanCentralBank/2018-03-28.xml')); |
|
35
|
|
|
$exchangeRate = $this->provider->getExchangeRate($EUR, $USD, new \DateTime('2018-03-28')); |
|
36
|
|
|
$this->assertInstanceOf(ExchangeRateInterface::class, $exchangeRate); |
|
37
|
|
|
$this->assertEquals(1.2398, $exchangeRate->getRatio()); |
|
38
|
|
|
|
|
39
|
|
|
$this->mockResponse(Tests::loadFixtureFile('EuropeanCentralBank/2018-03-28.xml')); |
|
40
|
|
|
$exchangeRate = $this->provider->getExchangeRate($USD, $EUR, new \DateTime('2018-03-28')); |
|
41
|
|
|
$this->assertInstanceOf(ExchangeRateInterface::class, $exchangeRate); |
|
42
|
|
|
$this->assertEquals(1 / 1.2398, $exchangeRate->getRatio()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testGetLastExchangeRate() |
|
46
|
|
|
{ |
|
47
|
|
|
$EUR = new Currency('EUR'); |
|
48
|
|
|
$USD = new Currency('USD'); |
|
49
|
|
|
|
|
50
|
|
|
$content = Tests::loadFixtureFile('EuropeanCentralBank/live.xml'); |
|
51
|
|
|
$content = str_replace('{{live}}', date('Y-m-d'), $content); |
|
52
|
|
|
|
|
53
|
|
|
$this->mockResponse($content); |
|
54
|
|
|
$exchangeRate = $this->provider->getExchangeRate($EUR, $USD); |
|
55
|
|
|
$this->assertInstanceOf(ExchangeRateInterface::class, $exchangeRate); |
|
56
|
|
|
$this->assertEquals(1.2304, $exchangeRate->getRatio()); |
|
57
|
|
|
|
|
58
|
|
|
$this->mockResponse($content); |
|
59
|
|
|
$exchangeRate = $this->provider->getExchangeRate($USD, $EUR); |
|
60
|
|
|
$this->assertInstanceOf(ExchangeRateInterface::class, $exchangeRate); |
|
61
|
|
|
$this->assertEquals(1 / 1.2304, $exchangeRate->getRatio()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @expectedException \BenTools\Currency\Model\ExchangeRateNotFoundException |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testExchangeRateFails() |
|
68
|
|
|
{ |
|
69
|
|
|
$GBP = new Currency('GBP'); |
|
70
|
|
|
$USD = new Currency('USD'); |
|
71
|
|
|
|
|
72
|
|
|
$this->mockResponse(Tests::loadFixtureFile('EuropeanCentralBank/2018-03-28.xml')); |
|
73
|
|
|
$exchangeRate = $this->provider->getExchangeRate($GBP, $USD, new \DateTime('2018-03-28')); |
|
|
|
|
|
|
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.