-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from jbrinksmeier/master
fixes #22 adopt to new endpoint and account structure of fixer.io
- Loading branch information
Showing
6 changed files
with
167 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace CurrencyConverter\Provider; | ||
|
||
use CurrencyConverter\Exception\UnsupportedCurrencyException; | ||
use GuzzleHttp\Client; | ||
|
||
/** | ||
* Get exchange rates from https://exchangeratesapi.io/ | ||
*/ | ||
class ExchangeRatesIo implements ProviderInterface | ||
{ | ||
/** | ||
* Base url of fixer api | ||
* | ||
* @var string | ||
*/ | ||
const EXCHANGERATESIO_API_BASEPATH = 'https://exchangeratesapi.io/api/latest'; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
protected $httpClient; | ||
|
||
/** | ||
* FixerApi constructor. | ||
* @param Client|null $httpClient | ||
*/ | ||
public function __construct(Client $httpClient = null) | ||
{ | ||
$this->httpClient = $httpClient ?: new Client(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRate($fromCurrency, $toCurrency) | ||
{ | ||
$path = sprintf( | ||
self::EXCHANGERATESIO_API_BASEPATH . '?symbols=%s&base=%s', | ||
$toCurrency, | ||
$fromCurrency | ||
); | ||
$result = json_decode($this->httpClient->get($path)->getBody(), true); | ||
|
||
if (!isset($result['rates'][$toCurrency])) { | ||
throw new UnsupportedCurrencyException(sprintf('Undefined rate for "%s" currency.', $toCurrency)); | ||
} | ||
|
||
return $result['rates'][$toCurrency]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
tests/CurrencyConverterTest/Provider/ExchangeRatesIoTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
namespace CurrencyConverter\Provider; | ||
|
||
use CurrencyConverter\Exception\UnsupportedCurrencyException; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7\Stream; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class ExchangeRatesIoTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testGetRate() | ||
{ | ||
$response = $this->getMock(ResponseInterface::class); | ||
$response | ||
->expects($this->any()) | ||
->method('getBody') | ||
->will($this->returnValue( | ||
new Stream(fopen('data://text/plain,{"base":"EUR","date":"2017-11-02","rates":{"USD":1.1645}}', 'r')) | ||
)); | ||
$httpClient = $this->getMock(Client::class); | ||
$httpClient | ||
->expects($this->once()) | ||
->method('__call') | ||
->with( | ||
'get', | ||
[ExchangeRatesIo::EXCHANGERATESIO_API_BASEPATH . '?symbols=USD&base=EUR'] | ||
) | ||
->will($this->returnValue($response)); | ||
$this->assertEquals( | ||
1.1645, | ||
(new ExchangeRatesIo($httpClient))->getRate('EUR', 'USD') | ||
); | ||
} | ||
|
||
public function testGetUnavailableRate() | ||
{ | ||
$response = $this->getMock(ResponseInterface::class); | ||
$response | ||
->expects($this->any()) | ||
->method('getBody') | ||
->will($this->returnValue( | ||
new Stream(fopen('data://text/plain,{"base":"EUR","date":"2017-11-28","rates":{}}', 'r')) | ||
)); | ||
$httpClient = $this->getMock(Client::class); | ||
$httpClient | ||
->expects($this->once()) | ||
->method('__call') | ||
->with( | ||
'get', | ||
[ExchangeRatesIo::EXCHANGERATESIO_API_BASEPATH . '?symbols=XXX&base=EUR'] | ||
) | ||
->will($this->returnValue($response)); | ||
|
||
$this->setExpectedException(UnsupportedCurrencyException::class); | ||
(new ExchangeRatesIo($httpClient))->getRate('EUR', 'XXX'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters