diff --git a/modules/cart/tests/src/Kernel/CartProviderTest.php b/modules/cart/tests/src/Kernel/CartProviderTest.php new file mode 100644 index 0000000..27de10f --- /dev/null +++ b/modules/cart/tests/src/Kernel/CartProviderTest.php @@ -0,0 +1,162 @@ +installSchema('system', 'router'); + $this->installEntitySchema('user'); + $this->installEntitySchema('profile'); + $this->installEntitySchema('commerce_store'); + $this->installEntitySchema('commerce_order'); + $this->installEntitySchema('commerce_line_item'); + $this->installConfig('commerce_order'); + // A line item type that doesn't need a purchasable entity, for simplicity. + LineItemType::create([ + 'id' => 'test', + 'label' => 'Test', + 'orderType' => 'default', + ])->save(); + + StoreType::create(['id' => 'animals', 'label' => 'Animals']); + $store = Store::create([ + 'type' => 'animals', + 'name' => 'Llamas and more', + ]); + $store->save(); + $this->store = Store::load($store->id()); + + $this->entityManager = $this->container->get('entity_type.manager'); + $user_storage = $this->entityManager->getStorage('user'); + + // Insert a row for the anonymous user. + $user_storage->create([ + 'uid' => 0, + 'name' => '', + 'status' => 0, + ]) + ->save(); + $this->anonymousUser = User::load(0); + } + + /** + * This test the createCart method in the CartProvider for an anonymous user. + * + * It specifically tests the exception thrown when trying to create a cart + * twice. + * + * @covers ::createCart + */ + 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. + $order_type = 'default'; + $cart = $cartProvider->createCart($order_type, $this->store, $this->anonymousUser); + $this->assertInstanceOf(OrderInterface::class, $cart); + + // Recreating a cart again will throw an exception. + $this->setExpectedException(DuplicateCartException::class); + $cartProvider->createCart($order_type, $this->store, $this->anonymousUser); + } + + /** + * This tests the get methods for the CartProvider with an anonymous user. + * + * @covers ::getCart + * @covers ::getCartId + * @covers ::getCarts + * @covers ::getCartIds + */ + 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. + $cart = $cartProvider->createCart('default', $this->store, $this->anonymousUser); + $this->assertInstanceOf(OrderInterface::class, $cart, 'The cart is found when an order type, store and user is given.'); + $cart = $cartProvider->getCart('default', $this->store, $this->anonymousUser); + $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->anonymousUser); + $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->anonymousUser); + $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); + $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.'); + } + +} diff --git a/modules/cart/tests/src/Unit/CartProviderTest.php b/modules/cart/tests/src/Unit/CartProviderTest.php deleted file mode 100644 index 2dfa8c2..0000000 --- a/modules/cart/tests/src/Unit/CartProviderTest.php +++ /dev/null @@ -1,375 +0,0 @@ -getMockBuilder(UserInterface::class); - - // Setup anonymous user and it's expectations. - $this->anonymousUser = $mockAccountBuilder->getMock(); - $this->anonymousUser->expects($this->any()) - ->method('isAuthenticated') - ->willReturn(FALSE); - $this->anonymousUser->expects($this->any()) - ->method('isAnonymous') - ->willReturn(TRUE); - $this->anonymousUser->expects($this->any()) - ->method('id') - ->willReturn(0); - - // Setup registered user and it's expectations. - $this->registeredUser = $mockAccountBuilder->getMock(); - $this->registeredUser->expects($this->any()) - ->method('isAuthenticated') - ->willReturn(TRUE); - $this->registeredUser->expects($this->any()) - ->method('isAnonymous') - ->willReturn(FALSE); - $this->registeredUser->expects($this->any()) - ->method('id') - ->willReturn(1); - - // Mock the store entity. - $mockStoreBuilder = $this->getMockBuilder(StoreInterface::class); - $this->store = $mockStoreBuilder->getMock(); - $this->store->expects($this->any()) - ->method('id') - ->willReturn(1); - - // MockBuilder to create mocked instances of the OrderInterface. - $mockOrderBuilder = $this->getMockBuilder(OrderInterface::class); - - // Some order type string we use to differentiate between carts. - $this->firstOrderType = 'default'; - $this->secondOrderType = 'randomOrderType'; - - // We need an order the test if we can create and get a cart. - $this->firstOrder = $mockOrderBuilder->getMock(); - $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); - // There's only an empty() check on the cart content, so fill it with - // something that passes that check. - $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('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 = [ - $this->firstOrder->id() => $this->firstOrder, - $this->secondOrder->id() => $this->secondOrder, - ]; - - // We need this Builder for the CartSessions that is needed during testing. - $this->mockCartSessionBuilder = $this->getMockBuilder(CartSessionInterface::class); - - // Mock the getQuery function in the orderStorage. - $mockQueryBuilder = $this->getMockBuilder(QueryInterface::class); - - // 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([]); - - // Mock the query class that returns cart ids. - $queryWithOrders = $mockQueryBuilder->getMock(); - $queryWithOrders->expects($this->any()) - ->method('condition') - ->willReturnSelf(); - $queryWithOrders->expects($this->any()) - ->method('sort') - ->willReturnSelf(); - $queryWithOrders->expects($this->any()) - ->method('execute') - ->willReturn(array_keys($orders)); - - // Entity storage mock builders to mock the get and create cart tests. - $mockEntityStorageBuilder = $this->getMockBuilder(EntityStorageInterface::class); - - // Create the orderStorage for the createCart tests. - $createOrderStorage = $mockEntityStorageBuilder->getMock(); - - // We want to mock the query that returns no cart ids. - $createOrderStorage->expects($this->any()) - ->method('getQuery') - ->willReturn($queryWithOutOrders); - - // Make sure the create returns an order. - $createOrderStorage->expects($this->any()) - ->method('create') - ->willReturn($this->firstOrder); - - // Create the orderStorage for the get method tests. - $getOrderStorage = $mockEntityStorageBuilder->getMock(); - - // We want to be able to mock the query that returns cart ids. - $getOrderStorage->expects($this->any()) - ->method('getQuery') - ->willReturn($queryWithOrders); - - // Make sure the load of the getOrderStorage returns an order. - $getOrderStorage->expects($this->any()) - ->method('load') - ->with($this->firstOrder->id()) - ->willReturn($this->firstOrder); - - // Make sure the loadMultiple returns both orders. - $getOrderStorage->expects($this->any()) - ->method('loadMultiple') - ->with(array_keys($orders)) - ->willReturn($orders); - - // Mock the entityManager for the get and create tests. - $mockEntityManagerBuilder = $this->getMockBuilder(EntityTypeManagerInterface::class); - - $this->createEntityManager = $mockEntityManagerBuilder->getMock(); - $this->createEntityManager->expects($this->any()) - ->method('getStorage') - ->with('commerce_order') - ->willReturn($createOrderStorage); - - $this->getEntityManager = $mockEntityManagerBuilder->getMock(); - $this->getEntityManager->expects($this->any()) - ->method('getStorage') - ->with('commerce_order') - ->willReturn($getOrderStorage); - } - - /** - * This test the createCart method in the CartProvider for an anonymous user. - * - * @covers ::createCart - */ - public function testCreateAnonymousUserCartProvider() { - // Mock cart session. - $cartSession = $this->mockCartSessionBuilder->getMock(); - $cartSession->expects($this->any()) - ->method('getCartIds') - ->willReturn([]); - - // This is the class we want to test. - $cartProvider = new CartProvider($this->createEntityManager, $this->anonymousUser, $cartSession); - - // Test the createCart method. - $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.'); - - // Recreating a cart again will throw an exception. - $this->setExpectedException(DuplicateCartException::class); - $cartProvider->createCart($this->firstOrderType, $this->store, $this->anonymousUser); - } - - /** - * This tests the get methods for the CartProvider with an anonymous user. - * - * @covers ::getCart - * @covers ::getCartId - * @covers ::getCarts - * @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); - - // Test the getCart method. - $cart = $cartProvider->getCart($this->firstOrderType, $this->store, $this->anonymousUser); - $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->anonymousUser); - $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->anonymousUser); - $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); - $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.'); - $this->assertContains(2, $cart_ids, 'The cart ids returned should contain an id with value 2.'); - } - - /** - * This test the createCart method in the CartProvider for a registered user. - * - * @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); - - // Test the createCart method. - $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.'); - - // Recreating a cart again will throw an exception. - $this->setExpectedException(DuplicateCartException::class); - $cartProvider->createCart($this->firstOrderType, $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() { - // 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::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); - $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.'); - $this->assertContains(2, $cart_ids, 'The cart ids returned should contain an id with value 2.'); - } - -}