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\CurrencyLayerProvider; |
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 CurrencyLayerProviderTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
use ClientMockTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var CurrencyLayerProvider |
20
|
|
|
*/ |
21
|
|
|
private $provider; |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->client = new Client(); |
26
|
|
|
$this->provider = new CurrencyLayerProvider('dummy_access_key', $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('CurrencyLayer/2018-03-28.json')); |
35
|
|
|
$exchangeRate = $this->provider->getExchangeRate($EUR, $USD, new \DateTime('2018-03-28')); |
36
|
|
|
$this->assertInstanceOf(ExchangeRateInterface::class, $exchangeRate); |
37
|
|
|
$this->assertEquals(1 / 0.812031, $exchangeRate->getRatio()); |
38
|
|
|
|
39
|
|
|
$this->mockResponse(Tests::loadFixtureFile('CurrencyLayer/2018-03-28.json')); |
40
|
|
|
$exchangeRate = $this->provider->getExchangeRate($USD, $EUR, new \DateTime('2018-03-28')); |
41
|
|
|
$this->assertInstanceOf(ExchangeRateInterface::class, $exchangeRate); |
42
|
|
|
$this->assertEquals(0.812031, $exchangeRate->getRatio()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testGetLastExchangeRate() |
46
|
|
|
{ |
47
|
|
|
$EUR = new Currency('EUR'); |
48
|
|
|
$USD = new Currency('USD'); |
49
|
|
|
|
50
|
|
|
$content = Tests::loadFixtureFile('CurrencyLayer/live.json'); |
51
|
|
|
|
52
|
|
|
$this->mockResponse($content); |
53
|
|
|
$exchangeRate = $this->provider->getExchangeRate($EUR, $USD); |
54
|
|
|
$this->assertInstanceOf(ExchangeRateInterface::class, $exchangeRate); |
55
|
|
|
$this->assertEquals(1 / 0.811199, $exchangeRate->getRatio()); |
56
|
|
|
|
57
|
|
|
$this->mockResponse($content); |
58
|
|
|
$exchangeRate = $this->provider->getExchangeRate($USD, $EUR); |
59
|
|
|
$this->assertInstanceOf(ExchangeRateInterface::class, $exchangeRate); |
60
|
|
|
$this->assertEquals(0.811199, $exchangeRate->getRatio()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @expectedException \BenTools\Currency\Model\ExchangeRateNotFoundException |
65
|
|
|
*/ |
66
|
|
|
public function testExchangeRateFails() |
67
|
|
|
{ |
68
|
|
|
$GBP = new Currency('GBP'); |
69
|
|
|
$EUR = new Currency('EUR'); |
70
|
|
|
|
71
|
|
|
$this->mockResponse(Tests::loadFixtureFile('CurrencyLayer/2018-03-28.json')); |
72
|
|
|
$exchangeRate = $this->provider->getExchangeRate($GBP, $EUR, new \DateTime('2018-03-28')); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.