You are here

public function USPSUnitTestBase::mockShipment in Commerce USPS 8

Creates a mock Drupal Commerce shipment entity.

Parameters

array $weight: A weight array keyed by weight and unit.

array $dimensions: A dimensions array keyed by length, width, height, and unit.

bool $domestic: FALSE for an intenrational shipment.

Return value

\Drupal\commerce_shipping\Entity\ShipmentInterface A mocked commerce shipment object.

7 calls to USPSUnitTestBase::mockShipment()
USPSInternationalRateRequestTest::testCommercialRates in tests/src/Unit/USPSInternationalRateRequestTest.php
Test getRates() with commercial rate response.
USPSInternationalRateRequestTest::testGetPackages in tests/src/Unit/USPSInternationalRateRequestTest.php
Test package setup.
USPSInternationalRateRequestTest::testGetRates in tests/src/Unit/USPSInternationalRateRequestTest.php
Tests getRates().
USPSRateRequestTest::testCommercialRates in tests/src/Unit/USPSRateRequestTest.php
Test getRates() with commercial rate response.
USPSRateRequestTest::testGetPackages in tests/src/Unit/USPSRateRequestTest.php
Test package setup.

... See full list

File

tests/src/Unit/USPSUnitTestBase.php, line 96

Class

USPSUnitTestBase
Class USPSUnitTestBase.

Namespace

Drupal\Tests\commerce_usps\Unit

Code

public function mockShipment($weight = [], $dimensions = [], $domestic = TRUE) {

  // Ensure default values for weight and dimensions.
  $weight = $weight + [
    'weight' => 10,
    'unit' => 'lb',
  ];
  $dimensions = $dimensions + [
    'length' => 10,
    'width' => 3,
    'height' => 10,
    'unit' => 'in',
  ];

  // Mock a Drupal Commerce Order and associated objects.
  $order = $this
    ->prophesize(OrderInterface::class);
  $store = $this
    ->prophesize(StoreInterface::class);
  $store
    ->getAddress()
    ->willReturn(new Address('US', 'NC', 'Asheville', '', 28806, '', '1025 Brevard Rd'));
  $order
    ->getStore()
    ->willReturn($store
    ->reveal());

  // Mock a Drupal Commerce shipment and associated objects.
  $shipment = $this
    ->prophesize(ShipmentInterface::class);
  $profile = $this
    ->prophesize(ProfileInterface::class);
  $address_list = $this
    ->prophesize(FieldItemListInterface::class);

  // Mock the address list to return a US address.
  if ($domestic) {
    $address_list
      ->first()
      ->willReturn(new Address('US', 'CO', 'Morrison', '', 80465, '', '18300 W Alameda Pkwy'));
  }
  else {
    $address_list
      ->first()
      ->willReturn(new Address('GB', 'London', 'Pimlico', '', 'SW1V 3EN', '', '113 Lupus St.'));
  }
  $profile
    ->get('address')
    ->willReturn($address_list
    ->reveal());
  $shipment
    ->getShippingProfile()
    ->willReturn($profile
    ->reveal());
  $shipment
    ->getOrder()
    ->willReturn($order
    ->reveal());

  // Mock a package type including dimensions and remote id.
  $package_type = $this
    ->prophesize(PackageTypeInterface::class);
  $package_type
    ->getHeight()
    ->willReturn((new Length($dimensions['height'], 'in'))
    ->convert($dimensions['unit']));
  $package_type
    ->getLength()
    ->willReturn((new Length($dimensions['length'], 'in'))
    ->convert($dimensions['unit']));
  $package_type
    ->getWidth()
    ->willReturn((new Length($dimensions['width'], 'in'))
    ->convert($dimensions['unit']));
  $package_type
    ->getRemoteId()
    ->willReturn('custom');

  // Mock the shipments weight and package type.
  $shipment
    ->getWeight()
    ->willReturn((new Weight($weight['weight'], 'lb'))
    ->convert($weight['unit']));
  $shipment
    ->getPackageType()
    ->willReturn($package_type
    ->reveal());

  // Return the mocked shipment object.
  return $shipment
    ->reveal();
}