class MeasurementTest in Physical Fields 8
Tests the measurement base class.
@coversDefaultClass \Drupal\physical\Measurement @group physical
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\physical\Unit\MeasurementTest
Expanded class hierarchy of MeasurementTest
File
- tests/
src/ Unit/ MeasurementTest.php, line 14
Namespace
Drupal\Tests\physical\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MeasurementTest:: |
protected | property | The measurement. | |
MeasurementTest:: |
public | function |
Overrides UnitTestCase:: |
|
MeasurementTest:: |
public | function | Tests the arithmetic methods. | |
MeasurementTest:: |
public | function | Tests the comparison methods. | |
MeasurementTest:: |
public | function | Tests the methods for getting the number and unit in various formats. | |
MeasurementTest:: |
public | function | ::covers __construct. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |