You are here

public function CanadianSalesTaxTest::testApplication in Commerce Core 8.2

@covers ::applies @covers ::apply

File

modules/tax/tests/src/Kernel/Plugin/Commerce/TaxType/CanadianSalesTaxTest.php, line 82

Class

CanadianSalesTaxTest
@coversDefaultClass \Drupal\commerce_tax\Plugin\Commerce\TaxType\CanadianSalesTax @group commerce

Namespace

Drupal\Tests\commerce_tax\Kernel\Plugin\Commerce\TaxType

Code

public function testApplication() {
  $plugin = $this->taxType
    ->getPlugin();

  // British Columbia customer, GST+PST.
  $order = $this
    ->buildOrder('CA', 'BC');
  $this
    ->assertTrue($plugin
    ->applies($order));
  $plugin
    ->apply($order);
  $adjustments = $order
    ->collectAdjustments();
  $this
    ->assertCount(2, $adjustments);
  $this
    ->assertEquals('canadian_sales_tax|ca|gst', $adjustments[0]
    ->getSourceId());
  $this
    ->assertEquals('canadian_sales_tax|bc|pst', $adjustments[1]
    ->getSourceId());
  $this
    ->assertEquals(t('GST'), $adjustments[0]
    ->getLabel());
  $this
    ->assertEquals(t('PST'), $adjustments[1]
    ->getLabel());

  // Both taxes should be calculated on the unit price (non-cumulative).
  $this
    ->assertEquals(new Price('0.5', 'USD'), $adjustments[0]
    ->getAmount());
  $this
    ->assertEquals(new Price('0.7', 'USD'), $adjustments[1]
    ->getAmount());

  // Alberta customer, GST.
  $order = $this
    ->buildOrder('CA', 'AB');
  $this
    ->assertTrue($plugin
    ->applies($order));
  $plugin
    ->apply($order);
  $adjustments = $order
    ->collectAdjustments();
  $adjustment = reset($adjustments);
  $this
    ->assertCount(1, $adjustments);
  $this
    ->assertEquals('canadian_sales_tax|ca|gst', $adjustment
    ->getSourceId());

  // Ontario customer, HST.
  $order = $this
    ->buildOrder('CA', 'ON');
  $this
    ->assertTrue($plugin
    ->applies($order));
  $plugin
    ->apply($order);
  $adjustments = $order
    ->collectAdjustments();
  $adjustment = reset($adjustments);
  $this
    ->assertCount(1, $adjustments);
  $this
    ->assertEquals('canadian_sales_tax|on|hst', $adjustment
    ->getSourceId());

  // US customer.
  $order = $this
    ->buildOrder('US', 'SC');
  $plugin
    ->apply($order);
  $this
    ->assertCount(0, $order
    ->collectAdjustments());
}