You are here

public function ShipmentFixedAmountOffTest::testDisplayInclusive in Commerce Shipping 8.2

Tests the display-inclusive offer.

@covers ::applyToShipment

File

tests/src/Kernel/Plugin/Commerce/PromotionOffer/ShipmentFixedAmountOffTest.php, line 183

Class

ShipmentFixedAmountOffTest
Tests the "Fixed amount off the shipment amount" offer.

Namespace

Drupal\Tests\commerce_shipping\Kernel\Plugin\Commerce\PromotionOffer

Code

public function testDisplayInclusive() {
  $this
    ->assertCount(0, $this->order
    ->collectAdjustments());
  $this
    ->assertEquals(new Price('20.00', 'USD'), $this->order
    ->getTotalPrice());
  $this->order
    ->setRefreshState(Order::REFRESH_ON_SAVE);
  $this->order
    ->save();

  // Confirm that both shipments were discounted.

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
  $shipments = $this->order
    ->get('shipments')
    ->referencedEntities();
  $first_shipment = reset($shipments);
  $this
    ->assertEquals(new Price('5.00', 'USD'), $first_shipment
    ->getOriginalAmount());
  $this
    ->assertEquals(new Price('0.00', 'USD'), $first_shipment
    ->getAmount());
  $this
    ->assertEquals(new Price('0.00', 'USD'), $first_shipment
    ->getAdjustedAmount());
  $adjustments = $first_shipment
    ->getAdjustments();
  $this
    ->assertCount(1, $adjustments);
  $adjustment = reset($adjustments);
  $this
    ->assertEquals('shipping_promotion', $adjustment
    ->getType());
  $this
    ->assertEquals('Discount', $adjustment
    ->getLabel());

  // Confirm that the adjustment amount is equal to the remaining shipment
  // amount at the time of application.
  $this
    ->assertEquals(new Price('-5.00', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals($this->promotion
    ->id(), $adjustment
    ->getSourceId());
  $this
    ->assertTrue($adjustment
    ->isIncluded());
  $second_shipment = end($shipments);
  $this
    ->assertEquals(new Price('20.00', 'USD'), $second_shipment
    ->getOriginalAmount());
  $this
    ->assertEquals(new Price('9.00', 'USD'), $second_shipment
    ->getAmount());
  $this
    ->assertEquals(new Price('9.00', 'USD'), $second_shipment
    ->getAdjustedAmount());
  $adjustments = $second_shipment
    ->getAdjustments();
  $this
    ->assertCount(1, $adjustments);
  $adjustment = reset($adjustments);
  $this
    ->assertEquals('shipping_promotion', $adjustment
    ->getType());
  $this
    ->assertEquals('Discount', $adjustment
    ->getLabel());

  // Confirm that the adjustment amount matches the offer amount.
  $this
    ->assertEquals(new Price('-11.00', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals($this->promotion
    ->id(), $adjustment
    ->getSourceId());
  $this
    ->assertTrue($adjustment
    ->isIncluded());

  // Confirm that the adjustments were transferred to the order.
  $this
    ->assertCount(4, $this->order
    ->collectAdjustments());
  $this
    ->assertCount(2, $this->order
    ->collectAdjustments([
    'shipping',
  ]));
  $this
    ->assertCount(2, $this->order
    ->collectAdjustments([
    'shipping_promotion',
  ]));
  $this
    ->assertEquals(new Price('29.00', 'USD'), $this->order
    ->getTotalPrice());

  // Confirm that it is possible to discount only a single shipment.
  $offer = $this->promotion
    ->getOffer();
  $offer_configuration = $offer
    ->getConfiguration();
  $offer_configuration['filter'] = 'exclude';
  $offer_configuration['shipping_methods'] = [
    [
      'shipping_method' => $first_shipment
        ->getShippingMethod()
        ->uuid(),
    ],
  ];
  $offer
    ->setConfiguration($offer_configuration);
  $this->promotion
    ->setOffer($offer);
  $this->promotion
    ->save();
  $this->order
    ->setRefreshState(Order::REFRESH_ON_SAVE);
  $this->order
    ->save();

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $first_shipment */
  $first_shipment = $this
    ->reloadEntity($first_shipment);
  $this
    ->assertEquals(new Price('5.00', 'USD'), $first_shipment
    ->getOriginalAmount());
  $this
    ->assertEquals(new Price('5.00', 'USD'), $first_shipment
    ->getAmount());
  $this
    ->assertEquals(new Price('5.00', 'USD'), $first_shipment
    ->getAdjustedAmount());
  $this
    ->assertCount(0, $first_shipment
    ->getAdjustments());

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $second_shipment */
  $second_shipment = $this
    ->reloadEntity($second_shipment);
  $this
    ->assertEquals(new Price('20.00', 'USD'), $second_shipment
    ->getOriginalAmount());
  $this
    ->assertEquals(new Price('9.00', 'USD'), $second_shipment
    ->getAmount());
  $this
    ->assertEquals(new Price('9.00', 'USD'), $second_shipment
    ->getAdjustedAmount());
  $this
    ->assertCount(1, $second_shipment
    ->getAdjustments());

  // Confirm that the adjustments were transferred to the order.
  $this
    ->assertCount(3, $this->order
    ->collectAdjustments());
  $this
    ->assertCount(2, $this->order
    ->collectAdjustments([
    'shipping',
  ]));
  $this
    ->assertCount(1, $this->order
    ->collectAdjustments([
    'shipping_promotion',
  ]));
  $this
    ->assertEquals(new Price('34.00', 'USD'), $this->order
    ->getTotalPrice());
}