You are here

class VolumeTest in Physical Fields 8

Tests the volume class.

@coversDefaultClass \Drupal\physical\Volume @group physical

Hierarchy

Expanded class hierarchy of VolumeTest

File

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

Namespace

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

  /**
   * The volume.
   *
   * @var \Drupal\physical\Volume
   */
  protected $volume;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->volume = new Volume('4', 'm3');
  }

  /**
   * ::covers __construct.
   */
  public function testInvalidUnit() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $volume = new Volume('1', 'kg');
  }

  /**
   * Tests unit conversion.
   *
   * ::covers convert.
   */
  public function testConvert() {
    $this
      ->assertEquals(new Volume('4000000', 'ml'), $this->volume
      ->convert('ml')
      ->round());
    $this
      ->assertEquals(new Volume('400000', 'cl'), $this->volume
      ->convert('cl')
      ->round());
    $this
      ->assertEquals(new Volume('40000', 'dl'), $this->volume
      ->convert('dl')
      ->round());
    $this
      ->assertEquals(new Volume('4000', 'l'), $this->volume
      ->convert('l')
      ->round());
    $this
      ->assertEquals(new Volume('4000000000', 'mm3'), $this->volume
      ->convert('mm3')
      ->round());
    $this
      ->assertEquals(new Volume('4000000', 'cm3'), $this->volume
      ->convert('cm3')
      ->round());
    $this
      ->assertEquals(new Volume('244095', 'in3'), $this->volume
      ->convert('in3')
      ->round());
    $this
      ->assertEquals(new Volume('141.259', 'ft3'), $this->volume
      ->convert('ft3')
      ->round(3));
    $this
      ->assertEquals(new Volume('135256', 'fl oz'), $this->volume
      ->convert('fl oz')
      ->round());
    $this
      ->assertEquals(new Volume('1056.69', 'gal'), $this->volume
      ->convert('gal')
      ->round(2));
  }

}

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.
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.
VolumeTest::$volume protected property The volume.
VolumeTest::setUp public function Overrides UnitTestCase::setUp
VolumeTest::testConvert public function Tests unit conversion.
VolumeTest::testInvalidUnit public function ::covers __construct.