7
7
8
8
namespace Magento \GraphQl \Quote ;
9
9
10
+ use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
11
+ use Magento \TestFramework \Fixture \DataFixtureStorage ;
12
+ use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
10
13
use Magento \TestFramework \Helper \Bootstrap ;
11
14
use Magento \TestFramework \TestCase \GraphQlAbstract ;
12
15
@@ -30,6 +33,16 @@ class AddSimpleProductToCartSingleMutationTest extends GraphQlAbstract
30
33
*/
31
34
private $ getCartItemOptionsFromUID ;
32
35
36
+ /**
37
+ * @var QuoteIdToMaskedQuoteIdInterface
38
+ */
39
+ private $ quoteIdToMaskedQuoteIdInterface ;
40
+
41
+ /**
42
+ * @var DataFixtureStorage
43
+ */
44
+ private $ fixtures ;
45
+
33
46
/**
34
47
* @inheritdoc
35
48
*/
@@ -41,6 +54,8 @@ protected function setUp(): void
41
54
$ this ->getCustomOptionsWithIDV2ForQueryBySku = $ objectManager ->get (
42
55
GetCustomOptionsWithUIDForQueryBySku::class
43
56
);
57
+ $ this ->quoteIdToMaskedQuoteIdInterface = $ objectManager ->get (QuoteIdToMaskedQuoteIdInterface::class);
58
+ $ this ->fixtures = $ objectManager ->get (DataFixtureStorageManager::class)->getStorage ();
44
59
}
45
60
46
61
/**
@@ -81,7 +96,7 @@ public function testAddSimpleProductWithOptions()
81
96
$ customizableOptionsOutput =
82
97
$ response ['addProductsToCart ' ]['cart ' ]['items ' ][0 ]['customizable_options ' ];
83
98
84
- foreach ($ customizableOptionsOutput as $ key => $ customizableOptionOutput ) {
99
+ foreach ($ customizableOptionsOutput as $ customizableOptionOutput ) {
85
100
$ customizableOptionOutputValues = [];
86
101
foreach ($ customizableOptionOutput ['values ' ] as $ customizableOptionOutputValue ) {
87
102
$ customizableOptionOutputValues [] = $ customizableOptionOutputValue ['value ' ];
@@ -220,6 +235,51 @@ public function testAddProductNotAssignedToWebsite(string $reservedOrderId, stri
220
235
self ::assertEquals ('PRODUCT_NOT_FOUND ' , $ response ['addProductsToCart ' ]['user_errors ' ][0 ]['code ' ]);
221
236
}
222
237
238
+ /**
239
+ * @magentoApiDataFixture Magento\Catalog\Test\Fixture\Product as:product1
240
+ * @magentoApiDataFixture Magento\Catalog\Test\Fixture\Product as:product2
241
+ * @magentoApiDataFixture Magento\Quote\Test\Fixture\GuestCart as:cart
242
+ */
243
+ public function testAddMultipleProductsToEmptyCart (): void
244
+ {
245
+ $ product1 = $ this ->fixtures ->get ('product1 ' );
246
+ $ product2 = $ this ->fixtures ->get ('product2 ' );
247
+ $ cart = $ this ->fixtures ->get ('cart ' );
248
+ $ maskedQuoteId = $ this ->quoteIdToMaskedQuoteIdInterface ->execute ((int ) $ cart ->getId ());
249
+ $ query = $ this ->getAddMultipleProductsToCartAndReturnCartTotalsMutation (
250
+ $ maskedQuoteId ,
251
+ [
252
+ [
253
+ 'sku ' => $ product1 ->getSku (),
254
+ 'quantity ' => 2
255
+ ],
256
+ [
257
+ 'sku ' => $ product2 ->getSku (),
258
+ 'quantity ' => 3
259
+ ]
260
+ ]
261
+ );
262
+ $ response = $ this ->graphQlMutation ($ query );
263
+ $ result = $ response ['addProductsToCart ' ];
264
+ self ::assertEmpty ($ result ['user_errors ' ]);
265
+ self ::assertNotEmpty ($ result ['cart ' ]['items ' ]);
266
+
267
+ $ cartItem = $ result ['cart ' ]['items ' ][0 ];
268
+ self ::assertEquals ($ product1 ->getSku (), $ cartItem ['product ' ]['sku ' ]);
269
+ self ::assertEquals (2 , $ cartItem ['quantity ' ]);
270
+ self ::assertEquals (10 , $ cartItem ['prices ' ]['price ' ]['value ' ]);
271
+ self ::assertEquals (20 , $ cartItem ['prices ' ]['row_total ' ]['value ' ]);
272
+
273
+ $ cartItem = $ result ['cart ' ]['items ' ][1 ];
274
+ self ::assertEquals ($ product2 ->getSku (), $ cartItem ['product ' ]['sku ' ]);
275
+ self ::assertEquals (3 , $ cartItem ['quantity ' ]);
276
+ self ::assertEquals (10 , $ cartItem ['prices ' ]['price ' ]['value ' ]);
277
+ self ::assertEquals (30 , $ cartItem ['prices ' ]['row_total ' ]['value ' ]);
278
+
279
+ $ cartTotals = $ result ['cart ' ]['prices ' ];
280
+ self ::assertEquals (50 , $ cartTotals ['grand_total ' ]['value ' ]);
281
+ }
282
+
223
283
/**
224
284
* @return array
225
285
*/
@@ -317,6 +377,61 @@ private function getAddToCartMutation(
317
377
}
318
378
}
319
379
}
380
+ MUTATION ;
381
+ }
382
+
383
+ /**
384
+ * Returns GraphQl mutation for addProductsToCart with cart totals
385
+ *
386
+ * @param string $maskedQuoteId
387
+ * @param array $cartItems
388
+ * @return string
389
+ */
390
+ private function getAddMultipleProductsToCartAndReturnCartTotalsMutation (
391
+ string $ maskedQuoteId ,
392
+ array $ cartItems
393
+ ): string {
394
+ $ cartItemsQuery = preg_replace (
395
+ '/"([^"]+)"\s*:\s*/ ' ,
396
+ '$1: ' ,
397
+ json_encode ($ cartItems )
398
+ );
399
+ return <<<MUTATION
400
+ mutation {
401
+ addProductsToCart(
402
+ cartId: " {$ maskedQuoteId }",
403
+ cartItems: $ cartItemsQuery
404
+ ) {
405
+ cart {
406
+ items {
407
+ product {
408
+ sku
409
+ }
410
+ quantity
411
+ prices {
412
+ price {
413
+ value
414
+ currency
415
+ }
416
+ row_total {
417
+ value
418
+ currency
419
+ }
420
+ }
421
+ }
422
+ prices {
423
+ grand_total {
424
+ value
425
+ currency
426
+ }
427
+ }
428
+ },
429
+ user_errors {
430
+ code
431
+ message
432
+ }
433
+ }
434
+ }
320
435
MUTATION ;
321
436
}
322
437
}
0 commit comments