You are here

public function BuyXGetYTest::testSameOrderItem in Commerce Core 8.2

Tests the same order item matching both buy and get conditions.

@covers ::apply

File

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

Class

BuyXGetYTest
Tests the "Buy X Get Y" offer.

Namespace

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

Code

public function testSameOrderItem() {
  $offer = $this->promotion
    ->getOffer();
  $offer_configuration = $offer
    ->getConfiguration();
  $offer_configuration['buy_quantity'] = '1';
  $offer_configuration['buy_conditions'] = [];
  $offer_configuration['get_quantity'] = '1';
  $offer_configuration['get_conditions'] = [];
  $offer
    ->setConfiguration($offer_configuration);
  $this->promotion
    ->setOffer($offer);

  /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
  $order_item_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_order_item');

  // '2' buy quantities, '2' get quantities, '1' ignored/irrelevant quantity.
  $order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[0], [
    'quantity' => '5',
  ]);
  $order_item
    ->save();
  $this->order
    ->addItem($order_item);
  $this->order
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  list($order_item) = $this->order
    ->getItems();
  $this
    ->assertCount(1, $order_item
    ->getAdjustments());
  $adjustments = $order_item
    ->getAdjustments();
  $adjustment = reset($adjustments);
  $this
    ->assertEquals('promotion', $adjustment
    ->getType());
  $this
    ->assertEquals(new Price('-2', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals($this->promotion
    ->id(), $adjustment
    ->getSourceId());
}