You are here

public function ShipmentManagerTest::testCalculateRates in Commerce Shipping 8.2

Tests calculating rates.

@covers ::calculateRates

File

tests/src/Kernel/ShipmentManagerTest.php, line 198

Class

ShipmentManagerTest
Tests the shipment manager.

Namespace

Drupal\Tests\commerce_shipping\Kernel

Code

public function testCalculateRates() {

  // Use the FR translation where available (e.g. $another_shipping_method).
  $this->container
    ->get('language.default')
    ->set(ConfigurableLanguage::createFromLangcode('fr'));
  $rates = $this->shipmentManager
    ->calculateRates($this->shipment);
  $this
    ->assertCount(2, $rates);
  $first_rate = reset($rates);
  $second_rate = end($rates);
  $this
    ->assertArrayHasKey($first_rate
    ->getId(), $rates);
  $this
    ->assertEquals('2', $first_rate
    ->getShippingMethodId());
  $this
    ->assertEquals('default', $first_rate
    ->getService()
    ->getId());
  $this
    ->assertEquals('Le overnight shipping', $first_rate
    ->getService()
    ->getLabel());
  $this
    ->assertEquals(new Price('22.00', 'USD'), $first_rate
    ->getOriginalAmount());
  $this
    ->assertEquals(new Price('22.00', 'USD'), $first_rate
    ->getAmount());
  $this
    ->assertArrayHasKey($second_rate
    ->getId(), $rates);
  $this
    ->assertEquals('1', $second_rate
    ->getShippingMethodId());
  $this
    ->assertEquals('default', $second_rate
    ->getService()
    ->getId());
  $this
    ->assertEquals('Standard shipping', $second_rate
    ->getService()
    ->getLabel());
  $this
    ->assertEquals(new Price('5.00', 'USD'), $second_rate
    ->getOriginalAmount());
  $this
    ->assertEquals(new Price('5.00', 'USD'), $second_rate
    ->getAmount());

  // Test rate altering.
  $this->shipment
    ->setData('alter_rate', TRUE);
  $rates = $this->shipmentManager
    ->calculateRates($this->shipment);
  $this
    ->assertCount(2, $rates);
  $first_rate = reset($rates);
  $second_rate = end($rates);
  $this
    ->assertArrayHasKey($first_rate
    ->getId(), $rates);
  $this
    ->assertEquals('2', $first_rate
    ->getShippingMethodId());
  $this
    ->assertEquals('default', $first_rate
    ->getService()
    ->getId());
  $this
    ->assertEquals('Le overnight shipping', $first_rate
    ->getService()
    ->getLabel());
  $this
    ->assertEquals(new Price('22.00', 'USD'), $first_rate
    ->getOriginalAmount());
  $this
    ->assertEquals(new Price('44.00', 'USD'), $first_rate
    ->getAmount());
  $this
    ->assertArrayHasKey($second_rate
    ->getId(), $rates);
  $this
    ->assertEquals('1', $second_rate
    ->getShippingMethodId());
  $this
    ->assertEquals('default', $second_rate
    ->getService()
    ->getId());
  $this
    ->assertEquals('Standard shipping', $second_rate
    ->getService()
    ->getLabel());
  $this
    ->assertEquals(new Price('5.00', 'USD'), $second_rate
    ->getOriginalAmount());
  $this
    ->assertEquals(new Price('10.00', 'USD'), $second_rate
    ->getAmount());
}