You are here

public function PackerManagerTest::testPackToShipments in Commerce Shipping 8.2

::covers packToShipments.

File

tests/src/Kernel/PackerManagerTest.php, line 86

Class

PackerManagerTest
Tests the packer manager.

Namespace

Drupal\Tests\commerce_shipping\Kernel

Code

public function testPackToShipments() {
  $user = $this
    ->createUser([
    'mail' => $this
      ->randomString() . '@example.com',
  ]);
  $first_variation = ProductVariation::create([
    'type' => 'default',
    'sku' => 'test-product-01',
    'title' => 'Hat',
    'price' => new Price('10', 'USD'),
  ]);
  $first_variation
    ->save();
  $second_variation = ProductVariation::create([
    'type' => 'default',
    'sku' => 'test-product-02',
    'title' => 'Mug',
    'price' => new Price('10', 'USD'),
  ]);
  $second_variation
    ->save();
  $first_order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 2,
    'title' => $first_variation
      ->getOrderItemTitle(),
    'purchased_entity' => $first_variation,
    'unit_price' => new Price('10', 'USD'),
  ]);
  $first_order_item
    ->save();

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = Order::create([
    'type' => 'default',
    'uid' => $user
      ->id(),
    'store_id' => $this->store
      ->id(),
    'order_items' => [
      $first_order_item,
    ],
  ]);
  $order
    ->save();
  $shipping_profile = Profile::create([
    'type' => 'customer',
    'address' => [
      'country_code' => 'FR',
    ],
  ]);
  $shipping_profile
    ->save();

  // Use the TestPacker that creates a shipment per order item.
  $this->packerManager
    ->addPacker(new TestPacker());
  $shipments = [];
  list($shipments, $removed_shipments) = $this->packerManager
    ->packToShipments($order, $shipping_profile, $shipments);
  $this
    ->assertCount(1, $shipments);
  $shipment = $shipments[0];
  $this
    ->assertEquals('Hat', $shipment
    ->getItems()[0]
    ->getTitle());
  $this
    ->assertTrue($shipment
    ->isNew());
  $this
    ->assertTrue($shipment
    ->getData('owned_by_packer'));
  $this
    ->assertCount(0, $removed_shipments);
  $shipment
    ->save();
  $second_order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 2,
    'title' => $second_variation
      ->getOrderItemTitle(),
    'purchased_entity' => $second_variation,
    'unit_price' => new Price('10', 'USD'),
  ]);
  $second_order_item
    ->save();
  $order
    ->addItem($second_order_item);

  // The first shipment should be reused, and a second one created.
  $shipment_id = $shipment
    ->id();
  $shipments = [
    $shipment,
  ];
  list($shipments, $removed_shipments) = $this->packerManager
    ->packToShipments($order, $shipping_profile, $shipments);
  $this
    ->assertCount(2, $shipments);
  $first_shipment = $shipments[0];
  $this
    ->assertEquals($shipment_id, $first_shipment
    ->id());
  $this
    ->assertEquals('Hat', $first_shipment
    ->getItems()[0]
    ->getTitle());
  $this
    ->assertFalse($first_shipment
    ->isNew());
  $this
    ->assertTrue($first_shipment
    ->getData('owned_by_packer'));
  $second_shipment = $shipments[1];
  $this
    ->assertEquals('Mug', $second_shipment
    ->getItems()[0]
    ->getTitle());
  $this
    ->assertTrue($second_shipment
    ->isNew());
  $this
    ->assertTrue($second_shipment
    ->getData('owned_by_packer'));
  $this
    ->assertCount(0, $removed_shipments);

  // The second order item will now be packed as the first shipment.
  $order
    ->removeItem($first_order_item);
  list($shipments, $removed_shipments) = $this->packerManager
    ->packToShipments($order, $shipping_profile, $shipments);
  $this
    ->assertCount(1, $shipments);
  $first_shipment = $shipments[0];
  $this
    ->assertEquals($shipment_id, $first_shipment
    ->id());
  $this
    ->assertEquals('Mug', $first_shipment
    ->getItems()[0]
    ->getTitle());
  $this
    ->assertFalse($first_shipment
    ->isNew());
  $this
    ->assertTrue($first_shipment
    ->getData('owned_by_packer'));

  // The second shipment was never saved, so it's not in $removed_shipments.
  $this
    ->assertCount(0, $removed_shipments);

  // No order items left.
  $order
    ->removeItem($second_order_item);
  list($shipments, $removed_shipments) = $this->packerManager
    ->packToShipments($order, $shipping_profile, $shipments);
  $this
    ->assertCount(0, $shipments);
  $this
    ->assertCount(1, $removed_shipments);
  $this
    ->assertEquals($shipment_id, $removed_shipments[0]
    ->id());
}