You are here

class BasicTest in Currency 8.3

@coversDefaultClass \Drupal\currency\Plugin\Currency\AmountFormatter\Basic

@group Currency

Hierarchy

Expanded class hierarchy of BasicTest

File

tests/src/Unit/Plugin/Currency/AmountFormatter/BasicTest.php, line 18

Namespace

Drupal\Tests\currency\Unit\Plugin\Currency\AmountFormatter
View source
class BasicTest extends UnitTestCase {

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

  /**
   * The string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $stringTranslation;

  /**
   * The class under test.
   *
   * @var \Drupal\currency\Plugin\Currency\AmountFormatter\Basic
   */
  protected $sut;

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

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = array(
      array(
        'currency.locale_resolver',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->localeResolver,
      ),
      array(
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ),
    );
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $configuration = array();
    $plugin_id = $this
      ->randomMachineName();
    $plugin_definition = array();
    $sut = Basic::create($container, $configuration, $plugin_id, $plugin_definition);
    $this
      ->assertInstanceOf(Basic::class, $sut);
  }

  /**
   * @covers ::formatAmount
   */
  function testFormatAmount() {
    $decimal_separator = '@';
    $grouping_separator = '%';
    $currency_locale = $this
      ->createMock(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
      ->createMock(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';
    $arguments = array(
      '@currency_code' => 'UAH',
      '@currency_sign' => '₴',
      '@amount' => '987%654@321',
    );
    $formatted_amount = $this->sut
      ->formatAmount($currency, $amount);
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $formatted_amount);
    $this
      ->assertSame('@currency_code @amount', $formatted_amount
      ->getUntranslatedString());
    $this
      ->assertSame($arguments, $formatted_amount
      ->getArguments());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BasicTest::$localeResolver protected property The locale resolver.
BasicTest::$stringTranslation protected property The string translator.
BasicTest::$sut protected property The class under test.
BasicTest::setUp public function Overrides UnitTestCase::setUp
BasicTest::testCreate function @covers ::create @covers ::__construct
BasicTest::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.