You are here

public function OrderTotalSummaryTest::testWithAllAdjustments in Commerce Core 8.2

Tests the order total summary with both order and order item adjustments.

File

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

Class

OrderTotalSummaryTest
Tests the order total summary.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

public function testWithAllAdjustments() {

  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
  $order_item = OrderItem::create([
    'type' => 'default',
    'quantity' => 2,
    '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',
  ]));

  // This adjustment should be first.
  $order_item
    ->addAdjustment(new Adjustment([
    'type' => 'test_adjustment_type',
    'label' => '50 cent item fee',
    'amount' => new Price('0.50', 'USD'),
  ]));
  $order_item
    ->save();
  $order_item = $this
    ->reloadEntity($order_item);
  $this->order
    ->addItem($order_item);
  $this->order
    ->addAdjustment(new Adjustment([
    'type' => 'promotion',
    'label' => 'Back to school discount',
    'amount' => new Price('-5.00', 'USD'),
    'percentage' => '0.1',
    'source_id' => '1',
  ]));
  $this->order
    ->addAdjustment(new Adjustment([
    'type' => 'custom',
    'label' => 'Handling fee',
    'amount' => new Price('10.00', 'USD'),
  ]));
  $this->order
    ->save();
  $totals = $this->orderTotalSummary
    ->buildTotals($this->order);
  $this
    ->assertEquals(new Price('24.00', 'USD'), $totals['subtotal']);
  $this
    ->assertEquals(new Price('28.50', 'USD'), $totals['total']);
  $this
    ->assertCount(3, $totals['adjustments']);
  $first = array_shift($totals['adjustments']);
  $this
    ->assertEquals('test_adjustment_type', $first['type']);
  $this
    ->assertEquals('50 cent item fee', $first['label']);
  $this
    ->assertEquals(new Price('0.50', 'USD'), $first['amount']);
  $this
    ->assertNull($first['percentage']);
  $second = array_shift($totals['adjustments']);
  $this
    ->assertEquals('promotion', $second['type']);
  $this
    ->assertEquals('Back to school discount', $second['label']);
  $this
    ->assertEquals(new Price('-6', 'USD'), $second['amount']);
  $this
    ->assertEquals('0.1', $second['percentage']);
  $third = array_shift($totals['adjustments']);
  $this
    ->assertEquals('custom', $third['type']);
  $this
    ->assertEquals('Handling fee', $third['label']);
  $this
    ->assertEquals(new Price('10', 'USD'), $third['amount']);
  $this
    ->assertNull($third['percentage']);
}