You are here

public function BuyXGetYTest::testFixedAmountOff in Commerce Core 8.2

Tests the fixed amount off offer type.

@covers ::apply

File

modules/promotion/tests/src/Kernel/Plugin/Commerce/PromotionOffer/BuyXGetYTest.php, line 203

Class

BuyXGetYTest
Tests the "Buy X Get Y" offer.

Namespace

Drupal\Tests\commerce_promotion\Kernel\Plugin\Commerce\PromotionOffer

Code

public function testFixedAmountOff() {

  /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
  $order_item_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_order_item');
  $first_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[0], [
    'quantity' => '7',
  ]);
  $first_order_item
    ->save();
  $second_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[1], [
    'quantity' => '2',
  ]);

  // Test having a single offer order item, quantity < get_quantity.
  $third_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[2], [
    'quantity' => '3',
  ]);
  $second_order_item
    ->save();
  $this->order
    ->setItems([
    $first_order_item,
    $second_order_item,
    $third_order_item,
  ]);
  $this->order
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  list($first_order_item, $second_order_item, $third_order_item) = $this->order
    ->getItems();
  $this
    ->assertCount(0, $first_order_item
    ->getAdjustments());
  $this
    ->assertCount(0, $second_order_item
    ->getAdjustments());
  $this
    ->assertCount(1, $third_order_item
    ->getAdjustments());
  $adjustments = $third_order_item
    ->getAdjustments();
  $adjustment = reset($adjustments);
  $this
    ->assertEquals('promotion', $adjustment
    ->getType());
  $this
    ->assertEquals('Discount', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-3', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals($this->promotion
    ->id(), $adjustment
    ->getSourceId());

  // Test having two offer order items, one ($third_order_item) reduced
  // completely, the other ($fourth_order_item) reduced partially.
  $fourth_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[2], [
    'quantity' => '2',
  ]);
  $this->order
    ->addItem($fourth_order_item);
  $this->order
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  list($first_order_item, $second_order_item, $third_order_item, $fourth_order_item) = $this->order
    ->getItems();
  $this
    ->assertCount(0, $first_order_item
    ->getAdjustments());
  $this
    ->assertCount(0, $second_order_item
    ->getAdjustments());
  $this
    ->assertCount(1, $third_order_item
    ->getAdjustments());
  $this
    ->assertCount(1, $fourth_order_item
    ->getAdjustments());
  $adjustments = $third_order_item
    ->getAdjustments();
  $adjustment = reset($adjustments);
  $this
    ->assertEquals('promotion', $adjustment
    ->getType());
  $this
    ->assertEquals('Discount', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-3', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals($this->promotion
    ->id(), $adjustment
    ->getSourceId());
  $adjustments = $fourth_order_item
    ->getAdjustments();
  $adjustment = reset($adjustments);
  $this
    ->assertEquals('promotion', $adjustment
    ->getType());
  $this
    ->assertEquals('Discount', $adjustment
    ->getLabel());
  $this
    ->assertEquals(new Price('-1', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals($this->promotion
    ->id(), $adjustment
    ->getSourceId());
}