/**
  * @depends testRemoveCustomCurrency
  */
 public function testAttemptToOverrideISOCurrency()
 {
     $euro = Currency::create('EUR', 0, 'Another Euro', 6);
     DefaultCurrencyProvider::getInstance()->addCurrency($euro);
     $isoCurrencies = ISOCurrencyProvider::getInstance()->getAvailableCurrencies();
     $this->assertCurrencyProviderContains($isoCurrencies, DefaultCurrencyProvider::getInstance());
 }
 /**
  * {@inheritdoc}
  */
 public static function setUpBeforeClass()
 {
     self::$fooCurrency = Currency::create('FOO', 1, 'Foo currency', 0);
     self::$barCurrency = Currency::create('BAR', 2, 'Bar currency', 2);
     self::$bazCurrency = Currency::create('BAZ', 3, 'Baz currency', 3);
     self::$competingFooCurrency = Currency::create('FOO', 999, 'A competing foo currency', 2);
 }
Exemple #3
0
 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'));
 }
 public function testGetAvailableCurrencies()
 {
     $providerChain = $this->createCurrencyProviderChain();
     // Add a different GBP instance, that comes after the first one.
     // The first instance available in the chain takes precedence.
     $provider = new ConfigurableCurrencyProvider();
     $provider->addCurrency(Currency::create('GBP', 999, 'A competing GBP instance', 6));
     $providerChain->addCurrencyProvider($provider);
     $isoCurrencyProvider = ISOCurrencyProvider::getInstance();
     $this->assertCurrencyProviderContains(['EUR' => $isoCurrencyProvider->getCurrency('EUR'), 'GBP' => $isoCurrencyProvider->getCurrency('GBP'), 'USD' => $isoCurrencyProvider->getCurrency('USD'), 'CAD' => $isoCurrencyProvider->getCurrency('CAD')], $providerChain);
 }
Exemple #5
0
 /**
  * {@inheritdoc}
  */
 public function getAvailableCurrencies()
 {
     if ($this->isPartial) {
         foreach ($this->currencyData as $currencyCode => $data) {
             if (!isset($this->currencies[$currencyCode])) {
                 $this->currencies[$currencyCode] = Currency::create(...$data);
             }
         }
         $this->isPartial = false;
     }
     return $this->currencies;
 }
Exemple #6
0
 /**
  * @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);
     }
 }
Exemple #8
0
 /**
  * {@inheritdoc}
  */
 public function applyTo(BigNumber $amount, Currency $currency, $currentScale)
 {
     return $amount->toScale($currency->getDefaultFractionDigits(), $this->roundingMode);
 }
Exemple #9
0
 /**
  * Returns the money in the given currency contained in the bag.
  *
  * If no money is present for the given currency, a zero-value money with the default scale
  * for the given currency will be returned.
  *
  * @param Currency $currency
  *
  * @return Money
  */
 public function get(Currency $currency)
 {
     $currencyCode = $currency->getCode();
     return isset($this->monies[$currencyCode]) ? $this->monies[$currencyCode] : Money::zero($currency);
 }
Exemple #10
0
 public function testParseWithCustomCurrency()
 {
     $bitCoin = Currency::create('BTC', 0, 'BitCoin', 8);
     DefaultCurrencyProvider::getInstance()->addCurrency($bitCoin);
     try {
         $money = Money::parse('BTC 1.23456789');
     } finally {
         DefaultCurrencyProvider::getInstance()->removeCurrency($bitCoin);
     }
     $this->assertMoneyEquals('1.23456789', 'BTC', $money);
 }
 /**
  * @param Currency $expected
  * @param Currency $actual
  *
  * @return CurrencyMismatchException
  */
 public static function currencyMismatch(Currency $expected, Currency $actual)
 {
     return new self(sprintf('Currency mismatch: expected %s, got %s', $expected->getCode(), $actual->getCode()));
 }
 /**
  * Removes a currency from this currency provider.
  *
  * If no currency with this code is registered, this method does nothing.
  *
  * @param Currency $currency The currency to remove.
  *
  * @return ConfigurableCurrencyProvider This instance, for chaining.
  */
 public function removeCurrency(Currency $currency)
 {
     unset($this->currencies[$currency->getCode()]);
     return $this;
 }
Exemple #13
0
 /**
  * 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);
 }
Exemple #14
0
 /**
  * Returns a copy of this Money with this value, and the default number of fraction digits of the currency in use.
  *
  * @param int $roudingMode The rounding mode to apply, if necessary.
  *
  * @return Money
  */
 public function withDefaultFractionDigits($roudingMode = RoundingMode::UNNECESSARY)
 {
     return $this->withFractionDigits($this->currency->getDefaultFractionDigits(), $roudingMode);
 }