You are here

class CurrencyTest in Currency 8.3

Same name in this branch
  1. 8.3 tests/src/Kernel/CurrencyTest.php \Drupal\Tests\currency\Kernel\CurrencyTest
  2. 8.3 tests/src/Unit/Entity/CurrencyTest.php \Drupal\Tests\currency\Unit\Entity\CurrencyTest
  3. 8.3 tests/src/Unit/Plugin/views/filter/CurrencyTest.php \Drupal\Tests\currency\Unit\Plugin\views\filter\CurrencyTest
  4. 8.3 tests/src/Unit/Plugin/views/field/CurrencyTest.php \Drupal\Tests\currency\Unit\Plugin\views\field\CurrencyTest

@coversDefaultClass \Drupal\currency\Entity\Currency

@group Currency

Hierarchy

Expanded class hierarchy of CurrencyTest

File

tests/src/Unit/Entity/CurrencyTest.php, line 18

Namespace

Drupal\Tests\currency\Unit\Entity
View source
class CurrencyTest extends UnitTestCase {

  /**
   * The currency amount formatter manager.
   *
   * @var \Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currencyAmountFormatterManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeManager;

  /**
   * The entity type ID.
   *
   * @var string
   */
  protected $entityTypeId;

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

  /**
   * {@inheritdoc}
   */
  function setUp() : void {
    $this->entityTypeId = $this
      ->randomMachineName();
    $this->currencyAmountFormatterManager = $this
      ->createMock(AmountFormatterManagerInterface::class);
    $this->entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $this->sut = new Currency([], $this->entityTypeId);
    $this->sut
      ->setCurrencyAmountFormatterManager($this->currencyAmountFormatterManager);
    $this->sut
      ->setEntityTypeManager($this->entityTypeManager);
  }

  /**
   * @covers ::setCurrencyAmountFormatterManager
   * @covers ::getCurrencyAmountFormatterManager
   */
  public function testGetCurrencyAmountFormatterManager() {
    $method = new \ReflectionMethod($this->sut, 'getCurrencyAmountFormatterManager');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($this->sut, $this->sut
      ->setCurrencyAmountFormatterManager($this->currencyAmountFormatterManager));
    $this
      ->assertSame($this->currencyAmountFormatterManager, $method
      ->invoke($this->sut));
  }

  /**
   * @covers ::setEntityTypeManager
   * @covers ::entityTypeManager
   */
  public function testEntityTypeManager() {
    $method = new \ReflectionMethod($this->sut, 'entityTypeManager');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($this->sut, $this->sut
      ->setEntityTypeManager($this->entityTypeManager));
    $this
      ->assertSame($this->entityTypeManager, $method
      ->invoke($this->sut));
  }

  /**
   * @covers ::getRoundingStep
   * @covers ::setRoundingStep
   */
  function testGetRoundingStep() {
    $rounding_step = mt_rand();
    $this
      ->assertSame($this->sut, $this->sut
      ->setRoundingStep($rounding_step));
    $this
      ->assertSame($rounding_step, $this->sut
      ->getRoundingStep());
  }

  /**
   * @covers ::getRoundingStep
   */
  function testGetRoundingStepBySubunits() {
    $subunits = 5;
    $rounding_step = '0.200000';
    $this->sut
      ->setSubunits($subunits);
    $this
      ->assertSame($rounding_step, $this->sut
      ->getRoundingStep());
  }

  /**
   * @covers ::getRoundingStep
   */
  function testGetRoundingStepUnavailable() {
    $this
      ->assertNull($this->sut
      ->getRoundingStep());
  }

  /**
   * @covers ::formatAmount
   * @covers ::getCurrencyAmountFormatterManager
   *
   * @depends testGetRoundingStep
   *
   * @dataProvider providerTestFormatAmount
   */
  function testFormatAmount($expected, $amount, $amount_with_currency_precision_applied) {
    $amount_formatter = $this
      ->createMock(AmountFormatterInterface::class);
    $amount_formatter
      ->expects($this
      ->atLeastOnce())
      ->method('formatAmount')
      ->with($this->sut, $amount_with_currency_precision_applied)
      ->willReturn($expected);
    $this->currencyAmountFormatterManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefaultPlugin')
      ->willReturn($amount_formatter);
    $this->sut
      ->setCurrencyCode('BLA');
    $this->sut
      ->setSubunits(100);
    $this
      ->assertSame($expected, $this->sut
      ->formatAmount($amount, $amount !== $amount_with_currency_precision_applied));
  }

  /**
   * Provides data to self::testFormatAmount().
   */
  public function providerTestFormatAmount() {
    return [
      [
        'BLA 12,345.68',
        '12345.6789',
        '12345.68',
      ],
      [
        'BLA 12,345.6789',
        '12345.6789',
        '12345.6789',
      ],
    ];
  }

  /**
   * @covers ::getDecimals
   */
  function testGetDecimals() {
    foreach ([
      1,
      2,
      3,
    ] as $decimals) {
      $this->sut
        ->setSubunits(pow(10, $decimals));
      $this
        ->assertSame($decimals, $this->sut
        ->getDecimals());
    }
  }

  /**
   * @covers ::isObsolete
   */
  function testIsObsolete() {

    // A currency without usage data.
    $this
      ->assertFalse($this->sut
      ->isObsolete());

    // A currency that is no longer being used.
    $usage = new Usage();
    $usage
      ->setStart('1813-01-01')
      ->setEnd('2002-02-28');
    $this->sut
      ->setUsages([
      $usage,
    ]);
    $this
      ->assertTrue($this->sut
      ->isObsolete());

    // A currency that will become obsolete next year.
    $usage = new Usage();
    $usage
      ->setStart('1813-01-01')
      ->setEnd(date('o') + 1 . '-02-28');
    $this->sut
      ->setUsages([
      $usage,
    ]);
    $this
      ->assertFalse($this->sut
      ->isObsolete());
  }

