You are here

class MeasurementTest in Physical Fields 8

Tests the measurement base class.

@coversDefaultClass \Drupal\physical\Measurement @group physical

Hierarchy

Expanded class hierarchy of MeasurementTest

File

tests/src/Unit/MeasurementTest.php, line 14

Namespace

Drupal\Tests\physical\Unit
View source
class MeasurementTest extends UnitTestCase {

  /**
   * The measurement.
   *
   * @var \Drupal\physical\Measurement
   */
  protected $measurement;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->measurement = new Length('10', 'm');
  }

  /**
   * ::covers __construct.
   */
  public function testInvalidNumber() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $measurement = new Length('INVALID', 'm');
  }

  /**
   * Tests the methods for getting the number and unit in various formats.
   *
   * ::covers getNumber
   * ::covers getUnit
   * ::covers __toString
   * ::covers toArray.
   */
  public function testGetters() {
    $this
      ->assertEquals('10', $this->measurement
      ->getNumber());
    $this
      ->assertEquals('m', $this->measurement
      ->getUnit());
    $this
      ->assertEquals('10 m', $this->measurement
      ->__toString());
    $this
      ->assertEquals([
      'number' => '10',
      'unit' => 'm',
    ], $this->measurement
      ->toArray());
  }

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

    // Test mismatched units.
    $result = $this->measurement
      ->add(new Length('200', 'cm'));
    $this
      ->assertEquals(new Length('12', 'm'), $result);
    $result = $this->measurement
      ->subtract(new Length('2.5', 'ft'));
    $this
      ->assertEquals(new Length('9.238', 'm'), $result);
  }

  /**
   * Tests the comparison methods.
   *
   * ::covers isZero
   * ::covers equals
   * ::covers greaterThan
   * ::covers greaterThanOrEqual
   * ::covers lessThan
   * ::covers lessThanOrEqual
   * ::covers compareTo.
   */
  public function testComparison() {
    $this
      ->assertFalse($this->measurement
      ->isZero());
    $zero_measurement = new Length('0', 'm');
    $this
      ->assertTrue($zero_measurement
      ->isZero());
    $this
      ->assertTrue($this->measurement
      ->equals(new Length('10', 'm')));
    $this
      ->assertFalse($this->measurement
      ->equals(new Length('15', 'm')));
    $this
      ->assertTrue($this->measurement
      ->greaterThan(new Length('5', 'm')));
    $this
      ->assertFalse($this->measurement
      ->greaterThan(new Length('10', 'm')));
    $this
      ->assertFalse($this->measurement
      ->greaterThan(new Length('15', 'm')));
    $this
      ->assertTrue($this->measurement
      ->greaterThanOrEqual(new Length('5', 'm')));
    $this
      ->assertTrue($this->measurement
      ->greaterThanOrEqual(new Length('10', 'm')));
    $this
      ->assertFalse($this->measurement
      ->greaterThanOrEqual(new Length('15', 'm')));
    $this
      ->assertTrue($this->measurement
      ->lessThan(new Length('15', 'm')));
    $this
      ->assertFalse($this->measurement
      ->lessThan(new Length('10', 'm')));
    $this
      ->assertFalse($this->measurement
      ->lessThan(new Length('5', 'm')));
    $this
      ->assertTrue($this->measurement
      ->lessThanOrEqual(new Length('15', 'm')));
    $this
      ->assertTrue($this->measurement
      ->lessThanOrEqual(new Length('10', 'm')));
    $this
      ->assertFalse($this->measurement
      ->lessThanOrEqual(new Length('5', 'm')));

    // Test mismatched units.
    $this
      ->assertTrue($this->measurement
      ->equals(new Length('1000', 'cm')));
    $this
      ->assertTrue($this->measurement
      ->greaterThan(new Length('500', 'cm')));
    $this
      ->assertTrue($this->measurement
      ->greaterThanOrEqual(new Length('500', 'cm')));
    $this
      ->assertTrue($this->measurement
      ->greaterThanOrEqual(new Length('1000', 'cm')));
    $this
      ->assertTrue($this->measurement
      ->lessThan(new Length('1500', 'cm')));
    $this
      ->assertTrue($this->measurement
      ->lessThanOrEqual(new Length('1500', 'cm')));
    $this
      ->assertTrue($this->measurement
      ->lessThanOrEqual(new Length('1000', 'cm')));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MeasurementTest::$measurement protected property The measurement.
MeasurementTest::setUp public function Overrides UnitTestCase::setUp
MeasurementTest::testArithmetic public function Tests the arithmetic methods.
MeasurementTest::testComparison public function Tests the comparison methods.
MeasurementTest::testGetters public function Tests the methods for getting the number and unit in various formats.
MeasurementTest::testInvalidNumber public function ::covers __construct.
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.