You are here

public function DefaultPackerTest::testPack in Commerce Shipping 8.2

::covers pack.

File

tests/src/Unit/Packer/DefaultPackerTest.php, line 59

Class

DefaultPackerTest
@coversDefaultClass \Drupal\commerce_shipping\Packer\DefaultPacker @group commerce_shipping

Namespace

Drupal\Tests\commerce_shipping\Unit\Packer

Code

public function testPack() {
  $order_items = [];
  $first_order_item = $this
    ->prophesize(OrderItemInterface::class);
  $first_order_item
    ->id()
    ->willReturn(2001);
  $first_order_item
    ->getPurchasedEntity()
    ->willReturn(NULL);
  $first_order_item
    ->getQuantity()
    ->willReturn(1);
  $order_items[] = $first_order_item
    ->reveal(1);
  $weight_item = $this
    ->prophesize(MeasurementItem::class);
  $weight_item
    ->toMeasurement()
    ->willReturn(new Weight('10', 'kg'));
  $weight_list = $this
    ->prophesize(FieldItemListInterface::class);
  $weight_list
    ->isEmpty()
    ->willReturn(FALSE);
  $weight_list
    ->first()
    ->willReturn($weight_item
    ->reveal());
  $purchased_entity = $this
    ->prophesize(PurchasableEntityInterface::class);
  $purchased_entity
    ->id()
    ->willReturn(3001);
  $purchased_entity
    ->getEntityTypeId()
    ->willReturn('commerce_product_variation');
  $purchased_entity
    ->hasField('weight')
    ->willReturn(TRUE);
  $purchased_entity
    ->get('weight')
    ->willReturn($weight_list
    ->reveal());
  $purchased_entity = $purchased_entity
    ->reveal();
  $second_order_item = $this
    ->prophesize(OrderItemInterface::class);
  $second_order_item
    ->id()
    ->willReturn(2002);
  $second_order_item
    ->getTitle()
    ->willReturn('T-shirt (red, small)');
  $second_order_item
    ->getPurchasedEntity()
    ->willReturn($purchased_entity);
  $second_order_item
    ->getUnitPrice()
    ->willReturn(new Price('15', 'USD'));
  $second_order_item
    ->getQuantity()
    ->willReturn(3);
  $order_items[] = $second_order_item
    ->reveal();
  $third_order_item = $this
    ->prophesize(OrderItemInterface::class);
  $third_order_item
    ->id()
    ->willReturn(2003);
  $third_order_item
    ->getTitle()
    ->willReturn('T-shirt (black, small)');
  $third_order_item
    ->getPurchasedEntity()
    ->willReturn($purchased_entity);
  $third_order_item
    ->getUnitPrice()
    ->willReturn(new Price('15', 'USD'));
  $third_order_item
    ->getQuantity()
    ->willReturn(0);
  $order_items[] = $third_order_item
    ->reveal();
  $order = $this
    ->prophesize(OrderInterface::class);
  $order
    ->bundle()
    ->willReturn('default');
  $order
    ->id()
    ->willReturn(2);
  $order
    ->getItems()
    ->willReturn($order_items);
  $order = $order
    ->reveal();
  $shipping_profile = $this
    ->prophesize(ProfileInterface::class);
  $shipping_profile
    ->id()
    ->willReturn(3);
  $shipping_profile = $shipping_profile
    ->reveal();
  $expected_proposed_shipment = new ProposedShipment([
    'type' => 'default',
    'order_id' => 2,
    'title' => 'Shipment #1',
    'items' => [
      new ShipmentItem([
        'order_item_id' => 2002,
        'title' => 'T-shirt (red, small)',
        'quantity' => 3,
        'weight' => new Weight('30', 'kg'),
        'declared_value' => new Price('45', 'USD'),
      ]),
    ],
    'shipping_profile' => $shipping_profile,
  ]);
  $result = $this->packer
    ->pack($order, $shipping_profile);
  $this
    ->assertEquals([
    $expected_proposed_shipment,
  ], $result);
}