You are here

protected function EarlyOrderProcessorTest::setUp in Commerce Shipping 8.2

Overrides ShippingKernelTestBase::setUp

File

tests/src/Kernel/EarlyOrderProcessorTest.php, line 59

Class

EarlyOrderProcessorTest
Tests the early order processor.

Namespace

Drupal\Tests\commerce_shipping\Kernel

Code

protected function setUp() : void {
  parent::setUp();
  $this->processor = $this->container
    ->get('commerce_shipping.early_order_processor');
  $this->variations[] = ProductVariation::create([
    'type' => 'default',
    'sku' => 'test-product-01',
    'title' => 'Hat',
    'price' => new Price('10', 'USD'),
    'weight' => new Weight('0', 'g'),
  ]);
  $this->variations[] = ProductVariation::create([
    'type' => 'default',
    'sku' => 'test-product-02',
    'title' => 'Mug',
    'price' => new Price('10', 'USD'),
    'weight' => new Weight('0', 'g'),
  ]);
  $this->variations[0]
    ->save();
  $this->variations[1]
    ->save();
  $first_order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 2,
    'title' => $this->variations[0]
      ->getOrderItemTitle(),
    'purchased_entity' => $this->variations[0],
    'unit_price' => new Price('10', 'USD'),
  ]);
  $first_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,
    ],
  ]);
  $order
    ->save();

  /** @var \Drupal\profile\Entity\ProfileInterface $shipping_profile */
  $shipping_profile = Profile::create([
    'type' => 'customer',
    'address' => [
      'country_code' => 'FR',
    ],
  ]);
  $shipping_profile
    ->save();

  // Create the first shipment.
  $shipping_order_manager = $this->container
    ->get('commerce_shipping.order_manager');
  $shipments = $shipping_order_manager
    ->pack($order, $shipping_profile);

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
  $shipment = reset($shipments);
  $shipment
    ->setOriginalAmount(new Price('4', 'USD'));
  $shipment
    ->setAmount(new Price('6', 'USD'));
  $shipment
    ->addAdjustment(new Adjustment([
    'type' => 'fee',
    'label' => 'Handling fee',
    'amount' => new Price('2.00', 'USD'),
    'included' => TRUE,
  ]));
  $shipment
    ->save();
  $order
    ->set('shipments', [
    $shipment,
  ]);
  $order
    ->setRefreshState(Order::REFRESH_SKIP);
  $order
    ->save();
  $this->order = $order;
  $this->shippingProfile = $shipping_profile;
}