Skip to content

Commit

Permalink
Merge cd42f9b into a0f0aa4
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrinksmeier committed Jun 4, 2018
2 parents a0f0aa4 + cd42f9b commit c3b3c1c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
25 changes: 22 additions & 3 deletions src/Provider/FixerApi.php
Expand Up @@ -16,20 +16,37 @@ class FixerApi implements ProviderInterface
*
* @var string
*/
const FIXER_API_BASEPATH = 'https://api.fixer.io/latest';
const FIXER_API_BASEPATH = 'data.fixer.io/api/latest';

/**
* The fixer access key
*
* @var string
*/
protected $accessKey;

/**
* @var Client
*/
protected $httpClient;

/**
* Flag to switch http(s) usage. required as fixer.io enables https api endpoints for paid accounts only
* @var bool
*/
protected $useHttps = false;

/**
* FixerApi constructor.
* @param string $accessKey
* @param Client|null $httpClient
* @param bool $useHttps defaults to false
*/
public function __construct(Client $httpClient = null)
public function __construct($accessKey, Client $httpClient = null, $useHttps = false)
{
$this->accessKey = $accessKey;
$this->httpClient = $httpClient ?: new Client();
$this->useHttps = (bool)$useHttps;
}

/**
Expand All @@ -38,7 +55,9 @@ public function __construct(Client $httpClient = null)
public function getRate($fromCurrency, $toCurrency)
{
$path = sprintf(
self::FIXER_API_BASEPATH . '?symbols=%s&base=%s',
'%s' . self::FIXER_API_BASEPATH . '?access_key=%s&symbols=%s&base=%s',
($this->useHttps) ? 'https://' : 'http://',
$this->accessKey,
$toCurrency,
$fromCurrency
);
Expand Down
32 changes: 28 additions & 4 deletions tests/CurrencyConverterTest/Provider/FixerApiTest.php
Expand Up @@ -24,12 +24,12 @@ public function testGetRate()
->method('__call')
->with(
'get',
[FixerApi::FIXER_API_BASEPATH . '?symbols=USD&base=EUR']
['http://' . FixerApi::FIXER_API_BASEPATH . '?access_key=SOME-ACCESS-KEY&symbols=USD&base=EUR']
)
->will($this->returnValue($response));
$this->assertEquals(
1.1645,
(new FixerApi($httpClient))->getRate('EUR', 'USD')
(new FixerApi('SOME-ACCESS-KEY', $httpClient))->getRate('EUR', 'USD')
);
}

Expand All @@ -48,12 +48,36 @@ public function testGetUnavailableRate()
->method('__call')
->with(
'get',
[FixerApi::FIXER_API_BASEPATH . '?symbols=XXX&base=EUR']
['http://' . FixerApi::FIXER_API_BASEPATH . '?access_key=SOME-ACCESS-KEY&symbols=XXX&base=EUR']
)
->will($this->returnValue($response));

$this->setExpectedException(UnsupportedCurrencyException::class);
(new FixerApi($httpClient))->getRate('EUR', 'XXX');
(new FixerApi('SOME-ACCESS-KEY', $httpClient))->getRate('EUR', 'XXX');
}

public function testHttpsUsage()
{
$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',
['https://' . FixerApi::FIXER_API_BASEPATH . '?access_key=SOME-ACCESS-KEY&symbols=USD&base=EUR']
)
->will($this->returnValue($response));
$this->assertEquals(
1.1645,
(new FixerApi('SOME-ACCESS-KEY', $httpClient, true))->getRate('EUR', 'USD')
);
}

}

0 comments on commit c3b3c1c

Please sign in to comment.