1010use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
1111use Magento \Framework \Webapi \Rest \Request ;
1212use Magento \Integration \Api \CustomerTokenServiceInterface ;
13+ use Magento \Quote \Model \Quote ;
14+ use Magento \Quote \Model \QuoteIdMask ;
1315use Magento \TestFramework \Helper \Bootstrap ;
1416use Magento \TestFramework \ObjectManager ;
1517use Magento \TestFramework \TestCase \WebapiAbstract ;
@@ -29,6 +31,11 @@ class CartAddingItemsTest extends WebapiAbstract
2931 */
3032 private $ productResource ;
3133
34+ /**
35+ * @var array
36+ */
37+ private $ createdQuotes = [];
38+
3239 /**
3340 * @inheritDoc
3441 */
@@ -38,6 +45,71 @@ protected function setUp(): void
3845 $ this ->productResource = $ this ->objectManager ->get (ProductResource::class);
3946 }
4047
48+ protected function tearDown (): void
49+ {
50+ /** @var Quote $quote */
51+ $ quote = $ this ->objectManager ->create (Quote::class);
52+ foreach ($ this ->createdQuotes as $ quoteId ) {
53+ $ quote ->load ($ quoteId );
54+ $ quote ->delete ();
55+ }
56+ }
57+
58+ /**
59+ * Test qty for cart after adding grouped product qty specified only for goruped product.
60+ *
61+ * @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped_with_simple.php
62+ * @magentoApiDataFixture Magento/Customer/_files/customer_one_address.php
63+ * @return void
64+ */
65+ public function testAddToCartGroupedWithParentQuantity (): void
66+ {
67+ $ this ->_markTestAsRestOnly ();
68+
69+ // Get customer ID token
70+ /** @var CustomerTokenServiceInterface $customerTokenService */
71+ $ customerTokenService = $ this ->objectManager ->create (CustomerTokenServiceInterface::class);
72+ $ token = $ customerTokenService ->createCustomerAccessToken (
73+ 'customer_one_address@test.com ' ,
74+ 'password '
75+ );
76+
77+ // Creating empty cart for registered customer.
78+ $ serviceInfo = [
79+ 'rest ' => [
80+ 'resourcePath ' => '/V1/carts/mine ' ,
81+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
82+ 'token ' => $ token
83+ ]
84+ ];
85+
86+ $ quoteId = $ this ->_webApiCall ($ serviceInfo , ['customerId ' => 999 ]); // customerId 999 will get overridden
87+ $ this ->assertGreaterThan (0 , $ quoteId );
88+
89+ /** @var CartRepositoryInterface $cartRepository */
90+ $ cartRepository = $ this ->objectManager ->get (CartRepositoryInterface::class);
91+ $ quote = $ cartRepository ->get ($ quoteId );
92+
93+ $ quoteItems = $ quote ->getItemsCollection ();
94+ foreach ($ quoteItems as $ item ) {
95+ $ quote ->removeItem ($ item ->getId ())->save ();
96+ }
97+
98+ $ requestData = [
99+ 'cartItem ' => [
100+ 'quote_id ' => $ quoteId ,
101+ 'sku ' => 'grouped ' ,
102+ 'qty ' => 7
103+ ]
104+ ];
105+ $ this ->_webApiCall ($ this ->getServiceInfoAddToCart ($ token ), $ requestData );
106+
107+ foreach ($ quote ->getAllItems () as $ item ) {
108+ $ this ->assertEquals (7 , $ item ->getQty ());
109+ }
110+ $ this ->createdQuotes [] = $ quoteId ;
111+ }
112+
41113 /**
42114 * Test price for cart after adding product to.
43115 *
@@ -94,10 +166,11 @@ public function testPriceForCreatingQuoteFromEmptyCart()
94166 $ paymentInfo = $ this ->_webApiCall ($ serviceInfoForGettingPaymentInfo );
95167 $ this ->assertEquals ($ paymentInfo ['totals ' ]['grand_total ' ], 10 );
96168
97- /** @var \Magento\Quote\Model\Quote $quote */
98- $ quote = $ this ->objectManager ->create (\Magento \Quote \Model \Quote::class);
99- $ quote ->load ($ quoteId );
100- $ quote ->delete ();
169+ $ this ->createdQuotes [] = $ quoteId ;
170+ // /** @var \Magento\Quote\Model\Quote $quote */
171+ // $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
172+ // $quote->load($quoteId);
173+ // $quote->delete();
101174 }
102175
103176 /**
@@ -163,6 +236,7 @@ public function testAddToCartGroupedCustomQuantity(): void
163236 foreach ($ quote ->getAllItems () as $ item ) {
164237 $ this ->assertEquals ($ qtyData [$ item ->getProductId ()], $ item ->getQty ());
165238 }
239+ $ this ->createdQuotes [] = $ quoteId ;
166240 }
167241
168242 /**
@@ -210,6 +284,8 @@ public function testAddToCartGroupedCustomQuantityNotAllParamsSpecified(): void
210284 ]
211285 ];
212286
287+ $ this ->createdQuotes [] = $ quoteId ;
288+
213289 $ this ->expectException (\Exception::class);
214290 $ this ->expectExceptionMessage ('Please specify id and qty for grouped options. ' );
215291
0 commit comments