public function testIs() { $original = Currency::of('EUR'); /** @var Currency $copy */ $copy = unserialize(serialize($original)); $this->assertNotSame($original, $copy); $this->assertTrue($copy->is($original)); $this->assertTrue($copy->is('EUR')); }
/** * @depends testAddSubtractMoney * * @param MoneyBag $moneyBag */ public function testTotal(MoneyBag $moneyBag) { $exchangeRateProvider = new ConfigurableExchangeRateProvider(); $exchangeRateProvider->setExchangeRate('EUR', 'USD', '1.23456789'); $exchangeRateProvider->setExchangeRate('JPY', 'USD', '0.00987654321'); $context = new DefaultContext(RoundingMode::DOWN); $currencyConverter = new CurrencyConverter($exchangeRateProvider, $context); $this->assertMoneyIs('USD 437.57', $moneyBag->getTotal(Currency::of('USD'), $currencyConverter)); $context = new DefaultContext(RoundingMode::UP); $currencyConverter = new CurrencyConverter($exchangeRateProvider, $context); $this->assertMoneyIs('USD 437.59', $moneyBag->getTotal(Currency::of('USD'), $currencyConverter)); }
/** * @dataProvider providerConvert * * @param string $money The string representation of the base money. * @param string $currency The currency code to convert to. * @param int $roundingMode The rounding mode to use. * @param string $expectedResult The expected money's string representation, or an exception class name. */ public function testConvert($money, $currency, $roundingMode, $expectedResult) { $money = Money::parse($money); $currency = Currency::of($currency); $currencyConverter = $this->createCurrencyConverter($roundingMode); if ($this->isExceptionClass($expectedResult)) { $this->setExpectedException($expectedResult); } $actualResult = $currencyConverter->convert($money, $currency); if (!$this->isExceptionClass($expectedResult)) { $this->assertMoneyIs($expectedResult, $actualResult); } }
/** * Returns a copy of this Money converted into another currency. * * By default, the scale of the result is adjusted to represent the exact converted value. * For example, converting `USD 1.23` to `EUR` with an exchange rate of `0.91` will yield `USD 1.1193`. * The scale can be specified by providing a `MoneyContext` instance. * * @param Currency|string $currency The target currency or currency code. * @param BigNumber|number|string $exchangeRate The exchange rate to multiply by. * @param MoneyContext|null $context An optional context for scale & rounding. * * @return Money * * @throws UnknownCurrencyException If an unknown currency code is given. * @throws ArithmeticException If the exchange rate or rounding mode is invalid, or rounding is necessary. */ public function convertedTo($currency, $exchangeRate, MoneyContext $context = null) { $currency = Currency::of($currency); if ($context === null) { $context = new ExactContext(); } $amount = $this->amount->toBigRational()->multipliedBy($exchangeRate); $amount = $context->applyTo($amount, $currency, $this->amount->scale()); return new Money($amount, $currency); }