Skip to content

Commit

Permalink
Fix issue #20 : add getAvailableCurrencies method for ProviderInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
slavielle committed Dec 8, 2017
1 parent bb9880e commit bc9720c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Provider/FixerApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ public function getRate($fromCurrency, $toCurrency)

return $result['rates'][$toCurrency];
}

/**
* {@inheritdoc}
*/
public function getSupportedCurrencies()
{
$result = json_decode($this->httpClient->get(self::FIXER_API_BASEPATH)->getBody(), true);
return array_keys($result['rates']);
}
}
8 changes: 8 additions & 0 deletions src/Provider/ProviderInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace CurrencyConverter\Provider;

interface ProviderInterface
Expand All @@ -11,4 +12,11 @@ interface ProviderInterface
* @return float
*/
public function getRate($fromCurrency, $toCurrency);

/**
* Gets a list of supported currency codes
*
* @return array
*/
public function getSupportedCurrencies();
}
25 changes: 24 additions & 1 deletion tests/CurrencyConverterTest/Provider/FixerApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,28 @@ public function testGetUnavailableRate()
$this->setExpectedException(UnsupportedCurrencyException::class);
(new FixerApi($httpClient))->getRate('EUR', 'XXX');
}


public function testGetSupportedCurrencies()
{
$response = $this->getMock(ResponseInterface::class);
$response
->expects($this->any())
->method('getBody')
->will($this->returnValue(
new Stream(fopen('data://text/plain,{"base":"EUR","date":"2017-12-08","rates":{"AUD":1.562,"BGN":1.9558,"BRL":3.8435,"CAD":1.5072,"CHF":1.1704,"CNY":7.7729,"CZK":25.555,"DKK":7.4417,"GBP":0.87525,"HKD":9.1661,"HRK":7.5493,"HUF":314.5,"IDR":15910.0,"ILS":4.1343,"INR":75.678,"JPY":133.26,"KRW":1285.3,"MXN":22.22,"MYR":4.8001,"NOK":9.7665,"NZD":1.7157,"PHP":59.336,"PLN":4.202,"RON":4.6336,"RUB":69.651,"SEK":9.977,"SGD":1.5889,"THB":38.361,"TRY":4.5165,"USD":1.1742,"ZAR":16.039}}', 'r'))
));
$httpClient = $this->getMock(Client::class);
$httpClient
->expects($this->once())
->method('__call')
->with(
'get',
[FixerApi::FIXER_API_BASEPATH]
)
->will($this->returnValue($response));
$this->assertEquals(
['AUD', 'BGN', 'BRL', 'CAD', 'CHF', 'CNY', 'CZK', 'DKK', 'GBP', 'HKD', 'HRK', 'HUF', 'IDR', 'ILS', 'INR', 'JPY', 'KRW', 'MXN', 'MYR', 'NOK', 'NZD', 'PHP', 'PLN', 'RON', 'RUB', 'SEK', 'SGD', 'THB', 'TRY', 'USD', 'ZAR'],
(new FixerApi($httpClient))->getSupportedCurrencies('EUR', 'USD')
);
}
}

0 comments on commit bc9720c

Please sign in to comment.