You are here

class UPSRateRequestTest in Commerce UPS 8.3

Class UPSRateRequestTest.

@coversDefaultClass \Drupal\commerce_ups\UPSRateRequest @group commerce_ups

Hierarchy

Expanded class hierarchy of UPSRateRequestTest

File

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

Namespace

Drupal\Tests\commerce_ups\Unit
View source
class UPSRateRequestTest extends UPSUnitTestBase {

  /**
   * A UPS rate request object.
   *
   * @var \Drupal\commerce_ups\UPSRateRequest
   */
  protected $rateRequest;

  /**
   * Set up requirements for test.
   */
  public function setUp() {
    parent::setUp();
    $logger_factory = $this
      ->prophesize(LoggerChannelFactoryInterface::class);
    $logger = $this
      ->prophesize(LoggerChannelInterface::class);
    $logger_factory
      ->get(UPSRateRequestInterface::LOGGER_CHANNEL)
      ->willReturn($logger
      ->reveal());
    $this->rateRequest = new UPSRateRequest(new UPSShipment(), $logger_factory
      ->reveal());
    $this->rateRequest
      ->setConfig($this->configuration);
  }

  /**
   * Test getAuth response.
   *
   * @covers ::getAuth
   */
  public function testAuth() {
    $auth = $this->rateRequest
      ->getAuth();
    $this
      ->assertEquals($auth['access_key'], $this->configuration['api_information']['access_key']);
    $this
      ->assertEquals($auth['user_id'], $this->configuration['api_information']['user_id']);
    $this
      ->assertEquals($auth['password'], $this->configuration['api_information']['password']);
  }

  /**
   * Test useIntegrationMode().
   *
   * @covers ::useIntegrationMode
   */
  public function testIntegrationMode() {
    $mode = $this->rateRequest
      ->useIntegrationMode();
    $this
      ->assertEquals(TRUE, $mode);
  }

  /**
   * Test getRateType().
   *
   * @covers ::getRateType
   */
  public function testRateType() {
    $type = $this->rateRequest
      ->getRateType();
    $this
      ->assertEquals(TRUE, $type);
  }

  /**
   * Test rate requests return valid rates.
   *
   * @param string $weight_unit
   *   Weight unit.
   * @param string $length_unit
   *   Length unit.
   * @param bool $send_from_usa
   *   Whether the shipment should be sent from USA.
   *
   * @covers ::getRates
   *
   * @dataProvider measurementUnitsDataProvider
   */
  public function testRateRequest($weight_unit, $length_unit, $send_from_usa) {

    // Invoke the rate request object.
    $shipment = $this
      ->mockShipment($weight_unit, $length_unit, $send_from_usa);
    $shipping_method_plugin = $this
      ->mockShippingMethod();
    $shipping_method_entity = $this
      ->prophesize(ShippingMethodInterface::class);
    $shipping_method_entity
      ->id()
      ->willReturn('123456789');
    $shipping_method_entity
      ->getPlugin()
      ->willReturn($shipping_method_plugin);
    $rates = $this->rateRequest
      ->getRates($shipment, $shipping_method_entity
      ->reveal());

    // Make sure at least one rate was returned.
    $this
      ->assertArrayHasKey(0, $rates);
    foreach ($rates as $rate) {
      assert($rate instanceof ShippingRate);
      $this
        ->assertGreaterThan(0, $rate
        ->getAmount()
        ->getNumber());
      $this
        ->assertGreaterThan(0, $rate
        ->getOriginalAmount()
        ->getNumber());
      $this
        ->assertEquals($rate
        ->getAmount()
        ->getCurrencyCode(), $send_from_usa ? 'USD' : 'EUR');
      $this
        ->assertNotEmpty($rate
        ->getService()
        ->getLabel());
      $this
        ->assertEquals('123456789', $rate
        ->getShippingMethodId());
    }
  }

  /**
   * Data provider for testRateRequest()
   */
  public function measurementUnitsDataProvider() {
    $weight_units = [
      WeightUnit::GRAM,
      WeightUnit::KILOGRAM,
      WeightUnit::OUNCE,
      WeightUnit::POUND,
    ];
    $length_units = [
      LengthUnit::MILLIMETER,
      LengthUnit::CENTIMETER,
      LengthUnit::METER,
      LengthUnit::INCH,
      LengthUnit::FOOT,
    ];
    foreach ($weight_units as $weight_unit) {
      foreach ($length_units as $length_unit) {
        (yield [
          $weight_unit,
          $length_unit,
          TRUE,
        ]);
        (yield [
          $weight_unit,
          $length_unit,
          FALSE,
        ]);
      }
    }
  }

}

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.
UPSRateRequestTest::$rateRequest protected property A UPS rate request object.
UPSRateRequestTest::measurementUnitsDataProvider public function Data provider for testRateRequest()
UPSRateRequestTest::setUp public function Set up requirements for test. Overrides UPSUnitTestBase::setUp
UPSRateRequestTest::testAuth public function Test getAuth response.
UPSRateRequestTest::testIntegrationMode public function Test useIntegrationMode().
UPSRateRequestTest::testRateRequest public function Test rate requests return valid rates.
UPSRateRequestTest::testRateType public function Test getRateType().
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.