You are here

public function OrderTotalSummaryTest::testWithOrderItemAdjustments in Commerce Core 8.2

Tests the order total summary with order item adjustments.

File

modules/order/tests/src/Kernel/OrderTotalSummaryTest.php, line 139

Class

OrderTotalSummaryTest
Tests the order total summary.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

public function testWithOrderItemAdjustments() {

  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
  $order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 1,
    'unit_price' => new Price('12.00', 'USD'),
  ]);
  $order_item
    ->addAdjustment(new Adjustment([
    'type' => 'promotion',
    'label' => 'Back to school discount',
    'amount' => new Price('-1.00', 'USD'),
    'percentage' => '0.1',
    'source_id' => '1',
  ]));
  $order_item
    ->save();
  $order_item = $this
    ->reloadEntity($order_item);
  $this->order
    ->addItem($order_item);
  $this->order
    ->save();
  $totals = $this->orderTotalSummary
    ->buildTotals($this->order);
  $this
    ->assertEquals(new Price('12.00', 'USD'), $totals['subtotal']);
  $this
    ->assertEquals(new Price('11.00', 'USD'), $totals['total']);
  $this
    ->assertCount(1, $totals['adjustments']);
  $first = array_shift($totals['adjustments']);
  $this
    ->assertEquals('promotion', $first['type']);
  $this
    ->assertEquals('Back to school discount', $first['label']);
  $this
    ->assertEquals(new Price('-1', 'USD'), $first['amount']);
  $this
    ->assertEquals('0.1', $first['percentage']);
}