Skip to content

Commit

Permalink
Fix CurrencyLayer and add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
otherguy committed Jun 13, 2019
1 parent ac2dab1 commit ce04e79
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 48 deletions.
94 changes: 48 additions & 46 deletions src/Drivers/CurrencyLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ class CurrencyLayer extends BaseCurrencyDriver implements CurrencyDriverContract
/** @var string $baseCurrency CurrencyLayer's Free Plan base currency is 'USD' */
protected $baseCurrency = Symbol::USD;

protected $httpParams = [
'format' => 1
protected $httpParams = [
'format' => 1,
];


/**
* @param string|array $forCurrency
*
Expand All @@ -40,12 +39,45 @@ public function get($forCurrency = []): ConversionResult
// Get API response
$response = $this->apiRequest('live', [
'source' => $this->getBaseCurrency(),
'currencies' => join(',', $this->getSymbols())
'currencies' => join(',', $this->getSymbols()),
]);

// Transform rates response
$rates = [];
foreach($response['quotes'] as $currency => $rate) {
foreach ($response['quotes'] as $currency => $rate) {
$rates[substr($currency, 3, 3)] = $rate;
}

return new ConversionResult($response['source'], $response['timestamp'], $rates);
}

/**
* @param int|string|DateTime $date
* @param string|array $forCurrency
*
* @return ConversionResult
*
* @throws CurrencyException
*/
public function historical($date = null, $forCurrency = []): ConversionResult
{
// Set date
$this->date($date);

if (!empty((array)$forCurrency)) {
$this->currencies((array)$forCurrency);
}

// Get API response
$response = $this->apiRequest('historical', [
'date' => $this->date,
'source' => $this->getBaseCurrency(),
'currencies' => join(',', $this->getSymbols()),
]);

// Transform rates response
$rates = [];
foreach ($response['quotes'] as $currency => $rate) {
$rates[substr($currency, 3, 3)] = $rate;
}

Expand All @@ -69,64 +101,35 @@ public function convert(float $amount = null, string $fromCurrency = null, strin
$this->date($date);

// Overwrite/set params
if($amount !== null) {
if ($amount !== null) {
$this->amount = $amount;
}

if($fromCurrency !== null) {
if ($fromCurrency !== null) {
$this->baseCurrency = $fromCurrency;
}

if($toCurrency !== null) {
if ($toCurrency !== null) {
$this->currencies = [$toCurrency];
}

// Get API response
$response = $this->apiRequest('convert', [
$params = [
'from' => $this->getBaseCurrency(),
'to' => reset($this->currencies),
'amount' => $this->amount
]);

// Return the rate as a float
return floatval($response['info']['rate']);
}

/**
* @param int|string|DateTime $date
* @param string|array $forCurrency
*
* @return ConversionResult
*
* @throws CurrencyException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function historical($date = null, $forCurrency = []): ConversionResult
{
// Set date
$this->date($date);
'amount' => $this->amount,
];

if (!empty((array)$forCurrency)) {
$this->currencies((array)$forCurrency);
if (null !== $this->getDate()) {
$params['date'] = $this->getDate();
}

// Get API response
$response = $this->apiRequest('historical', [
'date' => $this->date,
'source' => $this->getBaseCurrency(),
'currencies' => join(',', $this->getSymbols())
]);

// Transform rates response
$rates = [];
foreach($response['quotes'] as $currency => $rate) {
$rates[substr($currency, 3, 3)] = $rate;
}
$response = $this->apiRequest('convert', $params);

return new ConversionResult($response['source'], $response['timestamp'], $rates);
// Return the rate as a float
return floatval($response['result']);
}


/**
* Performs an HTTP request.
*
Expand All @@ -150,5 +153,4 @@ function apiRequest(string $endpoint, array $params = [], string $method = 'GET'

return $response;
}

}
1 change: 1 addition & 0 deletions src/Drivers/FixerIo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FixerIo extends BaseCurrencyDriver implements CurrencyDriverContract
/** @var string $baseCurrency Fixer.io's Free Plan base currency is 'EUR' */
protected $baseCurrency = Symbol::EUR;


/**
* @param string|array $forCurrency
*
Expand Down
89 changes: 89 additions & 0 deletions tests/Drivers/CurrencyLayerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use Otherguy\Currency\DriverFactory;
use Otherguy\Currency\Drivers\CurrencyLayer;
use Otherguy\Currency\Exceptions\ApiException;
use Otherguy\Currency\Results\ConversionResult;
use Otherguy\Currency\Symbol;
use PHPUnit\Framework\TestCase;

/**
* CurrencyLayerTest
*/
class CurrencyLayerTest extends TestCase
{
/** @var CurrencyLayer */
private $currencyLayer;

private $mockHandler;

protected function setUp()
{
$this->mockHandler = new MockHandler();
$this->currencyLayer = DriverFactory::make('currencylayer', new Client(['handler' => $this->mockHandler]));
}

/** @test */
public function can_get_latest_rates()
{
// Response from https://currencylayer.com/documentation
$this->mockHandler->append(new Response(200, [], '{"success":true,"terms":"https://currencylayer.com/terms","privacy":"https://currencylayer.com/privacy","timestamp":1432400348,"source":"USD","quotes":{"USDAUD":1.278342,"USDEUR":1.278342,"USDGBP":0.908019,"USDPLN":3.731504}}'));

$result = $this->currencyLayer->from(Symbol::USD)->get([Symbol::AUD, Symbol::EUR, Symbol::GBP, Symbol::PLN]);

$this->assertInstanceOf(ConversionResult::class, $result);

$this->assertEquals(Symbol::USD, $result->getBaseCurrency());
$this->assertEquals('2015-05-23', $result->getDate());

$this->assertEquals(1.278342, $result->rate(Symbol::AUD));
$this->assertEquals(1.278342, $result->rate(Symbol::EUR));
$this->assertEquals(0.908019, $result->rate(Symbol::GBP));
$this->assertEquals(3.731504, $result->rate(Symbol::PLN));
}


/** @test */
public function can_get_historical_rates()
{
// Response from https://currencylayer.com/documentation
$this->mockHandler->append(new Response(200, [], '{"success":true,"terms":"https://currencylayer.com/terms","privacy":"https://currencylayer.com/privacy","historical":true,"date":"2005-02-01","timestamp":1107302399,"source":"USD","quotes":{"USDAED":3.67266,"USDALL":96.848753,"USDAMD":475.798297,"USDANG":1.790403,"USDARS":2.918969,"USDAUD":1.293878}}'));

$result = $this->currencyLayer->from(Symbol::USD)->historical('2005-02-01', [Symbol::AED, Symbol::ALL, Symbol::AMD, Symbol::ANG, Symbol::ARS, Symbol::AUD]);

$this->assertInstanceOf(ConversionResult::class, $result);

$this->assertEquals(Symbol::USD, $result->getBaseCurrency());
$this->assertEquals('2005-02-01', $result->getDate());

$this->assertEquals(3.67266, $result->rate(Symbol::AED));
$this->assertEquals(96.848753, $result->rate(Symbol::ALL));
$this->assertEquals(475.798297, $result->rate(Symbol::AMD));
$this->assertEquals(1.790403, $result->rate(Symbol::ANG));
$this->assertEquals(2.918969, $result->rate(Symbol::ARS));
$this->assertEquals(1.293878, $result->rate(Symbol::AUD));
}

/** @test */
public function can_convert_currency_amounts()
{
// Response from https://currencylayer.com/documentation
$this->mockHandler->append(new Response(200, [], '{"success":true,"terms":"https://currencylayer.com/terms","privacy":"https://currencylayer.com/privacy","query":{"from":"USD","to":"GBP","amount":10},"info":{"timestamp":1430068515,"quote":0.658443},"result":6.58443}'));

$result = $this->currencyLayer->from(Symbol::USD)->date(1430068515)->convert(10, Symbol::USD, Symbol::GBP);
$this->assertEquals(6.58443, $result);
}

/** @test */
public function can_handle_response_failures()
{
// Response from https://currencylayer.com/documentation
$this->mockHandler->append(new Response(200, [], '{"success":false,"error":{"code":104,"info":"Your monthly usage limit has been reached. Please upgrade your subscription plan."}}'));

$this->expectException(ApiException::class);
$this->currencyLayer->from(Symbol::USD)->to(Symbol::LTL)->get();
}
}
3 changes: 1 addition & 2 deletions tests/Drivers/FixerIoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Otherguy\Currency\DriverFactory;
use Otherguy\Currency\Drivers\FixerIo;
Expand Down Expand Up @@ -64,7 +63,7 @@ public function can_get_historical_rates()
}

/** @test */
public function can_get_conversion_rates()
public function can_convert_currency_amounts()
{
// Response from https://fixer.io/documentation
$this->mockHandler->append(new Response(200, [], '{ "success": true, "query": { "from": "GBP", "to": "JPY", "amount": 25 }, "info": { "timestamp": 1519328414, "rate": 148.972231 }, "historical": "true", "date": "2018-02-22", "result": 3724.305775 }'));
Expand Down

0 comments on commit ce04e79

Please sign in to comment.