Skip to content

Commit e2854aa

Browse files
committed
Added test coverage for Add virtual product to cart functionality
1 parent 1ffd8c5 commit e2854aa

File tree

4 files changed

+392
-0
lines changed

4 files changed

+392
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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\Customer;
9+
10+
use Magento\Framework\Exception\AuthenticationException;
11+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
12+
use Magento\Integration\Api\CustomerTokenServiceInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\GraphQlAbstract;
15+
16+
/**
17+
* Test adding virtual product to Cart
18+
*/
19+
class AddVirtualProductToCartTest extends GraphQlAbstract
20+
{
21+
/**
22+
* @var CustomerTokenServiceInterface
23+
*/
24+
private $customerTokenService;
25+
26+
/**
27+
* @var GetMaskedQuoteIdByReservedOrderId
28+
*/
29+
private $getMaskedQuoteIdByReservedOrderId;
30+
31+
/**
32+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
33+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
34+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
35+
*/
36+
public function testAddVirtualProductToCart()
37+
{
38+
$sku = 'virtual_product';
39+
$qty = 2;
40+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
41+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
42+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
43+
44+
self::assertArrayHasKey('cart', $response['addVirtualProductsToCart']);
45+
self::assertEquals($qty, $response['addVirtualProductsToCart']['cart']['items'][0]['qty']);
46+
self::assertEquals($sku, $response['addVirtualProductsToCart']['cart']['items'][0]['product']['sku']);
47+
}
48+
49+
/**
50+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
51+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
52+
*/
53+
public function testAddVirtualToNonExistentCart()
54+
{
55+
$sku = 'virtual_product';
56+
$qty = 2;
57+
$nonExistentMaskedQuoteId = 'non_existent_masked_id';
58+
$query = $this->getQuery($nonExistentMaskedQuoteId, $sku, $qty);
59+
60+
$this->expectExceptionMessage(
61+
"Could not find a cart with ID \"$nonExistentMaskedQuoteId\""
62+
);
63+
64+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
65+
}
66+
67+
/**
68+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
69+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
70+
*/
71+
public function testNonExistentProductToCart()
72+
{
73+
$sku = 'virtual_product';
74+
$qty = 2;
75+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
76+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
77+
78+
$this->expectExceptionMessage(
79+
"Could not find a product with SKU \"virtual_product\""
80+
);
81+
82+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
83+
}
84+
85+
/**
86+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
87+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
88+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
89+
*/
90+
public function testAddVirtualProductToGuestCart()
91+
{
92+
$sku = 'virtual_product';
93+
$qty = 2;
94+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
95+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
96+
97+
$this->expectExceptionMessage(
98+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
99+
);
100+
101+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
102+
}
103+
104+
/**
105+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
106+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
107+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
108+
*/
109+
public function testAddVirtualProductToAnotherCustomerCart()
110+
{
111+
$sku = 'virtual_product';
112+
$qty = 2;
113+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
114+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
115+
116+
$this->expectExceptionMessage(
117+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
118+
);
119+
120+
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
121+
}
122+
123+
protected function setUp()
124+
{
125+
$objectManager = Bootstrap::getObjectManager();
126+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
127+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
128+
}
129+
130+
/**
131+
* @param string $maskedQuoteId
132+
* @return string
133+
*/
134+
private function getQuery(string $maskedQuoteId, string $sku, int $qty): string
135+
{
136+
return <<<QUERY
137+
mutation {
138+
addVirtualProductsToCart(input: {
139+
cart_id: "{$maskedQuoteId}",
140+
cartItems: [
141+
{
142+
data: {
143+
qty: $qty
144+
sku: "$sku"
145+
}
146+
}
147+
]
148+
}) {
149+
cart {
150+
items {
151+
id
152+
qty
153+
product {
154+
sku
155+
}
156+
}
157+
}
158+
}
159+
}
160+
QUERY;
161+
}
162+
163+
/**
164+
* Retrieve customer authorization headers
165+
*
166+
* @param string $username
167+
* @param string $password
168+
* @return array
169+
* @throws AuthenticationException
170+
*/
171+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
172+
{
173+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
174+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
175+
return $headerMap;
176+
}
177+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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\Guest;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
14+
/**
15+
* Add virtual product to cart testcases
16+
*/
17+
class AddVirtualProductToCartTest extends GraphQlAbstract
18+
{
19+
/**
20+
* @var GetMaskedQuoteIdByReservedOrderId
21+
*/
22+
private $getMaskedQuoteIdByReservedOrderId;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function setUp()
28+
{
29+
$objectManager = Bootstrap::getObjectManager();
30+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
31+
}
32+
33+
/**
34+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
35+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
36+
*/
37+
public function testAddVirtualProductToCart()
38+
{
39+
$sku = 'virtual_product';
40+
$qty = 2;
41+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
42+
43+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
44+
$response = $this->graphQlMutation($query);
45+
46+
self::assertArrayHasKey('cart', $response['addVirtualProductsToCart']);
47+
self::assertEquals($qty, $response['addVirtualProductsToCart']['cart']['items'][0]['qty']);
48+
self::assertEquals($sku, $response['addVirtualProductsToCart']['cart']['items'][0]['product']['sku']);
49+
}
50+
51+
/**
52+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
53+
*/
54+
public function testAddVirtualToNonExistentCart()
55+
{
56+
$sku = 'virtual_product';
57+
$qty = 1;
58+
$maskedQuoteId = 'non_existent_masked_id';
59+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
60+
61+
$this->expectExceptionMessage(
62+
"Could not find a cart with ID \"non_existent_masked_id\""
63+
);
64+
65+
$this->graphQlMutation($query);
66+
}
67+
68+
/**
69+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
70+
*/
71+
public function testNonExistentProductToCart()
72+
{
73+
$sku = 'virtual_product';
74+
$qty = 1;
75+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
76+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
77+
78+
$this->expectExceptionMessage(
79+
"Could not find a product with SKU \"virtual_product\""
80+
);
81+
82+
$this->graphQlMutation($query);
83+
}
84+
85+
/**
86+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
87+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
88+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
89+
*/
90+
public function testAddVirtualProductToCustomerCart()
91+
{
92+
$sku = 'virtual_product';
93+
$qty = 2;
94+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
95+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
96+
97+
$this->expectExceptionMessage(
98+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
99+
);
100+
101+
$this->graphQlMutation($query);
102+
}
103+
104+
/**
105+
* @param string $maskedQuoteId
106+
* @param string $sku
107+
* @param int $qty
108+
* @return string
109+
*/
110+
private function getQuery(string $maskedQuoteId, string $sku, int $qty): string
111+
{
112+
return <<<QUERY
113+
mutation {
114+
addVirtualProductsToCart(
115+
input: {
116+
cart_id: "{$maskedQuoteId}"
117+
cartItems: [
118+
{
119+
data: {
120+
qty: $qty
121+
sku: "$sku"
122+
}
123+
}
124+
]
125+
}
126+
) {
127+
cart {
128+
items {
129+
qty
130+
product {
131+
sku
132+
}
133+
}
134+
}
135+
}
136+
}
137+
QUERY;
138+
}
139+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
12+
use Magento\Catalog\Model\Product\Type;
13+
use Magento\Catalog\Model\Product\Visibility;
14+
use Magento\Framework\Api\DataObjectHelper;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
/** @var ProductInterfaceFactory $productFactory */
19+
$productFactory = $objectManager->get(ProductInterfaceFactory::class);
20+
/** @var DataObjectHelper $dataObjectHelper */
21+
$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
22+
/** @var ProductRepositoryInterface $productRepository */
23+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
24+
25+
$product = $productFactory->create();
26+
$productData = [
27+
ProductInterface::TYPE_ID => Type::TYPE_VIRTUAL,
28+
ProductInterface::ATTRIBUTE_SET_ID => 4,
29+
ProductInterface::SKU => 'virtual_product',
30+
ProductInterface::NAME => 'Virtual Product',
31+
ProductInterface::PRICE => 10,
32+
ProductInterface::VISIBILITY => Visibility::VISIBILITY_BOTH,
33+
ProductInterface::STATUS => Status::STATUS_ENABLED,
34+
];
35+
$dataObjectHelper->populateWithArray($product, $productData, ProductInterface::class);
36+
/** Out of interface */
37+
$product
38+
->setWebsiteIds([1])
39+
->setStockData([
40+
'qty' => 85.5,
41+
'is_in_stock' => true,
42+
'manage_stock' => true,
43+
'is_qty_decimal' => true
44+
]);
45+
$productRepository->save($product);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Framework\Registry;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var ProductRepositoryInterface $productRepository */
14+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
15+
/** @var Registry $registry */
16+
$registry = $objectManager->get(Registry::class);
17+
18+
$currentArea = $registry->registry('isSecureArea');
19+
$registry->unregister('isSecureArea');
20+
$registry->register('isSecureArea', true);
21+
22+
try {
23+
$productRepository->deleteById('virtual_product');
24+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
25+
/**
26+
* Tests which are wrapped with MySQL transaction clear all data by transaction rollback.
27+
*/
28+
}
29+
30+
$registry->unregister('isSecureArea');
31+
$registry->register('isSecureArea', $currentArea);

0 commit comments

Comments
 (0)