7
7
8
8
namespace Magento \Quote \Api ;
9
9
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 ;
10
15
use Magento \TestFramework \TestCase \WebapiAbstract ;
11
16
12
17
/**
15
20
class CartAddingItemsTest extends WebapiAbstract
16
21
{
17
22
/**
18
- * @var \Magento\TestFramework\ ObjectManager
23
+ * @var ObjectManager
19
24
*/
20
25
protected $ objectManager ;
21
26
27
+ /**
28
+ * @var ProductResource
29
+ */
30
+ private $ productResource ;
31
+
32
+ /**
33
+ * @inheritDoc
34
+ */
22
35
protected function setUp (): void
23
36
{
24
- $ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
37
+ $ this ->objectManager = Bootstrap::getObjectManager ();
38
+ $ this ->productResource = $ this ->objectManager ->get (ProductResource::class);
25
39
}
26
40
27
41
/**
@@ -36,9 +50,9 @@ public function testPriceForCreatingQuoteFromEmptyCart()
36
50
$ this ->_markTestAsRestOnly ();
37
51
38
52
// Get customer ID token
39
- /** @var \Magento\Integration\Api\ CustomerTokenServiceInterface $customerTokenService */
53
+ /** @var CustomerTokenServiceInterface $customerTokenService */
40
54
$ customerTokenService = $ this ->objectManager ->create (
41
- \ Magento \ Integration \ Api \ CustomerTokenServiceInterface::class
55
+ CustomerTokenServiceInterface::class
42
56
);
43
57
$ token = $ customerTokenService ->createCustomerAccessToken (
44
58
@@ -49,7 +63,7 @@ public function testPriceForCreatingQuoteFromEmptyCart()
49
63
$ serviceInfo = [
50
64
'rest ' => [
51
65
'resourcePath ' => '/V1/carts/mine ' ,
52
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST ,
66
+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
53
67
'token ' => $ token
54
68
]
55
69
];
@@ -58,29 +72,22 @@ public function testPriceForCreatingQuoteFromEmptyCart()
58
72
$ this ->assertGreaterThan (0 , $ quoteId );
59
73
60
74
// 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
- ];
68
75
$ requestData = [
69
76
'cartItem ' => [
70
77
'quote_id ' => $ quoteId ,
71
78
'sku ' => 'simple ' ,
72
79
'qty ' => 1
73
80
]
74
81
];
75
- $ item = $ this ->_webApiCall ($ serviceInfoForAddingProduct , $ requestData );
82
+ $ item = $ this ->_webApiCall ($ this -> getServiceInfoAddToCart ( $ token ) , $ requestData );
76
83
$ this ->assertNotEmpty ($ item );
77
84
$ this ->assertEquals (10 , $ item ['price ' ]);
78
85
79
86
// Get payment information
80
87
$ serviceInfoForGettingPaymentInfo = [
81
88
'rest ' => [
82
89
'resourcePath ' => '/V1/carts/mine/payment-information ' ,
83
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_GET ,
90
+ 'httpMethod ' => Request::HTTP_METHOD_GET ,
84
91
'token ' => $ token
85
92
]
86
93
];
@@ -92,4 +99,86 @@ public function testPriceForCreatingQuoteFromEmptyCart()
92
99
$ quote ->load ($ quoteId );
93
100
$ quote ->delete ();
94
101
}
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
+ }
95
184
}
0 commit comments