Skip to content

Commit 646e9dc

Browse files
authored
ENGCOM-4779: Added test coverage for Adding simple product to cart functionality #616
2 parents b8746cc + 7f4a21a commit 646e9dc

File tree

2 files changed

+220
-6
lines changed

2 files changed

+220
-6
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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 simple product to Cart
18+
*/
19+
class AddSimpleProductToCartTest extends GraphQlAbstract
20+
{
21+
/**
22+
* @var CustomerTokenServiceInterface
23+
*/
24+
private $customerTokenService;
25+
26+
/**
27+
* @var GetMaskedQuoteIdByReservedOrderId
28+
*/
29+
private $getMaskedQuoteIdByReservedOrderId;
30+
31+
protected function setUp()
32+
{
33+
$objectManager = Bootstrap::getObjectManager();
34+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
35+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
36+
}
37+
38+
/**
39+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
40+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
41+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
42+
*/
43+
public function testAddSimpleProductToCart()
44+
{
45+
$sku = 'simple_product';
46+
$qty = 2;
47+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
48+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
49+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
50+
51+
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
52+
self::assertEquals($qty, $response['addSimpleProductsToCart']['cart']['items'][0]['qty']);
53+
self::assertEquals($sku, $response['addSimpleProductsToCart']['cart']['items'][0]['product']['sku']);
54+
}
55+
56+
/**
57+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
58+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
59+
*
60+
* @expectedException \Exception
61+
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
62+
*/
63+
public function testAddProductToNonExistentCart()
64+
{
65+
$sku = 'simple_product';
66+
$qty = 2;
67+
$maskedQuoteId = 'non_existent_masked_id';
68+
69+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
70+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
71+
}
72+
73+
/**
74+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
75+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
76+
*
77+
* @expectedException \Exception
78+
* @expectedExceptionMessage Could not find a product with SKU "simple_product"
79+
*/
80+
public function testNonExistentProductToCart()
81+
{
82+
$sku = 'simple_product';
83+
$qty = 2;
84+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
85+
86+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
87+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
88+
}
89+
90+
/**
91+
* _security
92+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
93+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
94+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
95+
*/
96+
public function testAddSimpleProductToGuestCart()
97+
{
98+
$sku = 'simple_product';
99+
$qty = 2;
100+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
101+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
102+
103+
$this->expectExceptionMessage(
104+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
105+
);
106+
107+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
108+
}
109+
110+
/**
111+
* _security
112+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
113+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
114+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
115+
*/
116+
public function testAddSimpleProductToAnotherCustomerCart()
117+
{
118+
$sku = 'simple_product';
119+
$qty = 2;
120+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
121+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
122+
123+
$this->expectExceptionMessage(
124+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
125+
);
126+
127+
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
128+
}
129+
130+
/**
131+
* @param string $maskedQuoteId
132+
* @param string $sku
133+
* @param int $qty
134+
* @return string
135+
*/
136+
private function getQuery(string $maskedQuoteId, string $sku, int $qty): string
137+
{
138+
return <<<QUERY
139+
mutation {
140+
addSimpleProductsToCart(input: {
141+
cart_id: "{$maskedQuoteId}",
142+
cartItems: [
143+
{
144+
data: {
145+
qty: {$qty}
146+
sku: "{$sku}"
147+
}
148+
}
149+
]
150+
}) {
151+
cart {
152+
items {
153+
id
154+
qty
155+
product {
156+
sku
157+
}
158+
}
159+
}
160+
}
161+
}
162+
QUERY;
163+
}
164+
165+
/**
166+
* Retrieve customer authorization headers
167+
*
168+
* @param string $username
169+
* @param string $password
170+
* @return array
171+
* @throws AuthenticationException
172+
*/
173+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
174+
{
175+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
176+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
177+
return $headerMap;
178+
}
179+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ protected function setUp()
3131
}
3232

3333
/**
34-
* @magentoApiDataFixture Magento/Catalog/_files/products.php
35-
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
34+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
35+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
3636
*/
3737
public function testAddSimpleProductToCart()
3838
{
39-
$sku = 'simple';
39+
$sku = 'simple_product';
4040
$qty = 2;
41-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
41+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
4242

4343
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
4444
$response = $this->graphQlMutation($query);
@@ -49,21 +49,56 @@ public function testAddSimpleProductToCart()
4949
}
5050

5151
/**
52-
* @magentoApiDataFixture Magento/Catalog/_files/products.php
52+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
5353
*
5454
* @expectedException \Exception
5555
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
5656
*/
5757
public function testAddProductToNonExistentCart()
5858
{
59-
$sku = 'simple';
59+
$sku = 'simple_product';
6060
$qty = 1;
6161
$maskedQuoteId = 'non_existent_masked_id';
6262

6363
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
6464
$this->graphQlMutation($query);
6565
}
6666

67+
/**
68+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
69+
*
70+
* @expectedException \Exception
71+
* @expectedExceptionMessage Could not find a product with SKU "simple_product"
72+
*/
73+
public function testNonExistentProductToCart()
74+
{
75+
$sku = 'simple_product';
76+
$qty = 1;
77+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
78+
79+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
80+
$this->graphQlMutation($query);
81+
}
82+
83+
/**
84+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
85+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
86+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
87+
*/
88+
public function testAddSimpleProductToCustomerCart()
89+
{
90+
$sku = 'simple_product';
91+
$qty = 2;
92+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
93+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
94+
95+
$this->expectExceptionMessage(
96+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
97+
);
98+
99+
$this->graphQlMutation($query);
100+
}
101+
67102
/**
68103
* @param string $maskedQuoteId
69104
* @param string $sku

0 commit comments

Comments
 (0)