You are here

public function ShippingOrderManagerTest::testPack in Commerce Shipping 8.2

@covers ::pack

File

tests/src/Kernel/ShippingOrderManagerTest.php, line 213

Class

ShippingOrderManagerTest
Tests the shipping order manager.

Namespace

Drupal\Tests\commerce_shipping\Kernel

Code

public function testPack() {
  $shipping_profile = Profile::create([
    'type' => 'customer',
    'address' => [
      'country_code' => 'FR',
    ],
  ]);
  $shipping_profile
    ->save();
  $shipping_profile = $this
    ->reloadEntity($shipping_profile);
  $shipments = $this->shippingOrderManager
    ->pack($this->shippableOrder, $shipping_profile);
  $this
    ->assertCount(1, $shipments);

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
  $shipment = $shipments[0];
  $this
    ->assertEquals('Mug', $shipment
    ->getItems()[0]
    ->getTitle());
  $this
    ->assertTrue($shipment
    ->getData('owned_by_packer'));
  $this
    ->assertEquals($shipping_profile, $shipment
    ->getShippingProfile());
  $this->shippableOrder
    ->set('shipments', $shipments);
  $variation = ProductVariation::create([
    'type' => 'default',
    'sku' => 'test-product-01',
    'title' => 'Hat',
    'price' => new Price('10', 'USD'),
  ]);
  $variation
    ->save();
  $order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 2,
    'title' => $variation
      ->getOrderItemTitle(),
    'purchased_entity' => $variation,
    'unit_price' => new Price('10', 'USD'),
  ]);
  $order_item
    ->save();
  $this->shippableOrder
    ->addItem($order_item);

  // The first shipment should be reused, and a second one created.
  $shipment_id = $shipment
    ->id();
  $shipments = $this->shippingOrderManager
    ->pack($this->shippableOrder, $shipping_profile);
  $this
    ->assertCount(2, $shipments);
  $first_shipment = $shipments[0];
  $this
    ->assertEquals($shipment_id, $first_shipment
    ->id());
  $this
    ->assertEquals('Mug', $first_shipment
    ->getItems()[0]
    ->getTitle());
  $this
    ->assertTrue($first_shipment
    ->getData('owned_by_packer'));
  $second_shipment = $shipments[1];
  $this
    ->assertEquals('Hat', $second_shipment
    ->getItems()[0]
    ->getTitle());
  $this
    ->assertTrue($second_shipment
    ->getData('owned_by_packer'));
  $shipments = $this->shippingOrderManager
    ->pack($this->nonShippableOrder);
  $this
    ->assertEmpty($shipments);
}