You are here

protected function ShippingTest::setUp in Commerce Shipping 8.2

Overrides ShippingKernelTestBase::setUp

File

tests/src/Kernel/Plugin/Commerce/TaxType/ShippingTest.php, line 48

Class

ShippingTest
Tests the shipping tax type.

Namespace

Drupal\Tests\commerce_shipping\Kernel\Plugin\Commerce\TaxType

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installConfig([
    'commerce_tax',
  ]);
  $this->store
    ->set('prices_include_tax', TRUE);
  $this->store
    ->save();
  $eu_tax_type = TaxType::create([
    'id' => 'eu_vat',
    'label' => 'EU VAT',
    'plugin' => 'european_union_vat',
    'configuration' => [
      'display_inclusive' => TRUE,
    ],
    // Don't allow the tax type to apply automatically.
    'status' => FALSE,
  ]);
  $eu_tax_type
    ->save();
  $first_variation = ProductVariation::create([
    'type' => 'default',
    'sku' => 'test-product-01',
    'title' => 'Hat',
    'price' => new Price('70.00', 'USD'),
    'weight' => new Weight('0', 'g'),
  ]);
  $first_variation
    ->save();
  $second_variation = ProductVariation::create([
    'type' => 'default',
    'sku' => 'test-product-02',
    'title' => 'Mug',
    'price' => new Price('15.00', 'USD'),
    'weight' => new Weight('0', 'g'),
  ]);
  $second_variation
    ->save();
  $first_order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 1,
    'title' => $first_variation
      ->getOrderItemTitle(),
    'purchased_entity' => $first_variation,
    'unit_price' => new Price('70.00', 'USD'),
  ]);
  $first_order_item
    ->addAdjustment(new Adjustment([
    'type' => 'tax',
    'label' => 'VAT',
    'amount' => new Price('6.36', 'USD'),
    'percentage' => '0.1',
    'source_id' => 'eu_vat|fr|intermediate',
    'included' => TRUE,
    'locked' => TRUE,
  ]));
  $first_order_item
    ->save();
  $second_order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 1,
    'title' => $second_variation
      ->getOrderItemTitle(),
    'purchased_entity' => $second_variation,
    'unit_price' => new Price('15.00', 'USD'),
  ]);
  $second_order_item
    ->addAdjustment(new Adjustment([
    'type' => 'tax',
    'label' => 'VAT',
    'amount' => new Price('2.50', 'USD'),
    'percentage' => '0.2',
    'source_id' => 'eu_vat|fr|standard',
    'included' => TRUE,
    'locked' => TRUE,
  ]));
  $second_order_item
    ->save();
  $third_order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 1,
    'title' => $second_variation
      ->getOrderItemTitle(),
    'purchased_entity' => $second_variation,
    'unit_price' => new Price('15.00', 'USD'),
  ]);
  $third_order_item
    ->addAdjustment(new Adjustment([
    'type' => 'tax',
    'label' => 'VAT',
    'amount' => new Price('2.50', 'USD'),
    'percentage' => '0.2',
    'source_id' => 'eu_vat|fr|standard',
    'included' => TRUE,
    'locked' => TRUE,
  ]));
  $second_order_item
    ->save();

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = Order::create([
    'type' => 'default',
    'uid' => $this
      ->createUser([
      'mail' => $this
        ->randomString() . '@example.com',
    ]),
    'store_id' => $this->store
      ->id(),
    'order_items' => [
      $first_order_item,
      $second_order_item,
      $third_order_item,
    ],
  ]);
  $order
    ->save();
  $shipping_method = ShippingMethod::create([
    'stores' => $this->store
      ->id(),
    'name' => 'Standard shipping',
    'plugin' => [
      'target_plugin_id' => 'flat_rate',
      'target_plugin_configuration' => [
        'rate_label' => 'Standard shipping',
        'rate_amount' => [
          'number' => '10.00',
          'currency_code' => 'USD',
        ],
      ],
    ],
    'status' => TRUE,
  ]);
  $shipping_method
    ->save();

  /** @var \Drupal\profile\Entity\ProfileInterface $shipping_profile */
  $shipping_profile = Profile::create([
    'type' => 'customer',
    'address' => [
      'country_code' => 'DE',
    ],
  ]);
  $shipping_profile
    ->save();
  $shipping_order_manager = $this->container
    ->get('commerce_shipping.order_manager');

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
  $shipments = $shipping_order_manager
    ->pack($order, $shipping_profile);
  $shipment = reset($shipments);
  $shipment
    ->setShippingMethodId($shipping_method
    ->id());
  $shipment
    ->setShippingService('default');
  $shipment
    ->setAmount(new Price('10.00', 'USD'));
  $order
    ->set('shipments', [
    $shipment,
  ]);
  $order
    ->setRefreshState(Order::REFRESH_SKIP);
  $order
    ->save();
  $this->order = $order;
  $this->taxType = TaxType::create([
    'id' => 'shipping',
    'label' => 'Shipping',
    'plugin' => 'shipping',
    'configuration' => [
      'strategy' => 'default',
    ],
    // Don't allow the tax type to apply automatically.
    'status' => FALSE,
  ]);
  $this->taxType
    ->save();
}