You are here

class MinorUnitsConverterTest in Commerce Core 8.2

Tests the MinorUnitsConverter class.

@coversDefaultClass \Drupal\commerce_price\MinorUnitsConverter @group commerce

Hierarchy

Expanded class hierarchy of MinorUnitsConverterTest

File

modules/price/tests/src/Unit/MinorUnitsConverterTest.php, line 19

Namespace

Drupal\Tests\commerce_price\Unit
View source
class MinorUnitsConverterTest extends UnitTestCase {

  /**
   * The minor units converter.
   *
   * @var \Drupal\commerce_price\MinorUnitsConverterInterface
   */
  protected $minorUnitsConverter;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $storage = $this
      ->prophesize(EntityStorageInterface::class);
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->getStorage('commerce_currency')
      ->willReturn($storage
      ->reveal());
    $this->minorUnitsConverter = new MinorUnitsConverter(new CurrencyRepository($entity_type_manager
      ->reveal()));
  }

  /**
   * Tests converting a price with an unknown currency.
   *
   * @covers ::toMinorUnits
   */
  public function testUnknownCurrency() {
    $this
      ->expectException(UnknownCurrencyException::class);
    $this->minorUnitsConverter
      ->toMinorUnits(new Price('10', 'XYZ'));
  }

  /**
   * Tests if price can be converted to minor unit and then back to decimal.
   *
   * @dataProvider currencyConversionData
   */
  public function testConversion(Price $price, $expected_amount) {
    $amount = $this->minorUnitsConverter
      ->toMinorUnits($price);
    $this
      ->assertEquals($expected_amount, $amount);
    $this
      ->assertTrue($this->minorUnitsConverter
      ->fromMinorUnits($amount, $price
      ->getCurrencyCode())
      ->equals($price));
  }

  /**
   * Data provider for ::testConversion.
   *
   * @return array
   *   The test data.
   */
  public function currencyConversionData() {
    return [
      [
        new Price(10, 'EUR'),
        1000,
      ],
      [
        new Price(1.23, 'USD'),
        123,
      ],
      [
        new Price(0.99, 'NOK'),
        99,
      ],
      [
        new Price(99, 'JPY'),
        99,
      ],
      [
        new Price(99, 'KWD'),
        99000,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MinorUnitsConverterTest::$minorUnitsConverter protected property The minor units converter.
MinorUnitsConverterTest::currencyConversionData public function Data provider for ::testConversion.
MinorUnitsConverterTest::setUp protected function Overrides UnitTestCase::setUp
MinorUnitsConverterTest::testConversion public function Tests if price can be converted to minor unit and then back to decimal.
MinorUnitsConverterTest::testUnknownCurrency public function Tests converting a price with an unknown currency.
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.