CreateEmptyCartTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Quote;
  8. use Magento\Quote\Api\Data\CartInterface;
  9. use Magento\TestFramework\ObjectManager;
  10. use Magento\TestFramework\TestCase\GraphQlAbstract;
  11. use Magento\Quote\Model\QuoteIdMask;
  12. use Magento\Quote\Api\GuestCartRepositoryInterface;
  13. /**
  14. * Test for empty cart creation mutation
  15. */
  16. class CreateEmptyCartTest extends GraphQlAbstract
  17. {
  18. /**
  19. * @var QuoteIdMask
  20. */
  21. private $quoteIdMask;
  22. /**
  23. * @var ObjectManager
  24. */
  25. private $objectManager;
  26. /**
  27. * @var GuestCartRepositoryInterface
  28. */
  29. private $guestCartRepository;
  30. protected function setUp()
  31. {
  32. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  33. $this->quoteIdMask = $this->objectManager->create(QuoteIdMask::class);
  34. $this->guestCartRepository = $this->objectManager->create(GuestCartRepositoryInterface::class);
  35. }
  36. public function testCreateEmptyCartForGuest()
  37. {
  38. $query = <<<QUERY
  39. mutation {
  40. createEmptyCart
  41. }
  42. QUERY;
  43. $response = $this->graphQlQuery($query);
  44. self::assertArrayHasKey('createEmptyCart', $response);
  45. $maskedCartId = $response['createEmptyCart'];
  46. /** @var CartInterface $guestCart */
  47. $guestCart = $this->guestCartRepository->get($maskedCartId);
  48. self::assertNotNull($guestCart->getId());
  49. self::assertNull($guestCart->getCustomer()->getId());
  50. }
  51. /**
  52. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  53. */
  54. public function testCreateEmptyCartForRegisteredCustomer()
  55. {
  56. $query = <<<QUERY
  57. mutation {
  58. createEmptyCart
  59. }
  60. QUERY;
  61. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  62. $customerTokenService = $this->objectManager->create(
  63. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  64. );
  65. $customerToken = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  66. $headerMap = ['Authorization' => 'Bearer ' . $customerToken];
  67. $response = $this->graphQlQuery($query, [], '', $headerMap);
  68. self::assertArrayHasKey('createEmptyCart', $response);
  69. $maskedCartId = $response['createEmptyCart'];
  70. /* guestCartRepository is used for registered customer to get the cart hash */
  71. $guestCart = $this->guestCartRepository->get($maskedCartId);
  72. self::assertNotNull($guestCart->getId());
  73. self::assertEquals(1, $guestCart->getCustomer()->getId());
  74. }
  75. }