You are here

class USPSRateRequestTest in Commerce USPS 8

Class USPSRateRequestTest.

@coversDefaultClass \Drupal\commerce_usps\USPSRateRequest @group commerce_usps

Hierarchy

Expanded class hierarchy of USPSRateRequestTest

File

tests/src/Unit/USPSRateRequestTest.php, line 17

Namespace

Drupal\Tests\commerce_usps\Unit
View source
class USPSRateRequestTest extends USPSUnitTestBase {

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    // Add the services to the config.
    $this
      ->setConfig([
      'services' => [
        1,
        2,
        3,
        4,
        6,
        7,
      ],
    ]);

    // Mock all the objects and set the config.
    $event_dispatcher = new EventDispatcher();
    $this->uspsShipment = new USPSShipment($event_dispatcher);
    $this->rateRequest = new USPSRateRequest($this->uspsShipment, $event_dispatcher);
    $this->rateRequest
      ->setConfig($this
      ->getConfig());
  }

  /**
   * Tests getRates().
   *
   * @covers ::getRates
   * @covers ::buildRate
   * @covers ::setMode
   * @covers ::setShipment
   * @covers ::resolveRates
   */
  public function testGetRates() {
    $config = $this
      ->getConfig();
    $shipment = $this
      ->mockShipment();
    $shipping_method = $this
      ->prophesize(ShippingMethodInterface::class);
    $shipping_method
      ->id()
      ->willReturn('123456789');

    // Fetch rates from the USPS api.
    $rates = $this->rateRequest
      ->getRates($shipment, $shipping_method
      ->reveal());

    // Make sure the same number of rates requested
    // is returned.
    $this
      ->assertEquals(count($config['services']), count($rates));

    /** @var \Drupal\commerce_shipping\ShippingRate $rate */
    foreach ($rates as $rate) {
      $this
        ->assertInstanceOf(ShippingRate::class, $rate);
      $this
        ->assertNotEmpty($rate
        ->getAmount()
        ->getNumber());
    }
  }

  /**
   * Test getRates() with commercial rate response.
   *
   * @throws \Exception
   */
  public function testCommercialRates() {
    $shipment = $this
      ->mockShipment();
    $shipping_method = $this
      ->prophesize(ShippingMethodInterface::class);
    $shipping_method
      ->id()
      ->willReturn('123456789');

    // Fetch a retail rate first.
    $config_update = [
      'services' => [
        3,
      ],
      'rate_options' => [
        'rate_class' => 'retail',
      ],
    ];
    $this
      ->setConfig($config_update);
    $config = $this
      ->getConfig();
    $this->uspsShipment
      ->setConfig($config);
    $this->rateRequest
      ->setConfig($config);
    $retail_rates = $this->rateRequest
      ->getRates($shipment, $shipping_method
      ->reveal());

    // Then fetch a commercial rate for the same service.
    $config_update = [
      'services' => [
        3,
      ],
      'rate_options' => [
        'rate_class' => 'commercial',
      ],
    ];
    $this
      ->setConfig($config_update);
    $config = $this
      ->getConfig();

    // Pass the config to the rate and shipment services.
    $this->uspsShipment
      ->setConfig($config);
    $this->rateRequest
      ->setConfig($config);
    $commercial_rates = $this->rateRequest
      ->getRates($shipment, $shipping_method
      ->reveal());

    // Make sure both return rates.
    $this
      ->assertEquals(count($retail_rates), count($commercial_rates));

    /** @var \Drupal\commerce_shipping\ShippingRate $rate */
    foreach ($commercial_rates as $delta => $commercial_rate) {

      // Ensure a commercial rate was returned.
      $this
        ->assertNotEmpty($commercial_rate
        ->getAmount()
        ->getNumber());

      // Ensure the commercial rate is less than the retail rate.
      $this
        ->assertLessThan($retail_rates[$delta]
        ->getAmount()
        ->getNumber(), $commercial_rate
        ->getAmount()
        ->getNumber());
    }
  }

  /**
   * Tests returning a single rate response.
   *
   * See https://www.drupal.org/project/commerce_usps/issues/3064571
   */
  public function testSingleRate() {
    $this
      ->setConfig([
      'services' => [
        9,
      ],
    ]);
    $config = $this
      ->getConfig();
    $shipment = $this
      ->mockShipment([], [
      'length' => 30,
      'width' => 20,
      'height' => 20,
    ]);
    $shipping_method = $this
      ->prophesize(ShippingMethodInterface::class);
    $shipping_method
      ->id()
      ->willReturn('123456789');

    // Fetch rates from the USPS api.
    $rates = $this->rateRequest
      ->getRates($shipment, $shipping_method
      ->reveal());

    // Make sure the same number of rates requested
    // is returned.
    $this
      ->assertEquals(count($config['services']), count($rates));

    /** @var \Drupal\commerce_shipping\ShippingRate $rate */
    foreach ($rates as $rate) {
      $this
        ->assertInstanceOf(ShippingRate::class, $rate);
      $this
        ->assertNotEmpty($rate
        ->getAmount()
        ->getNumber());
    }
  }

  /**
   * Test cleaning service names.
   *
   * @covers ::cleanServiceName
   */
  public function testCleanServiceName() {
    $service = 'Priority Mail Express 2-Day<sup>™</sup>';
    $cleaned = $this->rateRequest
      ->cleanServiceName($service);
    $this
      ->assertEquals('Priority Mail Express 2-Day', $cleaned);
  }

  /**
   * Test package setup.
   *
   * @covers ::getPackages
   */
  public function testGetPackages() {
    $this->rateRequest
      ->setShipment($this
      ->mockShipment());
    $packages = $this->rateRequest
      ->getPackages();

    // TODO: Support multiple packages.

    /** @var \USPS\RatePackage $package */
    $package = reset($packages);
    $info = $package
      ->getPackageInfo();
    $this
      ->assertEquals(28806, $info['ZipOrigination']);
    $this
      ->assertEquals(80465, $info['ZipDestination']);
    $this
      ->assertEquals('ALL', $info['Service']);
    $this
      ->assertEquals(10, $info['Pounds']);
    $this
      ->assertEquals(0, $info['Ounces']);
    $this
      ->assertEquals('VARIABLE', $info['Container']);
    $this
      ->assertEquals('REGULAR', $info['Size']);
    $this
      ->assertEquals(3, $info['Width']);
    $this
      ->assertEquals(10, $info['Length']);
    $this
      ->assertEquals(10, $info['Height']);
    $this
      ->assertEquals(0, $info['Girth']);
    $this
      ->assertEquals(TRUE, $info['Machinable']);
    $this
      ->assertEquals(date('Y-m-d'), $info['ShipDate']);
  }

}

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.
USPSRateRequestTest::setUp public function Overrides USPSUnitTestBase::setUp
USPSRateRequestTest::testCleanServiceName public function Test cleaning service names.
USPSRateRequestTest::testCommercialRates public function Test getRates() with commercial rate response.
USPSRateRequestTest::testGetPackages public function Test package setup.
USPSRateRequestTest::testGetRates public function Tests getRates().
USPSRateRequestTest::testSingleRate public function Tests returning a single rate response.
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.