public function CartManagerTest::testCartManager in Commerce Core 8.2
Tests the cart manager.
@covers ::addEntity @covers ::createOrderItem @covers ::addOrderItem @covers ::updateOrderItem @covers ::removeOrderItem @covers ::emptyCart
File
- modules/
cart/ tests/ src/ Kernel/ CartManagerTest.php, line 87
Class
- CartManagerTest
- Tests the cart manager.
Namespace
Drupal\Tests\commerce_cart\KernelCode
public function testCartManager() {
$cart = $this->cartProvider
->createCart('default', $this->store, $this->user);
$this
->assertInstanceOf(OrderInterface::class, $cart);
$this
->assertEmpty($cart
->getItems());
$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());
$order_item1
->setQuantity(2);
$this->cartManager
->updateOrderItem($cart, $order_item1);
$this
->assertNotEmpty($cart
->hasItem($order_item1));
$this
->assertEquals(2, $order_item1
->getQuantity());
$this
->assertEquals(new Price('2.00', 'USD'), $cart
->getTotalPrice());
$order_item2 = $this->cartManager
->addEntity($cart, $this->variation2, 3);
$order_item2 = $this
->reloadEntity($order_item2);
$this
->assertNotEmpty($cart
->hasItem($order_item1));
$this
->assertNotEmpty($cart
->hasItem($order_item2));
$this
->assertEquals(3, $order_item2
->getQuantity());
$this
->assertEquals($cart
->id(), $order_item2
->getOrderId());
$this
->assertEquals(new Price('8.00', 'USD'), $cart
->getTotalPrice());
$this->cartManager
->removeOrderItem($cart, $order_item1);
$this
->assertNotEmpty($cart
->hasItem($order_item2));
$this
->assertEmpty($cart
->hasItem($order_item1));
$this
->assertEquals(new Price('6.00', 'USD'), $cart
->getTotalPrice());
$cart
->addAdjustment(new Adjustment([
'type' => 'promotion',
'label' => 'Discount',
'amount' => new Price('-3.00', 'USD'),
'locked' => TRUE,
'source_id' => '1',
]))
->save();
$this
->assertNotEmpty($cart
->getAdjustments());
$this
->assertEquals(new Price('3.00', 'USD'), $cart
->getTotalPrice());
$this
->assertNotEmpty($cart
->getAdjustments());
$this->cartManager
->emptyCart($cart);
$this
->assertEmpty($cart
->getItems());
$this
->assertEquals(NULL, $cart
->getTotalPrice());
$this
->assertEmpty($cart
->getAdjustments());
}