Skip to content

Commit

Permalink
Merge pull request #53 from philperusse/master
Browse files Browse the repository at this point in the history
Add an enterprise option for Fixer.io
  • Loading branch information
florianv authored May 14, 2018
2 parents 9148c53 + 74a21a7 commit b0d3a83
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 21 deletions.
49 changes: 37 additions & 12 deletions src/Service/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ class Fixer extends HistoricalService
{
const ACCESS_KEY_OPTION = 'access_key';

const LATEST_URL = 'http://data.fixer.io/api/latest?base=%s&access_key=%s';
const ENTERPRISE_LATEST_URL = 'http://data.fixer.io/api/latest?base=%s&access_key=%s';

const HISTORICAL_URL = 'http://data.fixer.io/api/%s?base=%s&access_key=%s';
const ENTERPRISE_HISTORICAL_URL = 'http://data.fixer.io/api/%s?base=%s&access_key=%s';

const FREE_LATEST_URL = 'http://data.fixer.io/api/latest?access_key=%s';

const FREE_HISTORICAL_URL = 'http://data.fixer.io/api/%s?access_key=%s';

/**
* {@inheritdoc}
Expand All @@ -38,8 +42,18 @@ protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery)
{
$currencyPair = $exchangeQuery->getCurrencyPair();

$accessKey = $this->options[self::ACCESS_KEY_OPTION];
$url = sprintf(self::LATEST_URL, $currencyPair->getBaseCurrency(), $accessKey);
if ($this->options['enterprise']) {
$url = sprintf(
self::ENTERPRISE_LATEST_URL,
$currencyPair->getBaseCurrency(),
$this->options[self::ACCESS_KEY_OPTION]
);
} else {
$url = sprintf(
self::FREE_LATEST_URL,
$this->options[self::ACCESS_KEY_OPTION]
);
}

return $this->createRate($url, $currencyPair);
}
Expand All @@ -52,6 +66,10 @@ public function processOptions(array &$options)
if (!isset($options[self::ACCESS_KEY_OPTION])) {
throw new \InvalidArgumentException('The "access_key" option must be provided to use fixer.io');
}

if (!isset($options['enterprise'])) {
$options['enterprise'] = false;
}
}

/**
Expand All @@ -61,13 +79,20 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan
{
$currencyPair = $exchangeQuery->getCurrencyPair();

$accessKey = $this->options[self::ACCESS_KEY_OPTION];
$url = sprintf(
self::HISTORICAL_URL,
$exchangeQuery->getDate()->format('Y-m-d'),
$currencyPair->getBaseCurrency(),
$accessKey
);
if ($this->options['enterprise']) {
$url = sprintf(
self::ENTERPRISE_HISTORICAL_URL,
$exchangeQuery->getDate()->format('Y-m-d'),
$exchangeQuery->getCurrencyPair()->getBaseCurrency(),
$this->options[self::ACCESS_KEY_OPTION]
);
} else {
$url = sprintf(
self::FREE_HISTORICAL_URL,
$exchangeQuery->getDate()->format('Y-m-d'),
$this->options[self::ACCESS_KEY_OPTION]
);
}

return $this->createRate($url, $currencyPair);
}
Expand All @@ -77,7 +102,7 @@ protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchan
*/
public function supportQuery(ExchangeRateQuery $exchangeQuery)
{
return true;
return $this->options['enterprise'] || 'EUR' === $exchangeQuery->getCurrencyPair()->getBaseCurrency();
}

/**
Expand Down
65 changes: 56 additions & 9 deletions tests/Tests/Service/FixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,28 @@ class FixerTest extends ServiceTestCase
/**
* @test
*/
public function it_supports_all_queries()
public function it_does_not_support_all_queries()
{
$service = new Fixer($this->getMock('Http\Client\HttpClient'), null, ['access_key' => 'x']);
$this->assertFalse($service->supportQuery(new ExchangeRateQuery(CurrencyPair::createFromString('USD/EUR'))));
}

/**
* @test
*/
public function it_supports_eur_base_normal_mode()
{
$service = new Fixer($this->getMock('Http\Client\HttpClient'), null, ['access_key' => 'x']);
$this->assertTrue($service->supportQuery(new ExchangeRateQuery(CurrencyPair::createFromString('EUR/CAD'))));
}

$this->assertTrue($service->supportQuery(new ExchangeRateQuery(CurrencyPair::createFromString('USD/EUR'))));
$this->assertTrue($service->supportQuery(new HistoricalExchangeRateQuery(CurrencyPair::createFromString('EUR/USD'), new \DateTime())));
/**
* @test
*/
public function it_does_not_support_other_than_eur_base_in_normal_mode()
{
$service = new Fixer($this->getMock('Http\Client\HttpClient'), null, ['access_key' => 'x']);
$this->assertFalse($service->supportQuery(new ExchangeRateQuery(CurrencyPair::createFromString('USD/CAD'))));
}

/**
Expand All @@ -35,7 +51,7 @@ public function it_supports_all_queries()
*/
public function it_throws_an_exception_with_error_response()
{
$uri = 'http://data.fixer.io/api/latest?base=USD&access_key=x';
$uri = 'http://data.fixer.io/api/latest?access_key=x';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/Fixer/error.json');

$service = new Fixer($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x']);
Expand All @@ -45,9 +61,9 @@ public function it_throws_an_exception_with_error_response()
/**
* @test
*/
public function it_fetches_a_rate()
public function it_fetches_a_rate_normal_mode()
{
$uri = 'http://data.fixer.io/api/latest?base=EUR&access_key=x';
$uri = 'http://data.fixer.io/api/latest?access_key=x';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/Fixer/latest.json');

$service = new Fixer($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x']);
Expand All @@ -60,14 +76,45 @@ public function it_fetches_a_rate()
/**
* @test
*/
public function it_fetches_a_historical_rate()
public function it_fetches_a_rate_enterprise_mode()
{
$uri = 'http://data.fixer.io/api/2000-01-03?base=USD&access_key=x';
$uri = 'http://data.fixer.io/api/latest?base=EUR&access_key=x';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/Fixer/latest.json');

$service = new Fixer($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x', 'enterprise' => true]);
$rate = $service->getExchangeRate(new ExchangeRateQuery(CurrencyPair::createFromString('EUR/CHF')));

$this->assertEquals('1.0933', $rate->getValue());
$this->assertEquals(new \DateTime('2016-08-26'), $rate->getDate());
}

/**
* @test
*/
public function it_fetches_a_historical_rate_normal_mode()
{
$uri = 'http://data.fixer.io/api/2000-01-03?access_key=x';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/Fixer/historical.json');
$date = new \DateTime('2000-01-03');

$service = new Fixer($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x']);
$rate = $service->getExchangeRate(new HistoricalExchangeRateQuery(CurrencyPair::createFromString('USD/AUD'), $date));
$rate = $service->getExchangeRate(new HistoricalExchangeRateQuery(CurrencyPair::createFromString('EUR/AUD'), $date));

$this->assertEquals('1.5209', $rate->getValue());
$this->assertEquals($date, $rate->getDate());
}

/**
* @test
*/
public function it_fetches_a_historical_rate_enterprise_mode()
{
$uri = 'http://data.fixer.io/api/2000-01-03?base=EUR&access_key=x';
$content = file_get_contents(__DIR__.'/../../Fixtures/Service/Fixer/historical.json');
$date = new \DateTime('2000-01-03');

$service = new Fixer($this->getHttpAdapterMock($uri, $content), null, ['access_key' => 'x', 'enterprise' => true]);
$rate = $service->getExchangeRate(new HistoricalExchangeRateQuery(CurrencyPair::createFromString('EUR/AUD'), $date));

$this->assertEquals('1.5209', $rate->getValue());
$this->assertEquals($date, $rate->getDate());
Expand Down

0 comments on commit b0d3a83

Please sign in to comment.