You are here

public function CartManagerTest::testAddDuplicateOrderItem in Commerce Core 8.2

Tests that duplicate order items are combined.

File

modules/cart/tests/src/Kernel/CartManagerTest.php, line 154

Class

CartManagerTest
Tests the cart manager.

Namespace

Drupal\Tests\commerce_cart\Kernel

Code

public function testAddDuplicateOrderItem() {
  $cart = $this->cartProvider
    ->createCart('default', $this->store, $this->user);
  $this
    ->assertInstanceOf(OrderInterface::class, $cart);
  $this
    ->assertEmpty($cart
    ->getItems());

  // First item added.
  $order_item1 = $this->cartManager
    ->addEntity($cart, $this->variation1);
  $order_item1 = $this
    ->reloadEntity($order_item1);
  $this
    ->assertNotEmpty($cart
    ->hasItem($order_item1));
  $this
    ->assertEquals(1, $order_item1
    ->getQuantity());
  $this
    ->assertEquals($cart
    ->id(), $order_item1
    ->getOrderId());
  $this
    ->assertEquals(new Price('1.00', 'USD'), $cart
    ->getTotalPrice());

  // Second item should be combined.
  $order_item2 = $this->cartManager
    ->addEntity($cart, $this->variation1, 3);
  $order_item2 = $this
    ->reloadEntity($order_item2);
  $this
    ->assertNotEmpty($cart
    ->hasItem($order_item2));
  $this
    ->assertEquals(4, $order_item2
    ->getQuantity());
  $this
    ->assertEquals($cart
    ->id(), $order_item2
    ->getOrderId());
  $this
    ->assertEquals(new Price('4.00', 'USD'), $cart
    ->getTotalPrice());

  // Test FALSE combine flag.
  $order_item3 = $this->cartManager
    ->addEntity($cart, $this->variation1, 3, FALSE);
  $order_item3 = $this
    ->reloadEntity($order_item3);
  $this
    ->assertNotEmpty($cart
    ->hasItem($order_item3));
  $this
    ->assertEquals(4, $order_item2
    ->getQuantity());
  $this
    ->assertEquals($cart
    ->id(), $order_item2
    ->getOrderId());
  $this
    ->assertEquals(3, $order_item3
    ->getQuantity());
  $this
    ->assertEquals($cart
    ->id(), $order_item3
    ->getOrderId());
  $this
    ->assertEquals(new Price('7.00', 'USD'), $cart
    ->getTotalPrice());
}