class CalculatorTest in Commerce Core 8.2
Tests the Calculator class.
@group commerce @coversDefaultClass \Drupal\commerce_price\Calculator
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\commerce_price\Unit\CalculatorTest
Expanded class hierarchy of CalculatorTest
File
- modules/
price/ tests/ src/ Unit/ CalculatorTest.php, line 14
Namespace
Drupal\Tests\commerce_price\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CalculatorTest:: |
public | function | @covers ::add @covers ::subtract @covers ::multiply @covers ::divide @covers ::compare @covers ::trim | |
CalculatorTest:: |
public | function | @covers ::compare | |
CalculatorTest:: |
public | function | @covers ::ceil @covers ::floor @covers ::round | |
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. | |
UnitTestCase:: |
protected | function | 340 |