/** * Tests the exchange cases when the we receive an invalid currency. * * @param string $convertFromIso The ISO we are converting from. * @param string $convertToIso The ISO we are converting to. * * @dataProvider nonExistentCurrencyConversionsProvider */ public function testNonPresentCurrencyConversions($convertFromIso, $convertToIso) { $currencyFrom = $this->createCurrency($convertFromIso); $currencyTo = $this->createCurrency($convertToIso); $exception = 'Elcodi\\Component\\Currency\\Exception\\CurrencyNotConvertibleException'; $this->setExpectedException($exception); self::$exchangeRateCalculator->calculateExchangeRate($currencyFrom, $currencyTo); }
/** * Convert amount between two currencies * * If are the same currency, return same amount * * If is impossible to convert between them, throw Exception * * @param CurrencyInterface $currencyFrom Currency where to convert from * @param CurrencyInterface $currencyTo Currency where to convert to * @param integer $amount Amount to convert * * @return MoneyInterface Money converted * * @throws CurrencyNotConvertibleException Currencies cannot be converted */ private function convertCurrency(CurrencyInterface $currencyFrom, CurrencyInterface $currencyTo, $amount) { if ($currencyFrom->getIso() == $currencyTo->getIso()) { return Money::create($amount, $currencyFrom); } $exchangeRate = $this->exchangeRateCalculator->calculateExchangeRate($currencyFrom, $currencyTo); return Money::create($amount * $exchangeRate, $currencyTo); }