Skip to content

Commit 74709d2

Browse files
test coverage
1 parent cc0c942 commit 74709d2

File tree

1 file changed

+103
-14
lines changed

1 file changed

+103
-14
lines changed

dev/tests/api-functional/testsuite/Magento/Quote/Api/CartAddingItemsTest.php

Lines changed: 103 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
namespace Magento\Quote\Api;
99

10+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
11+
use Magento\Framework\Webapi\Rest\Request;
12+
use Magento\Integration\Api\CustomerTokenServiceInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\ObjectManager;
1015
use Magento\TestFramework\TestCase\WebapiAbstract;
1116

1217
/**
@@ -15,13 +20,22 @@
1520
class CartAddingItemsTest extends WebapiAbstract
1621
{
1722
/**
18-
* @var \Magento\TestFramework\ObjectManager
23+
* @var ObjectManager
1924
*/
2025
protected $objectManager;
2126

27+
/**
28+
* @var ProductResource
29+
*/
30+
private $productResource;
31+
32+
/**
33+
* @inheritDoc
34+
*/
2235
protected function setUp(): void
2336
{
24-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
37+
$this->objectManager = Bootstrap::getObjectManager();
38+
$this->productResource = $this->objectManager->get(ProductResource::class);
2539
}
2640

2741
/**
@@ -36,9 +50,9 @@ public function testPriceForCreatingQuoteFromEmptyCart()
3650
$this->_markTestAsRestOnly();
3751

3852
// Get customer ID token
39-
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
53+
/** @var CustomerTokenServiceInterface $customerTokenService */
4054
$customerTokenService = $this->objectManager->create(
41-
\Magento\Integration\Api\CustomerTokenServiceInterface::class
55+
CustomerTokenServiceInterface::class
4256
);
4357
$token = $customerTokenService->createCustomerAccessToken(
4458
@@ -49,7 +63,7 @@ public function testPriceForCreatingQuoteFromEmptyCart()
4963
$serviceInfo = [
5064
'rest' => [
5165
'resourcePath' => '/V1/carts/mine',
52-
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
66+
'httpMethod' => Request::HTTP_METHOD_POST,
5367
'token' => $token
5468
]
5569
];
@@ -58,29 +72,22 @@ public function testPriceForCreatingQuoteFromEmptyCart()
5872
$this->assertGreaterThan(0, $quoteId);
5973

6074
// Adding item to the cart
61-
$serviceInfoForAddingProduct = [
62-
'rest' => [
63-
'resourcePath' => '/V1/carts/mine/items',
64-
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
65-
'token' => $token
66-
]
67-
];
6875
$requestData = [
6976
'cartItem' => [
7077
'quote_id' => $quoteId,
7178
'sku' => 'simple',
7279
'qty' => 1
7380
]
7481
];
75-
$item = $this->_webApiCall($serviceInfoForAddingProduct, $requestData);
82+
$item = $this->_webApiCall($this->getServiceInfoAddToCart($token), $requestData);
7683
$this->assertNotEmpty($item);
7784
$this->assertEquals(10, $item['price']);
7885

7986
// Get payment information
8087
$serviceInfoForGettingPaymentInfo = [
8188
'rest' => [
8289
'resourcePath' => '/V1/carts/mine/payment-information',
83-
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
90+
'httpMethod' => Request::HTTP_METHOD_GET,
8491
'token' => $token
8592
]
8693
];
@@ -92,4 +99,86 @@ public function testPriceForCreatingQuoteFromEmptyCart()
9299
$quote->load($quoteId);
93100
$quote->delete();
94101
}
102+
103+
/**
104+
* Test qty for cart after adding grouped product with custom qty.
105+
*
106+
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped_with_simple.php
107+
* @magentoApiDataFixture Magento/Customer/_files/customer_one_address.php
108+
* @return void
109+
*/
110+
public function testAddToCartGroupedCustomQuantity(): void
111+
{
112+
$this->_markTestAsRestOnly();
113+
114+
$firstProductId = $this->productResource->getIdBySku('simple_11');
115+
$secondProductId = $this->productResource->getIdBySku('simple_22');
116+
$qtyData = [$firstProductId => 2, $secondProductId => 4];
117+
118+
// Get customer ID token
119+
/** @var CustomerTokenServiceInterface $customerTokenService */
120+
$customerTokenService = $this->objectManager->create(CustomerTokenServiceInterface::class);
121+
$token = $customerTokenService->createCustomerAccessToken(
122+
123+
'password'
124+
);
125+
126+
// Creating empty cart for registered customer.
127+
$serviceInfo = [
128+
'rest' => [
129+
'resourcePath' => '/V1/carts/mine',
130+
'httpMethod' => Request::HTTP_METHOD_POST,
131+
'token' => $token
132+
]
133+
];
134+
135+
$quoteId = $this->_webApiCall($serviceInfo, ['customerId' => 999]); // customerId 999 will get overridden
136+
$this->assertGreaterThan(0, $quoteId);
137+
138+
// Adding item to the cart
139+
$productOptionData = [
140+
'extension_attributes' => [
141+
'grouped_options' => [
142+
['id' => $firstProductId, 'qty' => $qtyData[$firstProductId]],
143+
['id' => $secondProductId, 'qty' => $qtyData[$secondProductId]],
144+
]
145+
]
146+
];
147+
$requestData = [
148+
'cartItem' => [
149+
'quote_id' => $quoteId,
150+
'sku' => 'grouped',
151+
'qty' => 1,
152+
'product_option' => $productOptionData
153+
]
154+
];
155+
$response = $this->_webApiCall($this->getServiceInfoAddToCart($token), $requestData);
156+
$this->assertArrayHasKey('product_option', $response);
157+
$this->assertEquals($response['product_option'], $productOptionData);
158+
159+
/** @var CartRepositoryInterface $cartRepository */
160+
$cartRepository = $this->objectManager->get(CartRepositoryInterface::class);
161+
$quote = $cartRepository->get($quoteId);
162+
163+
foreach ($quote->getAllItems() as $item) {
164+
$this->assertEquals($qtyData[$item->getProductId()], $item->getQty());
165+
}
166+
}
167+
168+
/**
169+
* Returns service info add to cart
170+
*
171+
* @param string $token
172+
* @return array
173+
*/
174+
private function getServiceInfoAddToCart(string $token): array
175+
{
176+
return [
177+
'rest' => [
178+
'resourcePath' => '/V1/carts/mine/items',
179+
'httpMethod' => Request::HTTP_METHOD_POST,
180+
'token' => $token
181+
]
182+
];
183+
}
95184
}

0 commit comments

Comments
 (0)