diff --git a/modules/cart/tests/src/Unit/CartProviderTest.php b/modules/cart/tests/src/Unit/CartProviderTest.php index be8e489..cb44d1e 100644 --- a/modules/cart/tests/src/Unit/CartProviderTest.php +++ b/modules/cart/tests/src/Unit/CartProviderTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\commerce_cart\Unit; use Drupal\commerce_cart\CartProvider; use Drupal\commerce_cart\CartSessionInterface; +use Drupal\commerce_cart\Exception\DuplicateCartException; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\commerce_store\Entity\StoreInterface; use Drupal\Core\Entity\EntityManagerInterface; @@ -117,32 +118,34 @@ class CartProviderTest extends UnitTestCase { // We need an order the test if we can create and get a cart. $this->firstOrder = $mockOrderBuilder->getMock(); $this->firstOrder->expects($this->any()) - ->method('getOwnerId') - ->willReturn(1); - $this->firstOrder->expects($this->any()) ->method('getType') ->willReturn($this->firstOrderType); $this->firstOrder->expects($this->any()) + ->method('bundle') + ->willReturn($this->firstOrderType); + $this->firstOrder->expects($this->any()) ->method('getStoreId') ->willReturn($this->store->id()); $this->firstOrder->expects($this->any()) ->method('id') ->willReturn(1); + $this->firstOrder->cart = 'cart contents'; // We need a second order for the getCarts and getCartIds method tests. $this->secondOrder = $mockOrderBuilder->getMock(); $this->secondOrder->expects($this->any()) - ->method('getOwnerId') - ->willReturn(1); - $this->secondOrder->expects($this->any()) ->method('getType') ->willReturn($this->secondOrderType); $this->secondOrder->expects($this->any()) + ->method('bundle') + ->willReturn($this->secondOrderType); + $this->secondOrder->expects($this->any()) ->method('getStoreId') ->willReturn($this->store->id()); $this->secondOrder->expects($this->any()) ->method('id') ->willReturn(2); + $this->secondOrder->cart = 'cart contents'; // Put the orders in an array for the get mock methods. $orders = [ @@ -159,6 +162,12 @@ class CartProviderTest extends UnitTestCase { // Query object that returns no cart ids. $queryWithOutOrders = $mockQueryBuilder->getMock(); $queryWithOutOrders->expects($this->any()) + ->method('condition') + ->willReturnSelf(); + $queryWithOutOrders->expects($this->any()) + ->method('sort') + ->willReturnSelf(); + $queryWithOutOrders->expects($this->any()) ->method('execute') ->willReturn([]); @@ -232,7 +241,6 @@ class CartProviderTest extends UnitTestCase { * @covers ::createCart */ public function testCreateAnonymousUserCartProvider() { - // Mock cart session. $cartSession = $this->mockCartSessionBuilder->getMock(); $cartSession->expects($this->any()) @@ -246,9 +254,9 @@ class CartProviderTest extends UnitTestCase { $cart = $cartProvider->createCart($this->firstOrderType, $this->store, $this->anonymousUser); $this->assertInstanceOf(OrderInterface::class, $cart, 'The cart is created for an anonymous user if createCart returns an Order entity.'); - // Test recreating the cart again with the createCart method. - $cart = $cartProvider->createCart($this->firstOrderType, $this->store, $this->anonymousUser); - $this->assertFalse($cart, 'The cart is created for an anonymous user if createCart returns an Order entity.'); + // Recreating a cart again will throw an exception. + $this->setExpectedException(DuplicateCartException::class); + $cartProvider->createCart($this->firstOrderType, $this->store, $this->anonymousUser); } /** @@ -260,12 +268,17 @@ class CartProviderTest extends UnitTestCase { * @covers ::getCartIds */ public function testGetAnonymousUserCartProvider() { - // Mock cart session to get relevant cart ids. $cartSession = $this->mockCartSessionBuilder->getMock(); $cartSession->expects($this->any()) ->method('getCartIds') ->willReturn([$this->firstOrder->id(), $this->secondOrder->id()]); + $this->firstOrder->expects($this->any()) + ->method('getOwnerId') + ->willReturn(0); + $this->secondOrder->expects($this->any()) + ->method('getOwnerId') + ->willReturn(0); // This is the class we want to test. $cartProvider = new CartProvider($this->getEntityManager, $this->anonymousUser, $cartSession); @@ -281,7 +294,7 @@ class CartProviderTest extends UnitTestCase { // Test the getCarts method. $carts = $cartProvider->getCarts($this->anonymousUser); - $this->assertContainsOnlyInstancesOf('OrderInterface', $carts, 'The carts returned should all be objects that implement the OrderInterface.'); + $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->anonymousUser); @@ -296,9 +309,11 @@ class CartProviderTest extends UnitTestCase { * @covers ::createCart */ public function testCreateRegisteredUserCartProvider() { - // Mock cart session. $cartSession = $this->mockCartSessionBuilder->getMock(); + $this->firstOrder->expects($this->any()) + ->method('getOwnerId') + ->willReturn(1); // This is the class we want to test. $cartProvider = new CartProvider($this->createEntityManager, $this->registeredUser, $cartSession); @@ -307,9 +322,9 @@ class CartProviderTest extends UnitTestCase { $cart = $cartProvider->createCart($this->firstOrderType, $this->store, $this->registeredUser); $this->assertInstanceOf(OrderInterface::class, $cart, 'The cart is created for a registered user if createCart returns an Order entity.'); - // Test recreating the cart again with the createCart method. - $cart = $cartProvider->createCart($this->firstOrderType, $this->store, $this->registeredUser); - $this->assertFalse($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($this->firstOrderType, $this->store, $this->registeredUser); } /** @@ -323,13 +338,19 @@ class CartProviderTest extends UnitTestCase { public function testGetRegisteredUserCartProvider() { // Mock cart session to get cart ids. $cartSession = $this->mockCartSessionBuilder->getMock(); + $this->firstOrder->expects($this->any()) + ->method('getOwnerId') + ->willReturn(1); + $this->secondOrder->expects($this->any()) + ->method('getOwnerId') + ->willReturn(1); // This is what we want to test. $cartProvider = new CartProvider($this->getEntityManager, $this->registeredUser, $cartSession); // Test the getCart method. $cart = $cartProvider->getCart($this->firstOrderType, $this->store, $this->registeredUser); - $this->assertInstanceOf('OrderInterface', $cart, 'The cart is found when an order type, store and user is given.'); + $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($this->firstOrderType, $this->store, $this->registeredUser); @@ -338,7 +359,7 @@ class CartProviderTest extends UnitTestCase { // Test the getCarts method. $carts = $cartProvider->getCarts($this->registeredUser); - $this->assertContainsOnlyInstancesOf('OrderInterface', $carts, 'The carts returned should all be objects that implement the OrderInterface.'); + $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);