예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function formatAmount(CurrencyInterface $currency, $amount, $language_type = LanguageInterface::TYPE_CONTENT)
 {
     // Compute the number of decimals, so we can format all of them and no less
     // or more.
     $decimals = strlen($amount) - strpos($amount, '.') - 1;
     $currency_locale = $this->localeDelegator->resolveCurrencyLocale();
     $formatted_amount = number_format($amount, $decimals, $currency_locale->getDecimalSeparator(), $currency_locale->getGroupingSeparator());
     $arguments = array('@currency_code' => $currency->getCurrencyCode(), '@currency_sign' => $currency->getSign(), '@amount' => $formatted_amount);
     return $this->t('@currency_code @amount', $arguments);
 }
예제 #2
0
 /**
  * @covers ::formatAmount
  */
 function testFormatAmount()
 {
     $decimal_separator = '@';
     $grouping_separator = '%';
     $currency_locale = $this->getMock(CurrencyLocaleInterface::class);
     $currency_locale->expects($this->any())->method('getDecimalSeparator')->willReturn($decimal_separator);
     $currency_locale->expects($this->any())->method('getGroupingSeparator')->willReturn($grouping_separator);
     $this->localeResolver->expects($this->any())->method('resolveCurrencyLocale')->willReturn($currency_locale);
     // The formatter must not alter the decimals.
     $amount = '987654.321';
     $currency_sign = '₴';
     $currency_code = 'UAH';
     $currency_decimals = 2;
     $currency = $this->getMock(CurrencyInterface::class);
     $currency->expects($this->any())->method('getCurrencyCode')->willReturn($currency_code);
     $currency->expects($this->any())->method('getDecimals')->willReturn($currency_decimals);
     $currency->expects($this->any())->method('getSign')->willReturn($currency_sign);
     $translation = 'UAH 987%654@321';
     $formatted_amount = $this->sut->formatAmount($currency, $amount);
     $this->logicalOr(new \PHPUnit_Framework_Constraint_IsType('string', $formatted_amount), new \PHPUnit_Framework_Constraint_IsInstanceOf(TranslatableMarkup::class, $formatted_amount));
     $this->assertSame($translation, (string) $formatted_amount);
 }