Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreateEmptyCartTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 
20 {
24  private $quoteIdMask;
25 
29  private $objectManager;
30 
34  private $guestCartRepository;
35 
36  protected function setUp()
37  {
39  $this->quoteIdMask = $this->objectManager->create(QuoteIdMask::class);
40  $this->guestCartRepository = $this->objectManager->create(GuestCartRepositoryInterface::class);
41  }
42 
43  public function testCreateEmptyCartForGuest()
44  {
45  $query = <<<QUERY
46 mutation {
47  createEmptyCart
48 }
49 QUERY;
50  $response = $this->graphQlQuery($query);
51 
52  self::assertArrayHasKey('createEmptyCart', $response);
53 
54  $maskedCartId = $response['createEmptyCart'];
56  $guestCart = $this->guestCartRepository->get($maskedCartId);
57 
58  self::assertNotNull($guestCart->getId());
59  self::assertNull($guestCart->getCustomer()->getId());
60  }
61 
65  public function testCreateEmptyCartForRegisteredCustomer()
66  {
67  $query = <<<QUERY
68 mutation {
69  createEmptyCart
70 }
71 QUERY;
72 
74  $customerTokenService = $this->objectManager->create(
75  \Magento\Integration\Api\CustomerTokenServiceInterface::class
76  );
77  $customerToken = $customerTokenService->createCustomerAccessToken('[email protected]', 'password');
78  $headerMap = ['Authorization' => 'Bearer ' . $customerToken];
79 
80  $response = $this->graphQlQuery($query, [], '', $headerMap);
81 
82  self::assertArrayHasKey('createEmptyCart', $response);
83 
84  $maskedCartId = $response['createEmptyCart'];
85  /* guestCartRepository is used for registered customer to get the cart hash */
86  $guestCart = $this->guestCartRepository->get($maskedCartId);
87 
88  self::assertNotNull($guestCart->getId());
89  self::assertEquals(1, $guestCart->getCustomer()->getId());
90  }
91 }
$response
Definition: 404.php:11
graphQlQuery(string $query, array $variables=[], string $operationName='', array $headers=[])