コード例 #1
0
 /**
  * @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());
 }
コード例 #2
0
 /**
  * {@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);
 }
コード例 #3
0
ファイル: CurrencyTest.php プロジェクト: brick/money
 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'));
 }
コード例 #4
0
 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);
 }
コード例 #5
0
ファイル: ISOCurrencyProvider.php プロジェクト: brick/money
 /**
  * {@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;
 }
コード例 #6
0
ファイル: MoneyBagTest.php プロジェクト: brick/money
 /**
  * @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));
 }
コード例 #7
0
ファイル: CurrencyConverterTest.php プロジェクト: brick/money
 /**
  * @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);
     }
 }
コード例 #8
0
ファイル: DefaultContext.php プロジェクト: brick/money
 /**
  * {@inheritdoc}
  */
 public function applyTo(BigNumber $amount, Currency $currency, $currentScale)
 {
     return $amount->toScale($currency->getDefaultFractionDigits(), $this->roundingMode);
 }
コード例 #9
0
ファイル: MoneyBag.php プロジェクト: brick/money
 /**
  * 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);
 }
コード例 #10
0
ファイル: MoneyTest.php プロジェクト: brick/money
 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);
 }
コード例 #11
0
 /**
  * @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()));
 }
コード例 #12
0
 /**
  * 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;
 }
コード例 #13
0
ファイル: Money.php プロジェクト: brick/money
 /**
  * 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);
 }
コード例 #14
0
ファイル: Money.php プロジェクト: brick/money
 /**
  * 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);
 }