Skip to content

Commit 8fe1e21

Browse files
committed
Api-functional tests added
1 parent 09ed1b8 commit 8fe1e21

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote;
9+
10+
use Magento\Quote\Api\Data\CartInterface;
11+
use Magento\TestFramework\ObjectManager;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
use Magento\Quote\Model\QuoteIdMask;
14+
use Magento\Quote\Api\GuestCartRepositoryInterface;
15+
16+
/**
17+
* Test for empty cart creation mutation
18+
*/
19+
class CreateEmptyCartTest extends GraphQlAbstract
20+
{
21+
/**
22+
* @var QuoteIdMask
23+
*/
24+
private $quoteIdMask;
25+
26+
/**
27+
* @var ObjectManager
28+
*/
29+
private $objectManager;
30+
31+
/**
32+
* @var GuestCartRepositoryInterface
33+
*/
34+
private $guestCartRepository;
35+
36+
protected function setUp()
37+
{
38+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
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'];
55+
/** @var CartInterface $guestCart */
56+
$guestCart = $this->guestCartRepository->get($maskedCartId);
57+
58+
self::assertNotNull($guestCart->getId());
59+
self::assertNull($guestCart->getCustomer()->getId());
60+
}
61+
62+
/**
63+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
64+
*/
65+
public function testCreateEmptyCartForRegisteredCustomer()
66+
{
67+
$query = <<<QUERY
68+
mutation {
69+
createEmptyCart
70+
}
71+
QUERY;
72+
73+
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
74+
$customerTokenService = $this->objectManager->create(
75+
\Magento\Integration\Api\CustomerTokenServiceInterface::class
76+
);
77+
$customerToken = $customerTokenService->createCustomerAccessToken('customer@example.com', '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+
}

0 commit comments

Comments
 (0)