class MinorUnitsConverterTest in Commerce Core 8.2
Tests the MinorUnitsConverter class.
@coversDefaultClass \Drupal\commerce_price\MinorUnitsConverter @group commerce
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\commerce_price\Unit\MinorUnitsConverterTest
Expanded class hierarchy of MinorUnitsConverterTest
File
- modules/
price/ tests/ src/ Unit/ MinorUnitsConverterTest.php, line 19
Namespace
Drupal\Tests\commerce_price\UnitView source
class MinorUnitsConverterTest extends UnitTestCase {
/**
* The minor units converter.
*
* @var \Drupal\commerce_price\MinorUnitsConverterInterface
*/
protected $minorUnitsConverter;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$storage = $this
->prophesize(EntityStorageInterface::class);
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$entity_type_manager
->getStorage('commerce_currency')
->willReturn($storage
->reveal());
$this->minorUnitsConverter = new MinorUnitsConverter(new CurrencyRepository($entity_type_manager
->reveal()));
}
/**
* Tests converting a price with an unknown currency.
*
* @covers ::toMinorUnits
*/
public function testUnknownCurrency() {
$this
->expectException(UnknownCurrencyException::class);
$this->minorUnitsConverter
->toMinorUnits(new Price('10', 'XYZ'));
}
/**
* Tests if price can be converted to minor unit and then back to decimal.
*
* @dataProvider currencyConversionData
*/
public function testConversion(Price $price, $expected_amount) {
$amount = $this->minorUnitsConverter
->toMinorUnits($price);
$this
->assertEquals($expected_amount, $amount);
$this
->assertTrue($this->minorUnitsConverter
->fromMinorUnits($amount, $price
->getCurrencyCode())
->equals($price));
}
/**
* Data provider for ::testConversion.
*
* @return array
* The test data.
*/
public function currencyConversionData() {
return [
[
new Price(10, 'EUR'),
1000,
],
[
new Price(1.23, 'USD'),
123,
],
[
new Price(0.99, 'NOK'),
99,
],
[
new Price(99, 'JPY'),
99,
],
[
new Price(99, 'KWD'),
99000,
],
];
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MinorUnitsConverterTest:: |
protected | property | The minor units converter. | |
MinorUnitsConverterTest:: |
public | function | Data provider for ::testConversion. | |
MinorUnitsConverterTest:: |
protected | function |
Overrides UnitTestCase:: |
|
MinorUnitsConverterTest:: |
public | function | Tests if price can be converted to minor unit and then back to decimal. | |
MinorUnitsConverterTest:: |
public | function | Tests converting a price with an unknown currency. | |
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. |