diff --git a/modules/cart/tests/src/Kernel/CartProviderTest.php b/modules/cart/tests/src/Kernel/CartProviderTest.php index 27de10f..24b3282 100644 --- a/modules/cart/tests/src/Kernel/CartProviderTest.php +++ b/modules/cart/tests/src/Kernel/CartProviderTest.php @@ -6,8 +6,8 @@ use Drupal\commerce_cart\CartProvider; use Drupal\commerce_cart\CartSession; use Drupal\commerce_cart\Exception\DuplicateCartException; use Drupal\commerce_order\Entity\LineItemType; +use Drupal\commerce_order\Entity\Order; use Drupal\commerce_order\Entity\OrderInterface; -use Drupal\commerce_order\Entity\OrderType; use Drupal\commerce_store\Entity\Store; use Drupal\commerce_store\Entity\StoreType; use Drupal\KernelTests\KernelTestBase; @@ -46,16 +46,29 @@ class CartProviderTest extends KernelTestBase { ]; /** + * The store. + * * @var \Drupal\commerce_store\Entity\StoreInterface */ protected $store; /** + * Anonymous user. + * * @var \Drupal\user\UserInterface */ protected $anonymousUser; /** + * Registered user. + * + * @var \Drupal\user\UserInterface + */ + protected $registeredUser; + + /** + * The entity manager. + * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityManager; @@ -92,12 +105,17 @@ class CartProviderTest extends KernelTestBase { // Insert a row for the anonymous user. $user_storage->create([ - 'uid' => 0, - 'name' => '', - 'status' => 0, - ]) - ->save(); + 'uid' => 0, + 'name' => '', + 'status' => 0, + ])->save(); $this->anonymousUser = User::load(0); + $user_storage->create([ + 'uid' => 1, + 'name' => 'Hank', + 'status' => 1, + ])->save(); + $this->registeredUser = User::load(0); } /** @@ -110,8 +128,6 @@ class CartProviderTest extends KernelTestBase { */ public function testCreateAnonymousUserCartProvider() { $cart_session = new CartSession($this->container->get('session')); - - // This is the class we want to test. $cartProvider = new CartProvider($this->entityManager, $this->anonymousUser, $cart_session); // Test the createCart method. @@ -134,8 +150,6 @@ class CartProviderTest extends KernelTestBase { */ public function testGetAnonymousUserCartProvider() { $cart_session = new CartSession($this->container->get('session')); - - // This is the class we want to test. $cartProvider = new CartProvider($this->entityManager, $this->anonymousUser, $cart_session); // Test the getCart method. @@ -159,4 +173,55 @@ class CartProviderTest extends KernelTestBase { $this->assertContains(1, $cart_ids, 'The cart ids returned should contain an id with value 1.'); } + /** + * This test the createCart method in the CartProvider for a registered user. + * + * @covers ::createCart + */ + public function testCreateRegisteredUserCartProvider() { + $cart_session = new CartSession($this->container->get('session')); + + $cartProvider = new CartProvider($this->entityManager, $this->registeredUser, $cart_session); + + // Test the createCart method. + $cart = $cartProvider->createCart('default', $this->store, $this->registeredUser); + $this->assertInstanceOf(OrderInterface::class, $cart, 'The cart is created for a registered user if createCart returns an Order entity.'); + + // Recreating a cart again will throw an exception. + $this->setExpectedException(DuplicateCartException::class); + $cartProvider->createCart('default', $this->store, $this->registeredUser); + } + + /** + * This tests the get methods for the CartProvider with a registered user. + * + * @covers ::getCart + * @covers ::getCartId + * @covers ::getCarts + * @covers ::getCartIds + */ + public function testGetRegisteredUserCartProvider() { + $cart_session = new CartSession($this->container->get('session')); + $cartProvider = new CartProvider($this->entityManager, $this->registeredUser, $cart_session); + $cartProvider->createCart('default', $this->store, $this->registeredUser); + + // Test the getCart method. + $cart = $cartProvider->getCart('default', $this->store, $this->registeredUser); + $this->assertInstanceOf(OrderInterface::class, $cart, 'The cart is found when an order type, store and user is given.'); + + // Test the getCartId method. + $cart_id = $cartProvider->getCartId('default', $this->store, $this->registeredUser); + $this->assertInternalType('int', $cart_id, 'The cart id is when an order type, store and user is given.'); + $this->assertEquals(1, $cart_id, 'The expected cart id 1 for the given order type, store and user.'); + + // Test the getCarts method. + $carts = $cartProvider->getCarts($this->registeredUser); + $this->assertContainsOnlyInstancesOf(OrderInterface::class, $carts, 'The carts returned should all be objects that implement the OrderInterface.'); + + // Test the getCartIds method. + $cart_ids = $cartProvider->getCartIds($this->registeredUser); + $this->assertContainsOnly('int', $cart_ids, 'The cart ids returned should all be integers.'); + $this->assertContains(1, $cart_ids, 'The cart ids returned should contain an id with value 1.'); + } + }