You are here

public function ShipmentFixedAmountOffTest::testNonDisplayInclusive in Commerce Shipping 8.2

Tests the non-display-inclusive offer.

@covers ::applyToShipment

File

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

Class

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

Namespace

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

Code

public function testNonDisplayInclusive() {
  $offer = $this->promotion
    ->getOffer();
  $offer_configuration = $offer
    ->getConfiguration();
  $offer_configuration['display_inclusive'] = FALSE;
  $offer
    ->setConfiguration($offer_configuration);
  $this->promotion
    ->setDisplayName('$11 off');
  $this->promotion
    ->setOffer($offer);
  $this->promotion
    ->save();
  $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('5.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('$11 off', $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
    ->assertFalse($adjustment
    ->isIncluded());
  $second_shipment = end($shipments);
  $this
    ->assertEquals(new Price('20.00', 'USD'), $second_shipment
    ->getOriginalAmount());
  $this
    ->assertEquals(new Price('20.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('$11 off', $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
    ->assertFalse($adjustment
    ->isIncluded());
}