You are here

class PriceTest in Commerce Core 8.2

Tests the Price class.

@coversDefaultClass \Drupal\commerce_price\Price @group commerce

Hierarchy

Expanded class hierarchy of PriceTest

File

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

Namespace

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

  /**
   * The price.
   *
   * @var \Drupal\commerce_price\Price
   */
  protected $price;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->price = new Price('10', 'USD');
  }

  /**
   * Tests creating a price from an invalid array.
   *
   * ::covers __construct.
   */
  public function testCreateFromInvalidArray() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('Price::fromArray() called with a malformed array.');
    $price = Price::fromArray([]);
  }

  /**
   * Tests creating a price from a valid array.
   *
   * ::covers __construct.
   */
  public function testCreateFromValidArray() {
    $price = Price::fromArray([
      'number' => '10',
      'currency_code' => 'USD',
    ]);
    $this
      ->assertEquals('10', $price
      ->getNumber());
    $this
      ->assertEquals('USD', $price
      ->getCurrencyCode());
    $this
      ->assertEquals('10 USD', $price
      ->__toString());
    $this
      ->assertEquals([
      'number' => '10',
      'currency_code' => 'USD',
    ], $price
      ->toArray());
  }

  /**
   * Tests creating a price with an invalid number.
   *
   * ::covers __construct.
   */
  public function testInvalidNumber() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $price = new Price('INVALID', 'USD');
  }

  /**
   * Tests creating a price with an invalid currency code.
   *
   * ::covers __construct.
   */
  public function testInvalidCurrencyCode() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('Invalid currency code "TEST".');
    $price = new Price('10', 'TEST');
  }

  /**
   * Tests the methods for getting the number/currency code in various formats.
   *
   * ::covers getNumber
   * ::covers getCurrencyCode
   * ::covers __toString
   * ::covers toArray.
   */
  public function testGetters() {
    $this
      ->assertEquals('10', $this->price
      ->getNumber());
    $this
      ->assertEquals('USD', $this->price
      ->getCurrencyCode());
    $this
      ->assertEquals('10 USD', $this->price
      ->__toString());
    $this
      ->assertEquals([
      'number' => '10',
      'currency_code' => 'USD',
    ], $this->price
      ->toArray());
  }

  /**
   * Tests addition with mismatched currencies.
   *
   * ::covers add.
   */
  public function testAddWithMismatchedCurrencies() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('The provided prices have mismatched currencies: 10 USD, 5 EUR.');
    $this->price
      ->add(new Price('5', 'EUR'));
  }

  /**
   * Tests subtraction with mismatched currencies.
   *
   * ::covers subtract.
   */
  public function testSubtractWithMismatchedCurrencies() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('The provided prices have mismatched currencies: 10 USD, 4 EUR.');
    $this->price
      ->subtract(new Price('4', 'EUR'));
  }

  /**
   * Tests the arithmetic methods.
   *
   * ::covers add
   * ::covers subtract
   * ::covers multiply
   * ::covers divide.
   */
  public function testArithmetic() {
    $result = $this->price
      ->add(new Price('5', 'USD'));
    $this
      ->assertEquals(new Price('15', 'USD'), $result);
    $result = $this->price
      ->subtract(new Price('5', 'USD'));
    $this
      ->assertEquals(new Price('5', 'USD'), $result);
    $result = $this->price
      ->multiply('5');
    $this
      ->assertEquals(new Price('50', 'USD'), $result);
    $result = $this->price
      ->divide('10');
    $this
      ->assertEquals(new Price('1', 'USD'), $result);
  }

  /**
   * Tests the comparison methods.
   *
   * ::covers isPositive
   * ::covers isNegative
   * ::covers isZero
   * ::covers equals
   * ::covers greaterThan
   * ::covers greaterThanOrEqual
   * ::covers lessThan
   * ::covers lessThanOrEqual
   * ::covers compareTo.
   */
  public function testComparison() {
    $this
      ->assertTrue($this->price
      ->isPositive());
    $this
      ->assertFalse($this->price
      ->isNegative());
    $this
      ->assertFalse($this->price
      ->isZero());
    $negative_price = new Price('-10', 'USD');
    $this
      ->assertFalse($negative_price
      ->isPositive());
    $this
      ->assertTrue($negative_price
      ->isNegative());
    $this
      ->assertFalse($negative_price
      ->isZero());
    $zero_price = new Price('0', 'USD');
    $this
      ->assertFalse($zero_price
      ->isPositive());
    $this
      ->assertFalse($zero_price
      ->isNegative());
    $this
      ->assertTrue($zero_price
      ->isZero());
    $this
      ->assertNotEmpty($this->price
      ->equals(new Price('10', 'USD')));
    $this
      ->assertEmpty($this->price
      ->equals(new Price('15', 'USD')));
    $this
      ->assertNotEmpty($this->price
      ->greaterThan(new Price('5', 'USD')));
    $this
      ->assertEmpty($this->price
      ->greaterThan(new Price('10', 'USD')));
    $this
      ->assertEmpty($this->price
      ->greaterThan(new Price('15', 'USD')));
    $this
      ->assertNotEmpty($this->price
      ->greaterThanOrEqual(new Price('5', 'USD')));
    $this
      ->assertNotEmpty($this->price
      ->greaterThanOrEqual(new Price('10', 'USD')));
    $this
      ->assertEmpty($this->price
      ->greaterThanOrEqual(new Price('15', 'USD')));
    $this
      ->assertNotEmpty($this->price
      ->lessThan(new Price('15', 'USD')));
    $this
      ->assertEmpty($this->price
      ->lessThan(new Price('10', 'USD')));
    $this
      ->assertEmpty($this->price
      ->lessThan(new Price('5', 'USD')));
    $this
      ->assertNotEmpty($this->price
      ->lessThanOrEqual(new Price('15', 'USD')));
    $this
      ->assertNotEmpty($this->price
      ->lessThanOrEqual(new Price('10', 'USD')));
    $this
      ->assertEmpty($this->price
      ->lessThanOrEqual(new Price('5', 'USD')));
  }

}

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.
PriceTest::$price protected property The price.
PriceTest::setUp protected function Overrides UnitTestCase::setUp
PriceTest::testAddWithMismatchedCurrencies public function Tests addition with mismatched currencies.
PriceTest::testArithmetic public function Tests the arithmetic methods.
PriceTest::testComparison public function Tests the comparison methods.
PriceTest::testCreateFromInvalidArray public function Tests creating a price from an invalid array.
PriceTest::testCreateFromValidArray public function Tests creating a price from a valid array.
PriceTest::testGetters public function Tests the methods for getting the number/currency code in various formats.
PriceTest::testInvalidCurrencyCode public function Tests creating a price with an invalid currency code.
PriceTest::testInvalidNumber public function Tests creating a price with an invalid number.
PriceTest::testSubtractWithMismatchedCurrencies public function Tests subtraction with mismatched currencies.
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.