You are here

class ProposedShipmentTest in Commerce Shipping 8.2

@coversDefaultClass \Drupal\commerce_shipping\ProposedShipment @group commerce_shipping

Hierarchy

Expanded class hierarchy of ProposedShipmentTest

File

tests/src/Unit/ProposedShipmentTest.php, line 16

Namespace

Drupal\Tests\commerce_shipping\Unit
View source
class ProposedShipmentTest extends UnitTestCase {

  /**
   * The proposed shipment.
   *
   * @var \Drupal\commerce_shipping\ProposedShipment
   */
  protected $proposedShipment;

  /**
   * {@inheritdoc}
   */
  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',
      ],
    ]);
  }

  /**
   * @covers ::getType
   */
  public function testGetType() {
    $this
      ->assertEquals('default', $this->proposedShipment
      ->getType());
  }

  /**
   * @covers ::getOrderId
   */
  public function testGetOrderId() {
    $this
      ->assertEquals(10, $this->proposedShipment
      ->getOrderId());
  }

  /**
   * @covers ::getTitle
   */
  public function testGetTitle() {
    $this
      ->assertEquals('Shipment from Narnia', $this->proposedShipment
      ->getTitle());
  }

  /**
   * @covers ::getItems
   */
  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);
  }

  /**
   * @covers ::getShippingProfile
   */
  public function testGetShippingProfile() {
    $this
      ->assertEquals(11, $this->proposedShipment
      ->getShippingProfile()
      ->id());
  }

  /**
   * @covers ::getPackageTypeId
   */
  public function testGetPackageTypeId() {
    $this
      ->assertEquals('default', $this->proposedShipment
      ->getPackageTypeId());
  }

  /**
   * @covers ::getCustomFields
   */
  public function testGetCustomFields() {
    $this
      ->assertEquals([
      'field_test' => 'value',
    ], $this->proposedShipment
      ->getCustomFields());
  }

  /**
   * @covers ::__construct
   */
  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',
    ]);
  }

  /**
   * @covers ::__construct
   */
  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',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
ProposedShipmentTest::$proposedShipment protected property The proposed shipment.
ProposedShipmentTest::setUp protected function Overrides UnitTestCase::setUp
ProposedShipmentTest::testGetCustomFields public function @covers ::getCustomFields
ProposedShipmentTest::testGetItems public function @covers ::getItems
ProposedShipmentTest::testGetOrderId public function @covers ::getOrderId
ProposedShipmentTest::testGetPackageTypeId public function @covers ::getPackageTypeId
ProposedShipmentTest::testGetShippingProfile public function @covers ::getShippingProfile
ProposedShipmentTest::testGetTitle public function @covers ::getTitle
ProposedShipmentTest::testGetType public function @covers ::getType
ProposedShipmentTest::testInvalidItems public function @covers ::__construct
ProposedShipmentTest::testMissingProperties public function @covers ::__construct
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.