You are here

class RounderTest in Commerce Core 8.2

Tests the Rounder class.

@coversDefaultClass \Drupal\commerce_price\Rounder @group commerce

Hierarchy

Expanded class hierarchy of RounderTest

File

modules/price/tests/src/Unit/RounderTest.php, line 18

Namespace

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

  /**
   * The rounder.
   *
   * @var \Drupal\commerce_price\Rounder
   */
  protected $rounder;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $usd_currency = $this
      ->prophesize(CurrencyInterface::class);
    $usd_currency
      ->id()
      ->willReturn('USD');
    $usd_currency
      ->getFractionDigits()
      ->willReturn('2');
    $storage = $this
      ->prophesize(EntityStorageInterface::class);
    $storage
      ->load('USD')
      ->willReturn($usd_currency
      ->reveal());
    $storage
      ->load('EUR')
      ->willReturn(NULL);
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->getStorage('commerce_currency')
      ->willReturn($storage
      ->reveal());
    $this->rounder = new Rounder($entity_type_manager
      ->reveal());
  }

  /**
   * Tests rounding a price with an unknown currency.
   *
   * ::covers round.
   */
  public function testUnknownCurrency() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this->rounder
      ->round(new Price('10', 'EUR'));
  }

  /**
   * Tests rounding a valid price.
   *
   * ::covers round.
   */
  public function testValid() {
    $rounded_price = $this->rounder
      ->round(new Price('3.3698', 'USD'));
    $this
      ->assertEquals('3.37', $rounded_price
      ->getNumber());
    $this
      ->assertEquals('USD', $rounded_price
      ->getCurrencyCode());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
RounderTest::$rounder protected property The rounder.
RounderTest::setUp protected function Overrides UnitTestCase::setUp
RounderTest::testUnknownCurrency public function Tests rounding a price with an unknown currency.
RounderTest::testValid public function Tests rounding a valid price.
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.