You are here

public function CartProviderTest::testGetAuthenticatedCart in Commerce Core 8.2

Tests getting an authenticated user's cart.

@covers ::getCart @covers ::getCartId @covers ::getCarts @covers ::getCartIds

File

modules/cart/tests/src/Kernel/CartProviderTest.php, line 125

Class

CartProviderTest
Tests the cart provider.

Namespace

Drupal\Tests\commerce_cart\Kernel

Code

public function testGetAuthenticatedCart() {
  $this->cartProvider
    ->createCart('default', $this->store, $this->authenticatedUser);
  $cart = $this->cartProvider
    ->getCart('default', $this->store, $this->authenticatedUser);
  $this
    ->assertInstanceOf(OrderInterface::class, $cart);
  $cart_id = $this->cartProvider
    ->getCartId('default', $this->store, $this->authenticatedUser);
  $this
    ->assertEquals(1, $cart_id);
  $carts = $this->cartProvider
    ->getCarts($this->authenticatedUser);
  $this
    ->assertContainsOnlyInstancesOf(OrderInterface::class, $carts);
  $this
    ->assertContains(1, $this->cartProvider
    ->getCartIds($this->authenticatedUser, $this->store));
  $this
    ->assertContains(1, $this->cartProvider
    ->getCartIds($this->authenticatedUser));
  $another_store = $this
    ->createStore();
  $another_cart = $this->cartProvider
    ->createCart('default', $another_store, $this->authenticatedUser);
  $another_cart = $this
    ->reloadEntity($another_cart);
  $this
    ->assertInstanceOf(OrderInterface::class, $another_cart);
  $this
    ->assertEquals($another_cart
    ->id(), $this->cartProvider
    ->getCartId('default', $another_store, $this->authenticatedUser));
  $carts = $this->cartProvider
    ->getCarts($this->authenticatedUser, $another_store);
  $this
    ->assertEquals($another_cart, reset($carts));
  $this
    ->assertContains($another_cart
    ->id(), $this->cartProvider
    ->getCartIds($this->authenticatedUser, $another_store));

  // Test that 2 carts are returned when omitting the store parameter.
  $this
    ->assertCount(2, $this->cartProvider
    ->getCartIds($this->authenticatedUser));
}