You are here

class CalculatorTest in Commerce Core 8.2

Tests the Calculator class.

@group commerce @coversDefaultClass \Drupal\commerce_price\Calculator

Hierarchy

Expanded class hierarchy of CalculatorTest

File

modules/price/tests/src/Unit/CalculatorTest.php, line 14

Namespace

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

  /**
   * @covers ::add
   * @covers ::subtract
   * @covers ::multiply
   * @covers ::divide
   * @covers ::compare
   * @covers ::trim
   */
  public function testArithmetic() {
    $this
      ->assertEquals('11', Calculator::add('5', '6'));
    $this
      ->assertEquals('-9', Calculator::subtract('11', '20'));
    $this
      ->assertEquals('132', Calculator::multiply('11', '12'));
    $this
      ->assertEquals('12.012', Calculator::divide('132.132', '11'));
    $this
      ->assertEquals('3', Calculator::trim('3.00'));
    $this
      ->assertEquals('3.03', Calculator::trim('3.030'));
  }

  /**
   * @covers ::compare
   */
  public function testComparison() {
    $this
      ->assertEquals('0', Calculator::compare('1', '1'));
    $this
      ->assertEquals('1', Calculator::compare('2', '1'));
    $this
      ->assertEquals('-1', Calculator::compare('1', '2'));
  }

  /**
   * @covers ::ceil
   * @covers ::floor
   * @covers ::round
   */
  public function testRounding() {
    $this
      ->assertEquals('5', Calculator::ceil('4.4'));
    $this
      ->assertEquals('-4', Calculator::ceil('-4.4'));
    $this
      ->assertEquals('4', Calculator::floor('4.8'));
    $this
      ->assertEquals('-5', Calculator::floor('-4.8'));
    $rounding_data = [
      [
        '1.95583',
        2,
        PHP_ROUND_HALF_UP,
        '1.96',
      ],
      [
        '9.4',
        0,
        PHP_ROUND_HALF_UP,
        '9',
      ],
      [
        '9.6',
        0,
        PHP_ROUND_HALF_UP,
        '10',
      ],
      [
        '9.5',
        0,
        PHP_ROUND_HALF_UP,
        '10',
      ],
      [
        '9.5',
        0,
        PHP_ROUND_HALF_DOWN,
        '9',
      ],
      [
        '9.5',
        0,
        PHP_ROUND_HALF_EVEN,
        '10',
      ],
      [
        '9.5',
        0,
        PHP_ROUND_HALF_ODD,
        '9',
      ],
      [
        '8.5',
        0,
        PHP_ROUND_HALF_UP,
        '9',
      ],
      [
        '8.5',
        0,
        PHP_ROUND_HALF_DOWN,
        '8',
      ],
      [
        '8.5',
        0,
        PHP_ROUND_HALF_EVEN,
        '8',
      ],
      [
        '8.5',
        0,
        PHP_ROUND_HALF_ODD,
        '9',
      ],
      [
        '1.55',
        1,
        PHP_ROUND_HALF_UP,
        '1.6',
      ],
      [
        '1.54',
        1,
        PHP_ROUND_HALF_UP,
        '1.5',
      ],
      [
        '-1.55',
        1,
        PHP_ROUND_HALF_UP,
        '-1.6',
      ],
      [
        '-1.54',
        1,
        PHP_ROUND_HALF_UP,
        '-1.5',
      ],
      [
        '1.55',
        1,
        PHP_ROUND_HALF_DOWN,
        '1.5',
      ],
      [
        '1.54',
        1,
        PHP_ROUND_HALF_DOWN,
        '1.5',
      ],
      [
        '-1.55',
        1,
        PHP_ROUND_HALF_DOWN,
        '-1.5',
      ],
      [
        '-1.54',
        1,
        PHP_ROUND_HALF_DOWN,
        '-1.5',
      ],
      [
        '1.55',
        1,
        PHP_ROUND_HALF_EVEN,
        '1.6',
      ],
      [
        '1.54',
        1,
        PHP_ROUND_HALF_EVEN,
        '1.5',
      ],
      [
        '-1.55',
        1,
        PHP_ROUND_HALF_EVEN,
        '-1.6',
      ],
      [
        '-1.54',
        1,
        PHP_ROUND_HALF_EVEN,
        '-1.5',
      ],
      [
        '1.55',
        1,
        PHP_ROUND_HALF_ODD,
        '1.5',
      ],
      [
        '1.54',
        1,
        PHP_ROUND_HALF_ODD,
        '1.5',
      ],
      [
        '-1.55',
        1,
        PHP_ROUND_HALF_ODD,
        '-1.5',
      ],
      [
        '-1.54',
        1,
        PHP_ROUND_HALF_ODD,
        '-1.5',
      ],
    ];
    foreach ($rounding_data as $item) {
      $this
        ->assertEquals($item[3], Calculator::round($item[0], $item[1], $item[2]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CalculatorTest::testArithmetic public function @covers ::add @covers ::subtract @covers ::multiply @covers ::divide @covers ::compare @covers ::trim
CalculatorTest::testComparison public function @covers ::compare
CalculatorTest::testRounding public function @covers ::ceil @covers ::floor @covers ::round
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.
UnitTestCase::setUp protected function 340