diff --git a/src/ExchangeRateProvider/BaseCurrencyProvider.php b/src/ExchangeRateProvider/BaseCurrencyProvider.php index 6051ffa..8f37fdb 100644 --- a/src/ExchangeRateProvider/BaseCurrencyProvider.php +++ b/src/ExchangeRateProvider/BaseCurrencyProvider.php @@ -4,6 +4,7 @@ namespace Brick\Money\ExchangeRateProvider; +use Brick\Math\BigNumber; use Brick\Money\ExchangeRateProvider; use Brick\Math\BigRational; @@ -46,10 +47,10 @@ public function __construct(ExchangeRateProvider $provider, string $baseCurrency /** * {@inheritdoc} */ - public function getExchangeRate(string $sourceCurrencyCode, string $targetCurrencyCode) + public function getExchangeRate(string $sourceCurrencyCode, string $targetCurrencyCode): BigNumber { if ($sourceCurrencyCode === $this->baseCurrencyCode) { - return $this->provider->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode); + return BigNumber::of($this->provider->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode)); } if ($targetCurrencyCode === $this->baseCurrencyCode) { diff --git a/tests/ExchangeRateProvider/BaseCurrencyProviderTest.php b/tests/ExchangeRateProvider/BaseCurrencyProviderTest.php index 0b27167..c866311 100644 --- a/tests/ExchangeRateProvider/BaseCurrencyProviderTest.php +++ b/tests/ExchangeRateProvider/BaseCurrencyProviderTest.php @@ -4,6 +4,7 @@ namespace Brick\Money\Tests\ExchangeRateProvider; +use Brick\Math\BigNumber; use Brick\Money\ExchangeRateProvider; use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider; use Brick\Money\ExchangeRateProvider\ConfigurableProvider; @@ -38,7 +39,7 @@ private function getExchangeRateProvider() : ExchangeRateProvider public function testGetExchangeRate(string $sourceCurrencyCode, string $targetCurrencyCode, string $exchangeRate) : void { $rate = $this->getExchangeRateProvider()->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode); - self::assertSame($exchangeRate, (string) BigRational::of($rate)->toScale(6, RoundingMode::DOWN)); + self::assertSame($exchangeRate, (string) $rate->toScale(6, RoundingMode::DOWN)); } public function providerGetExchangeRate() : array @@ -60,4 +61,25 @@ public function providerGetExchangeRate() : array ['CAD', 'GBP', '0.727272'], ]; } + + /** + * @dataProvider providerReturnBigNumber + * + * @param BigNumber|float|int|string $rate + */ + public function testReturnBigNumber($rate) : void + { + $configurableProvider = new ConfigurableProvider(); + $configurableProvider->setExchangeRate('USD', 'EUR', $rate); + $baseProvider = new BaseCurrencyProvider($configurableProvider, 'USD'); + + $rate = $baseProvider->getExchangeRate('USD', 'EUR'); + + $this->assertInstanceOf(BigNumber::class, $rate); + } + + public function providerReturnBigNumber() : array + { + return [[1], [1.1], ['1.0'], [BigNumber::of('1')]]; + } }