  /**
   * @covers ::getAlternativeSigns
   * @covers ::setAlternativeSigns
   */
  function testGetAlternativeSigns() {
    $alternative_signs = [
      'A',
      'B',
    ];
    $this
      ->assertSame($this->sut, $this->sut
      ->setAlternativeSigns($alternative_signs));
    $this
      ->assertSame($alternative_signs, $this->sut
      ->getAlternativeSigns());
  }

  /**
   * @covers ::id
   * @covers ::setCurrencyCode
   */
  function testId() {
    $currency_code = $this
      ->randomMachineName(3);
    $this
      ->assertSame($this->sut, $this->sut
      ->setCurrencyCode($currency_code));
    $this
      ->assertSame($currency_code, $this->sut
      ->id());
  }

  /**
   * @covers ::getCurrencyCode
   * @covers ::setCurrencyCode
   */
  function testGetCurrencyCode() {
    $currency_code = $this
      ->randomMachineName(3);
    $this
      ->assertSame($this->sut, $this->sut
      ->setCurrencyCode($currency_code));
    $this
      ->assertSame($currency_code, $this->sut
      ->getCurrencyCode());
  }

  /**
   * @covers ::getCurrencyNumber
   * @covers ::setCurrencyNumber
   */
  function testGetCurrencyNumber() {
    $currency_number = '000';
    $this
      ->assertSame($this->sut, $this->sut
      ->setCurrencyNumber($currency_number));
    $this
      ->assertSame($currency_number, $this->sut
      ->getCurrencyNumber());
  }

  /**
   * @covers ::label
   * @covers ::setLabel
   */
  function testLabel() {
    $entity_type = $this
      ->createMock(EntityTypeInterface::class);
    $entity_type
      ->expects($this
      ->atLeastOnce())
      ->method('getKey')
      ->with('label')
      ->willReturn('label');
    $this->entityTypeManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinition')
      ->with($this->entityTypeId)
      ->willReturn($entity_type);
    $label = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setLabel($label));
    $this
      ->assertSame($label, $this->sut
      ->label());
  }

  /**
   * @covers ::getSign
   * @covers ::setSign
   */
  function testGetSign() {
    $sign = $this
      ->randomMachineName(1);
    $this
      ->assertSame($this->sut, $this->sut
      ->setSign($sign));
    $this
      ->assertSame($sign, $this->sut
      ->getSign());
  }

  /**
   * @covers ::setSubunits
   * @covers ::getSubunits
   */
  function testGetSubunits() {
    $subunits = 73;
    $this
      ->assertSame($this->sut, $this->sut
      ->setSubunits($subunits));
    $this
      ->assertSame($subunits, $this->sut
      ->getSubunits());
  }

  /**
   * @covers ::setUsages
   * @covers ::getUsages
   */
  function testGetUsage() {
    $usage = new Usage();
    $usage
      ->setStart('1813-01-01')
      ->setEnd(date('o') + 1 . '-02-28');
    $this
      ->assertSame($this->sut, $this->sut
      ->setUsages([
      $usage,
    ]));
    $this
      ->assertSame([
      $usage,
    ], $this->sut
      ->getUsages());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrencyTest::$currencyAmountFormatterManager protected property The currency amount formatter manager.
CurrencyTest::$entityTypeId protected property The entity type ID.
CurrencyTest::$entityTypeManager protected property The entity type manager.
CurrencyTest::$sut protected property The class under test.
CurrencyTest::providerTestFormatAmount public function Provides data to self::testFormatAmount().
CurrencyTest::setUp function Overrides UnitTestCase::setUp
CurrencyTest::testEntityTypeManager public function @covers ::setEntityTypeManager @covers ::entityTypeManager
CurrencyTest::testFormatAmount function @covers ::formatAmount @covers ::getCurrencyAmountFormatterManager
CurrencyTest::testGetAlternativeSigns function @covers ::getAlternativeSigns @covers ::setAlternativeSigns
CurrencyTest::testGetCurrencyAmountFormatterManager public function @covers ::setCurrencyAmountFormatterManager @covers ::getCurrencyAmountFormatterManager
CurrencyTest::testGetCurrencyCode function @covers ::getCurrencyCode @covers ::setCurrencyCode
CurrencyTest::testGetCurrencyNumber function @covers ::getCurrencyNumber @covers ::setCurrencyNumber
CurrencyTest::testGetDecimals function @covers ::getDecimals
CurrencyTest::testGetRoundingStep function @covers ::getRoundingStep @covers ::setRoundingStep
CurrencyTest::testGetRoundingStepBySubunits function @covers ::getRoundingStep
CurrencyTest::testGetRoundingStepUnavailable function @covers ::getRoundingStep
CurrencyTest::testGetSign function @covers ::getSign @covers ::setSign
CurrencyTest::testGetSubunits function @covers ::setSubunits @covers ::getSubunits
CurrencyTest::testGetUsage function @covers ::setUsages @covers ::getUsages
CurrencyTest::testId function @covers ::id @covers ::setCurrencyCode
CurrencyTest::testIsObsolete function @covers ::isObsolete
CurrencyTest::testLabel function @covers ::label @covers ::setLabel
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.