You are here

protected function ShipmentFixedAmountOffTest::setUp in Commerce Shipping 8.2

Overrides ShippingKernelTestBase::setUp

File

tests/src/Kernel/Plugin/Commerce/PromotionOffer/ShipmentFixedAmountOffTest.php, line 47

Class

ShipmentFixedAmountOffTest
Tests the "Fixed amount off the shipment amount" offer.

Namespace

Drupal\Tests\commerce_shipping\Kernel\Plugin\Commerce\PromotionOffer

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installEntitySchema('commerce_promotion');
  $this
    ->installSchema('commerce_promotion', [
    'commerce_promotion_usage',
  ]);
  $first_variation = ProductVariation::create([
    'type' => 'default',
    'sku' => 'test-product-01',
    'title' => 'Hat',
    'price' => new Price('10.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('10.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('10.00', 'USD'),
  ]);
  $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('10.00', 'USD'),
  ]);
  $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,
    ],
  ]);
  $order
    ->save();
  $first_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' => '5',
          'currency_code' => 'USD',
        ],
      ],
    ],
    'status' => TRUE,
  ]);
  $first_shipping_method
    ->save();
  $second_shipping_method = ShippingMethod::create([
    'stores' => $this->store
      ->id(),
    'name' => 'Express shipping',
    'plugin' => [
      'target_plugin_id' => 'flat_rate',
      'target_plugin_configuration' => [
        'rate_label' => 'Express shipping',
        'rate_amount' => [
          'number' => '20',
          'currency_code' => 'USD',
        ],
      ],
    ],
    'status' => TRUE,
  ]);
  $second_shipping_method
    ->save();

  /** @var \Drupal\profile\Entity\ProfileInterface $shipping_profile */
  $shipping_profile = Profile::create([
    'type' => 'customer',
    'address' => [
      'country_code' => 'FR',
    ],
  ]);
  $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);
  $first_shipment = reset($shipments);
  $first_shipment
    ->setShippingMethodId($first_shipping_method
    ->id());
  $first_shipment
    ->setShippingService('default');
  $first_shipment
    ->setOriginalAmount(new Price('5.00', 'USD'));
  $first_shipment
    ->setAmount(new Price('5.00', 'USD'));
  $second_shipment = end($shipments);
  $second_shipment
    ->setShippingMethodId($second_shipping_method
    ->id());
  $second_shipment
    ->setShippingService('default');
  $second_shipment
    ->setOriginalAmount(new Price('20.00', 'USD'));
  $second_shipment
    ->setAmount(new Price('20.00', 'USD'));
  $order
    ->set('shipments', [
    $first_shipment,
    $second_shipment,
  ]);
  $order
    ->setRefreshState(Order::REFRESH_SKIP);
  $order
    ->save();
  $this->order = $order;
  $this->promotion = Promotion::create([
    'name' => 'Promotion 1',
    'order_types' => [
      $this->order
        ->bundle(),
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'shipment_fixed_amount_off',
      'target_plugin_configuration' => [
        'display_inclusive' => TRUE,
        'amount' => [
          'number' => '11.00',
          'currency_code' => 'USD',
        ],
      ],
    ],
    'status' => TRUE,
  ]);
  $this->promotion
    ->save();
}