guestCartRepository = $objectManager->get(GuestCartRepositoryInterface::class); $this->quoteCollectionFactory = $objectManager->get(QuoteCollectionFactory::class); $this->quoteResource = $objectManager->get(QuoteResource::class); $this->quoteIdMaskFactory = $objectManager->get(QuoteIdMaskFactory::class); } public function testCreateEmptyCart() { $query = $this->getQuery(); $response = $this->graphQlMutation($query); self::assertArrayHasKey('createEmptyCart', $response); self::assertNotEmpty($response['createEmptyCart']); $guestCart = $this->guestCartRepository->get($response['createEmptyCart']); self::assertNotNull($guestCart->getId()); self::assertNull($guestCart->getCustomer()->getId()); self::assertEquals('default', $guestCart->getStore()->getCode()); self::assertEquals('1', $guestCart->getCustomerIsGuest()); } /** * @magentoApiDataFixture Magento/Store/_files/second_store.php */ public function testCreateEmptyCartWithNotDefaultStore() { $query = $this->getQuery(); $headerMap = ['Store' => 'fixture_second_store']; $response = $this->graphQlMutation($query, [], '', $headerMap); self::assertArrayHasKey('createEmptyCart', $response); self::assertNotEmpty($response['createEmptyCart']); $guestCart = $this->guestCartRepository->get($response['createEmptyCart']); $this->maskedQuoteId = $response['createEmptyCart']; self::assertNotNull($guestCart->getId()); self::assertNull($guestCart->getCustomer()->getId()); self::assertSame('fixture_second_store', $guestCart->getStore()->getCode()); self::assertEquals('1', $guestCart->getCustomerIsGuest()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php */ public function testCreateEmptyCartWithPredefinedCartId() { $predefinedCartId = '572cda51902b5b517c0e1a2b2fd004b4'; $query = <<graphQlMutation($query); self::assertArrayHasKey('createEmptyCart', $response); self::assertEquals($predefinedCartId, $response['createEmptyCart']); $guestCart = $this->guestCartRepository->get($response['createEmptyCart']); self::assertNotNull($guestCart->getId()); self::assertNull($guestCart->getCustomer()->getId()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * */ public function testCreateEmptyCartIfPredefinedCartIdAlreadyExists() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Cart with ID "572cda51902b5b517c0e1a2b2fd004b4" already exists.'); $predefinedCartId = '572cda51902b5b517c0e1a2b2fd004b4'; $query = <<graphQlMutation($query); $this->graphQlMutation($query); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * */ public function testCreateEmptyCartWithWrongPredefinedCartId() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Cart ID length should to be 32 symbols.'); $predefinedCartId = '572'; $query = <<graphQlMutation($query); } /** * @return string */ private function getQuery(): string { return <<quoteCollectionFactory->create(); foreach ($quoteCollection as $quote) { $this->quoteResource->delete($quote); $quoteIdMask = $this->quoteIdMaskFactory->create(); $quoteIdMask->setQuoteId($quote->getId()) ->delete(); } parent::tearDown(); } }