You are here

public function EarlyOrderProcessorTest::testProcess in Commerce Shipping 8.2

@covers ::process @covers ::shouldRepack

File

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

Class

EarlyOrderProcessorTest
Tests the early order processor.

Namespace

Drupal\Tests\commerce_shipping\Kernel

Code

public function testProcess() {

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
  $shipments = $this->order
    ->get('shipments')
    ->referencedEntities();
  $this
    ->assertCount(1, $shipments);

  // Repack on adding an order item.
  $second_order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 2,
    'title' => $this->variations[1]
      ->getOrderItemTitle(),
    'purchased_entity' => $this->variations[1],
    'unit_price' => new Price('10', 'USD'),
  ]);
  $second_order_item
    ->save();
  $this->order
    ->addItem($second_order_item);
  $this->processor
    ->process($this->order);
  $shipments = $this->order
    ->get('shipments')
    ->referencedEntities();

  // Confirm that the first shipment's amount and adjustments were reset.

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $first_shipment */
  $first_shipment = reset($shipments);
  $this
    ->assertEquals(new Price('4', 'USD'), $first_shipment
    ->getOriginalAmount());
  $this
    ->assertEquals(new Price('4', 'USD'), $first_shipment
    ->getAmount());
  $this
    ->assertEmpty($first_shipment
    ->getAdjustments());

  // Confirm that an additional shipment was created.
  $this
    ->assertCount(2, $shipments);

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $second_shipment */
  $second_shipment = end($shipments);
  $this
    ->assertNull($second_shipment
    ->getOriginalAmount());
  $this
    ->assertNull($second_shipment
    ->getAmount());
  $this
    ->assertEmpty($second_shipment
    ->getAdjustments());

  // No repack when the checkout page changed but the order items didn't.
  // The country change makes the DefaultPacker take over from TestPacker,
  // resulting in a single shipment.
  $this->shippingProfile
    ->get('address')->country_code = 'RS';
  $this->shippingProfile
    ->save();
  $this->order->original = clone $this->order;
  $this->order
    ->set('checkout_step', 'review');
  $this->processor
    ->process($this->order);
  $this
    ->assertCount(2, $this->order
    ->get('shipments')
    ->referencedEntities());

  // Repack when the checkout page changed but so did the order items.
  $this->order->original = clone $this->order;
  $this->order->original
    ->set('checkout_step', 'order_information');
  $this->order
    ->removeItem($second_order_item);
  $this->order
    ->set('checkout_step', 'review');
  $this->processor
    ->process($this->order);
  $this
    ->assertCount(1, $this->order
    ->get('shipments')
    ->referencedEntities());
}