You are here

abstract class UPSUnitTestBase in Commerce UPS 8.3

Class UPSUnitTestBase.

@package Drupal\Tests\commerce_ups\Unit

Hierarchy

Expanded class hierarchy of UPSUnitTestBase

File

tests/src/Unit/UPSUnitTestBase.php, line 23

Namespace

Drupal\Tests\commerce_ups\Unit
View source
abstract class UPSUnitTestBase extends UnitTestCase {

  /**
   * A shipping method configuration array.
   *
   * @var array
   */
  protected $configuration;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->configuration = [
      'api_information' => [
        'access_key' => '5D3096C1F3C7953D',
        'user_id' => 'drupal_commerce',
        'password' => 'DCDev101!',
        'mode' => 'test',
      ],
      'rate_options' => [
        'rate_type' => TRUE,
      ],
      'services' => [
        "01" => "01",
        "02" => "02",
        "03" => "03",
        "07" => "07",
        "08" => "08",
        "11" => "11",
        "12" => "12",
        "13" => "13",
        "14" => "14",
        "54" => "54",
        "59" => "59",
        "65" => "65",
        "70" => "70",
      ],
    ];
  }

  /**
   * Creates a mock Drupal Commerce shipment entity.
   *
   * @param string $weight_unit
   *   Weight measurement unit.
   * @param string $length_unit
   *   Length measurement unit.
   * @param bool $send_form_usa
   *   Whether the shipment should be sent from USA.
   *
   * @return \Drupal\commerce_shipping\Entity\ShipmentInterface
   *   A mocked commerce shipment object.
   */
  public function mockShipment($weight_unit = 'lb', $length_unit = 'in', $send_form_usa = TRUE) {

    // Mock a Drupal Commerce Order and associated objects.
    $order = $this
      ->prophesize(OrderInterface::class);
    $store = $this
      ->prophesize(StoreInterface::class);

    // Mock the getAddress method to return a US address.
    if ($send_form_usa) {
      $store
        ->getAddress()
        ->willReturn(new Address('US', 'NC', 'Asheville', '', 28806, '', '1025 Brevard Rd'));
    }
    else {

      // Mock the address list to ship to Germany address.
      // To those who are wondering, this is where Drupal Europe 2018 took
      // place.
      $store
        ->getAddress()
        ->willReturn(new Address('DE', '', 'Darmstadt', '', 64283, '', 'Schlossgraben 1'));
    }
    $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.
    $address_list
      ->first()
      ->willReturn(new Address('US', 'NC', 'Asheville', '', 28806, '', '1025 Brevard Rd'));
    $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(10, 'in'))
      ->convert($length_unit));
    $package_type
      ->getLength()
      ->willReturn((new Length(10, 'in'))
      ->convert($length_unit));
    $package_type
      ->getWidth()
      ->willReturn((new Length(3, 'in'))
      ->convert($length_unit));
    $package_type
      ->getRemoteId()
      ->willReturn(PackagingType::PT_UNKNOWN);

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

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

  /**
   * Creates a mock Drupal Commerce 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.
UPSUnitTestBase::$configuration protected property A shipping method configuration array.
UPSUnitTestBase::mockShipment public function Creates a mock Drupal Commerce shipment entity.
UPSUnitTestBase::mockShippingMethod public function Creates a mock Drupal Commerce shipping method.
UPSUnitTestBase::setUp protected function Overrides UnitTestCase::setUp 2