You are here

public function BuyXGetYTest::testDecimalQuantities in Commerce Core 8.2

Tests working with decimal quantities.

@covers ::apply

File

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

Class

BuyXGetYTest
Tests the "Buy X Get Y" offer.

Namespace

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

Code

public function testDecimalQuantities() {

  /** @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' => '2.5',
  ]);
  $first_order_item
    ->save();
  $second_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[0], [
    'quantity' => '3.5',
  ]);
  $second_order_item
    ->save();
  $third_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[2], [
    'quantity' => '1.5',
  ]);
  $third_order_item
    ->save();
  $fourth_order_item = $order_item_storage
    ->createFromPurchasableEntity($this->variations[2], [
    'quantity' => '5.5',
  ]);
  $fourth_order_item
    ->save();
  $this->order
    ->setItems([
    $first_order_item,
    $second_order_item,
    $third_order_item,
    $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(new Price('-1.5', '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(new Price('-2.5', 'USD'), $adjustment
    ->getAmount());
  $this
    ->assertEquals($this->promotion
    ->id(), $adjustment
    ->getSourceId());
}