You are here

public function USPSRateRequestTest::testCommercialRates in Commerce USPS 8

Test getRates() with commercial rate response.

Throws

\Exception

File

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

Class

USPSRateRequestTest
Class USPSRateRequestTest.

Namespace

Drupal\Tests\commerce_usps\Unit

Code

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());
  }
}