Skip to content

Commit 4fcf7ed

Browse files
committed
Added test coverage for Adding simple product to cart functionality
1 parent 1ffd8c5 commit 4fcf7ed

File tree

2 files changed

+219
-6
lines changed

2 files changed

+219
-6
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 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+
/**
32+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
33+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
34+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
35+
*/
36+
public function testAddSimpleProductToCart()
37+
{
38+
$sku = 'simple_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['addSimpleProductsToCart']);
45+
self::assertEquals($qty, $response['addSimpleProductsToCart']['cart']['items'][0]['qty']);
46+
self::assertEquals($sku, $response['addSimpleProductsToCart']['cart']['items'][0]['product']['sku']);
47+
}
48+
49+
/**
50+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
51+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
52+
*/
53+
public function testAddProductToNonExistentCart()
54+
{
55+
$sku = 'simple_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 = 'simple_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 \"simple_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/simple_product.php
88+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
89+
*/
90+
public function testAddSimpleProductToGuestCart()
91+
{
92+
$sku = 'simple_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/simple_product.php
107+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
108+
*/
109+
public function testAddSimpleProductToAnotherCustomerCart()
110+
{
111+
$sku = 'simple_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+
addSimpleProductsToCart(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+
}

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

Lines changed: 42 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,57 @@ 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+
public function testNonExistentProductToCart()
71+
{
72+
$sku = 'simple_product';
73+
$qty = 1;
74+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
75+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
76+
77+
$this->expectExceptionMessage(
78+
"Could not find a product with SKU \"simple_product\""
79+
);
80+
81+
$this->graphQlMutation($query);
82+
}
83+
84+
/**
85+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
86+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
87+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
88+
*/
89+
public function testAddSimpleProductToCustomerCart()
90+
{
91+
$sku = 'simple_product';
92+
$qty = 2;
93+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
94+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
95+
96+
$this->expectExceptionMessage(
97+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
98+
);
99+
100+
$this->graphQlMutation($query);
101+
}
102+
67103
/**
68104
* @param string $maskedQuoteId
69105
* @param string $sku

0 commit comments

Comments
 (0)