You are here

abstract class USPSUnitTestBase in Commerce USPS 8

Class USPSUnitTestBase.

@package Drupal\Tests\commerce_usps\Unit

Hierarchy

Expanded class hierarchy of USPSUnitTestBase

File

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

Namespace

Drupal\Tests\commerce_usps\Unit
View source
abstract class USPSUnitTestBase extends UnitTestCase {

  /**
   * Configuration array.
   *
   * @var array
   */
  protected $configuration;

  /**
   * The USPS Rate Request class.
   *
   * @var \Drupal\commerce_usps\USPSRateRequest
   */
  protected $rateRequest;

  /**
   * The USPS shipment class.
   *
   * @var \Drupal\commerce_usps\USPSShipment
   */
  protected $uspsShipment;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->setConfig();
  }

  /**
   * Mocks the configuration array for tests.
   *
   * @param array $config
   *   The shipping method plugin configuration.
   */
  protected function setConfig(array $config = []) {
    $this->configuration = $config + [
      'api_information' => [
        'user_id' => '972BLUEO5743',
        'password' => '972BLUEO5743',
        'mode' => 'live',
      ],
      'rate_options' => [
        'rate_class' => 'retail',
      ],
      'default_package_type' => 'usps_small_flat_rate_box',
    ];
  }

  /**
   * Get the configuration array.
   *
   * @return array
   *   The config.
   */
  public function getConfig() {
    return $this->configuration;
  }

  /**
   * Creates a mock Drupal Commerce shipment entity.
   *
   * @param array $weight
   *   A weight array keyed by weight and unit.
   * @param array $dimensions
   *   A dimensions array keyed by length, width, height, and unit.
   * @param bool $domestic
   *   FALSE for an intenrational shipment.
   *
   * @return \Drupal\commerce_shipping\Entity\ShipmentInterface
   *   A mocked commerce shipment object.
   */
  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();
  }

  /**
   * Mocks a shipping method.
   *
   * @return \Drupal\commerce_shipping\Plugin\Commerce\ShippingMethod\ShippingMethodInterface
   *   The mocked shipping method.
   */
  public function mockShippingMethod() {
    $shipping_method = $this
      ->prophesize(ShippingMethodInterface::class);
    $package_type = $this
      ->prophesize(PackageTypeInterface::class);
    $package_type
      ->getHeight()
      ->willReturn(new Length(10, 'in'));
    $package_type
      ->getLength()
      ->willReturn(new Length(10, 'in'));
    $package_type
      ->getWidth()
      ->willReturn(new Length(3, 'in'));
    $package_type
      ->getWeight()
      ->willReturn(new Weight(10, 'lb'));
    $package_type
      ->getRemoteId()
      ->willReturn('custom');
    $shipping_method
      ->getDefaultPackageType()
      ->willReturn($package_type);
    return $shipping_method
      ->reveal();
  }

}

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.
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.
USPSUnitTestBase::$configuration protected property Configuration array.
USPSUnitTestBase::$rateRequest protected property The USPS Rate Request class.
USPSUnitTestBase::$uspsShipment protected property The USPS shipment class.
USPSUnitTestBase::getConfig public function Get the configuration array.
USPSUnitTestBase::mockShipment public function Creates a mock Drupal Commerce shipment entity.
USPSUnitTestBase::mockShippingMethod public function Mocks a shipping method.
USPSUnitTestBase::setConfig protected function Mocks the configuration array for tests.
USPSUnitTestBase::setUp protected function Overrides UnitTestCase::setUp 2