TaxRatePercentageTest.php in Commerce Core 8.2
File
modules/tax/tests/src/Kernel/TaxRatePercentageTest.php
View source
<?php
namespace Drupal\Tests\commerce_tax\Kernel;
use Drupal\commerce_price\Price;
use Drupal\commerce_tax\TaxRatePercentage;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
class TaxRatePercentageTest extends CommerceKernelTestBase {
public function testMissingProperty() {
$this
->expectException(\InvalidArgumentException::class);
$definition = [
'number' => '0.23',
];
new TaxRatePercentage($definition);
}
public function testInvalidNumber() {
$this
->expectException(\InvalidArgumentException::class);
$definition = [
'number' => 'INVALID',
'start_date' => '2012-01-01',
];
new TaxRatePercentage($definition);
}
public function testValid() {
$definition = [
'number' => '0.23',
'start_date' => '2012-01-01',
];
$percentage = new TaxRatePercentage($definition);
$this
->assertEquals($definition['number'], $percentage
->getNumber());
$this
->assertEquals(new DrupalDateTime($definition['start_date'], 'UTC'), $percentage
->getStartDate());
$this
->assertNull($percentage
->getEndDate());
$definition['end_date'] = '2012-12-31';
$percentage = new TaxRatePercentage($definition);
$this
->assertEquals(new DrupalDateTime($definition['end_date'], 'UTC'), $percentage
->getEndDate());
$this
->assertEquals($definition, $percentage
->toArray());
}
public function testCalculation() {
$definition = [
'number' => '0.20',
'start_date' => '2012-01-01',
];
$percentage = new TaxRatePercentage($definition);
$tax_amount = $percentage
->calculateTaxAmount(new Price('12', 'USD'), FALSE);
$this
->assertEquals(new Price('2.4', 'USD'), $tax_amount);
$tax_amount = $percentage
->calculateTaxAmount(new Price('12', 'USD'), TRUE);
$this
->assertEquals(new Price('2', 'USD'), $tax_amount);
}
}