You are here

public function ShipmentTest::testPopulatingFromProposedShipment in Commerce Shipping 8.2

@covers ::populateFromProposedShipment

File

tests/src/Kernel/Entity/ShipmentTest.php, line 274

Class

ShipmentTest
Tests the Shipment entity.

Namespace

Drupal\Tests\commerce_shipping\Kernel\Entity

Code

public function testPopulatingFromProposedShipment() {

  /** @var \Drupal\profile\Entity\ProfileInterface $profile */
  $profile = Profile::create([
    'type' => 'customer',
  ]);
  $profile
    ->save();
  $profile = $this
    ->reloadEntity($profile);
  $proposed_shipment = new ProposedShipment([
    'type' => 'default',
    'order_id' => 10,
    'title' => 'Test title',
    'items' => [
      new ShipmentItem([
        'order_item_id' => 10,
        'title' => 'T-shirt (red, large)',
        'quantity' => 1,
        'weight' => new Weight('10', 'kg'),
        'declared_value' => new Price('15', 'USD'),
      ]),
    ],
    'shipping_profile' => $profile,
    'package_type_id' => 'custom_box',
    // State is not a custom field, but it simplifies this test.
    'custom_fields' => [
      'state' => 'ready',
      'no_field' => 'custom_value',
    ],
  ]);
  $shipment = Shipment::create([
    'type' => 'default',
    'title' => 'Shipment',
  ]);
  $shipment
    ->populateFromProposedShipment($proposed_shipment);
  $this
    ->assertEquals($proposed_shipment
    ->getOrderId(), $shipment
    ->getOrderId());
  $this
    ->assertEquals($proposed_shipment
    ->getPackageTypeId(), $shipment
    ->getPackageType()
    ->getId());
  $this
    ->assertEquals($profile, $shipment
    ->getShippingProfile());
  $this
    ->assertEquals($proposed_shipment
    ->getTitle(), $shipment
    ->getTitle());
  $this
    ->assertEquals($proposed_shipment
    ->getItems(), $shipment
    ->getItems());
  $this
    ->assertEquals(new Weight('10', 'kg'), $shipment
    ->getWeight());
  $this
    ->assertEquals('ready', $shipment
    ->getState()->value);
  $this
    ->assertEquals('custom_value', $shipment
    ->getData('no_field'));
}