View source
<?php
namespace Drupal\Tests\commerce_shipping\Unit;
use Drupal\commerce_price\Price;
use Drupal\commerce_shipping\ProposedShipment;
use Drupal\commerce_shipping\ShipmentItem;
use Drupal\physical\Weight;
use Drupal\profile\Entity\ProfileInterface;
use Drupal\Tests\UnitTestCase;
class ProposedShipmentTest extends UnitTestCase {
protected $proposedShipment;
protected function setUp() : void {
parent::setUp();
$shipping_profile = $this
->prophesize(ProfileInterface::class);
$shipping_profile
->id()
->willReturn(11);
$this->proposedShipment = new ProposedShipment([
'type' => 'default',
'order_id' => 10,
'title' => 'Shipment from Narnia',
'items' => [
new ShipmentItem([
'order_item_id' => 10,
'title' => 'T-shirt (red, small)',
'quantity' => 1,
'weight' => new Weight('10', 'kg'),
'declared_value' => new Price('10', 'USD'),
]),
],
'shipping_profile' => $shipping_profile
->reveal(),
'package_type_id' => 'default',
'custom_fields' => [
'field_test' => 'value',
],
]);
}
public function testGetType() {
$this
->assertEquals('default', $this->proposedShipment
->getType());
}
public function testGetOrderId() {
$this
->assertEquals(10, $this->proposedShipment
->getOrderId());
}
public function testGetTitle() {
$this
->assertEquals('Shipment from Narnia', $this->proposedShipment
->getTitle());
}
public function testGetItems() {
$expected_items = [];
$expected_items[] = new ShipmentItem([
'order_item_id' => 10,
'title' => 'T-shirt (red, small)',
'quantity' => 1,
'weight' => new Weight('10', 'kg'),
'declared_value' => new Price('10', 'USD'),
]);
$items = $this->proposedShipment
->getItems();
$this
->assertArrayEquals($expected_items, $items);
}
public function testGetShippingProfile() {
$this
->assertEquals(11, $this->proposedShipment
->getShippingProfile()
->id());
}
public function testGetPackageTypeId() {
$this
->assertEquals('default', $this->proposedShipment
->getPackageTypeId());
}
public function testGetCustomFields() {
$this
->assertEquals([
'field_test' => 'value',
], $this->proposedShipment
->getCustomFields());
}
public function testMissingProperties() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Missing required property "items".');
$proposed_shipment = new ProposedShipment([
'type' => 'default',
'order_id' => 10,
'title' => 'Test shipment',
'package_type_id' => 'default',
]);
}
public function testInvalidItems() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Each shipment item under the "items" property must be an instance of ShipmentItem.');
$proposed_shipment = new ProposedShipment([
'type' => 'default',
'order_id' => 10,
'title' => 'Test shipment',
'items' => [
'invalid',
],
'shipping_profile' => $this
->prophesize(ProfileInterface::class)
->reveal(),
'package_type_id' => 'default',
]);
}
}