You are here

class IntlTest in Currency 8.3

@coversDefaultClass \Drupal\currency_intl\Plugin\Currency\AmountFormatter\Intl

@group Currency Intl @requires extension intl

Hierarchy

Expanded class hierarchy of IntlTest

File

modules/currency_intl/tests/src/Unit/Plugin/Currency/AmountFormatter/IntlTest.php, line 18

Namespace

Drupal\Tests\currency_intl\Unit\Plugin\Currency\AmountFormatter
View source
class IntlTest extends UnitTestCase {

  /**
   * The formatter under test.
   *
   * @var \Drupal\currency_intl\Plugin\Currency\AmountFormatter\Intl
   */
  protected $formatter;

  /**
   * The locale resolver.
   *
   * @var \Drupal\currency\LocaleResolverInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $localeResolver;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $configuration = array();
    $plugin_id = $this
      ->randomMachineName();
    $plugin_definition = array();
    $this->localeResolver = $this
      ->createMock(LocaleResolverInterface::class);
    $this->formatter = new Intl($configuration, $plugin_id, $plugin_definition, $this->localeResolver);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $container
      ->expects($this
      ->once())
      ->method('get')
      ->with('currency.locale_resolver')
      ->willReturn($this->localeResolver);
    $configuration = array();
    $plugin_id = $this
      ->randomMachineName();
    $plugin_definition = array();
    $formatter = Intl::create($container, $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf(Intl::class, $formatter);
  }

  /**
   * @covers ::formatAmount
   */
  function testFormatAmount() {
    $locale = 'nl_NL';
    $pattern = '¤-#,##0.00¤¤';
    $decimal_separator = '@';
    $grouping_separator = '%';
    $currency_locale = $this
      ->createMock(CurrencyLocaleInterface::class);
    $currency_locale
      ->expects($this
      ->any())
      ->method('getLocale')
      ->willReturn($locale);
    $currency_locale
      ->expects($this
      ->any())
      ->method('getPattern')
      ->willReturn($pattern);
    $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);

    // ICU, the C library that PHP's Intl extension uses for formatting, is
    // known to have trouble formatting combinations of currencies and locales
    // that it does not know. In order to make sure this works, test such a
    // combination, such as the Ukrainian Hryvnia (UAH) with Dutch, Netherlands
    // (nl_NL).
    $currency_sign = '₴';
    $currency_code = 'UAH';
    $currency = $this
      ->createMock(CurrencyInterface::class);
    $currency
      ->expects($this
      ->any())
      ->method('getCurrencyCode')
      ->willReturn($currency_code);
    $currency
      ->expects($this
      ->any())
      ->method('getSign')
      ->willReturn($currency_sign);
    $results = array(
      // An amount with no decimals should be formatted without decimals and
      // decimal separator.
      '123' => '₴-123UAH',
      // An amount with three groupings should have two grouping separators. All
      // of its three decimals should be formatted, even if the currency only
      // has two.
      '1234567.890' => '₴-1%234%567@890UAH',
      // An amount with only one decimal should be formatted with only one.
      '.3' => '₴-0@3UAH',
    );
    foreach ($results as $amount => $expected) {
      $formatted = $this->formatter
        ->formatAmount($currency, $amount);
      $this
        ->assertSame($expected, $formatted);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IntlTest::$formatter protected property The formatter under test.
IntlTest::$localeResolver protected property The locale resolver.
IntlTest::setUp public function Overrides UnitTestCase::setUp
IntlTest::testCreate function @covers ::create @covers ::__construct
IntlTest::testFormatAmount function @covers ::formatAmount
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.