public function ShipmentPercentageOffTest::testDisplayInclusive in Commerce Shipping 8.2
Tests the display-inclusive offer.
@covers ::applyToShipment
File
- tests/
src/ Kernel/ Plugin/ Commerce/ PromotionOffer/ ShipmentPercentageOffTest.php, line 187
Class
- ShipmentPercentageOffTest
- Tests the "Percentage off the shipment amount" offer.
Namespace
Drupal\Tests\commerce_shipping\Kernel\Plugin\Commerce\PromotionOfferCode
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('3.00', 'USD'), $first_shipment
->getAmount());
$this
->assertEquals(new Price('0.00', 'USD'), $first_shipment
->getAdjustedAmount());
$adjustments = $first_shipment
->getAdjustments();
$this
->assertCount(2, $adjustments);
$adjustment = end($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('-2.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('10.00', 'USD'), $second_shipment
->getAmount());
$this
->assertEquals(new Price('10.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());
$this
->assertEquals(new Price('-10.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(5, $this->order
->collectAdjustments());
$this
->assertCount(2, $this->order
->collectAdjustments([
'shipping',
]));
$this
->assertCount(3, $this->order
->collectAdjustments([
'shipping_promotion',
]));
$this
->assertEquals(new Price('30.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'] = 'include';
$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('3.00', 'USD'), $first_shipment
->getAmount());
$this
->assertEquals(new Price('0.00', 'USD'), $first_shipment
->getAdjustedAmount());
$this
->assertCount(2, $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('20.00', 'USD'), $second_shipment
->getAmount());
$this
->assertEquals(new Price('20.00', 'USD'), $second_shipment
->getAdjustedAmount());
$this
->assertCount(0, $second_shipment
->getAdjustments());
// Confirm that the adjustments were transferred to the order.
$this->order = $this
->reloadEntity($this->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('40.00', 'USD'), $this->order
->getTotalPrice());
}