From bc9720cb51ab49f7455288074b7d83adf4ac7ff9 Mon Sep 17 00:00:00 2001 From: slavielle Date: Fri, 8 Dec 2017 22:37:39 +0100 Subject: [PATCH] Fix issue #20 : add getAvailableCurrencies method for ProviderInterface --- src/Provider/FixerApi.php | 9 +++++++ src/Provider/ProviderInterface.php | 8 ++++++ .../Provider/FixerApiTest.php | 25 ++++++++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Provider/FixerApi.php b/src/Provider/FixerApi.php index ef3b8a3..2faa2b2 100644 --- a/src/Provider/FixerApi.php +++ b/src/Provider/FixerApi.php @@ -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']); + } } diff --git a/src/Provider/ProviderInterface.php b/src/Provider/ProviderInterface.php index 55a1478..de0b3d1 100644 --- a/src/Provider/ProviderInterface.php +++ b/src/Provider/ProviderInterface.php @@ -1,4 +1,5 @@ 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') + ); + } }