@@ -105,6 +105,64 @@ public function testAddProductWithOptionsResponse()
105105 );
106106 }
107107
108+ #[
109+ DataFixture(GuestCart::class, as: 'quote ' ),
110+ DataFixture(QuoteIdMask::class, ['cart_id ' => '$quote.id$ ' ], 'quoteIdMask ' ),
111+ DataFixture(
112+ Product::class,
113+ [
114+ 'sku ' => 'simple1 ' ,
115+ 'options ' => [
116+ [
117+ 'title ' => 'option1 ' ,
118+ 'type ' => ProductCustomOptionInterface::OPTION_TYPE_FIELD ,
119+ ]
120+ ]
121+ ],
122+ 'product1 '
123+ ),
124+ DataFixture(Indexer::class, as: 'indexer ' )
125+ ]
126+ public function testAddSameProductWithDifferentOptionValues ()
127+ {
128+ $ uidEncoder = Bootstrap::getObjectManager ()->create (Uid::class);
129+
130+ $ cartId = DataFixtureStorageManager::getStorage ()->get ('quoteIdMask ' )->getMaskedId ();
131+ $ product = DataFixtureStorageManager::getStorage ()->get ('product1 ' );
132+ /* @var \Magento\Catalog\Api\Data\ProductInterface $product */
133+ $ sku = $ product ->getSku ();
134+ $ option = $ product ->getOptions ();
135+ $ optionUid = $ uidEncoder ->encode (
136+ 'custom-option ' . '/ ' . $ option [0 ]->getData ()['option_id ' ]
137+ );
138+
139+ // Assert if product options for item added to the cart
140+ // are present in mutation response after product with selected option was added
141+ $ mutation = $ this ->getAddProductWithDifferentOptionValuesMutation (
142+ $ cartId ,
143+ $ sku ,
144+ $ optionUid
145+ );
146+ $ response = $ this ->graphQlMutation ($ mutation );
147+
148+ $ this ->assertArrayHasKey ('items ' , $ response ['addProductsToCart ' ]['cart ' ]);
149+ $ this ->assertCount (2 , $ response ['addProductsToCart ' ]['cart ' ]['items ' ]);
150+ $ this ->assertArrayHasKey ('customizable_options ' , $ response ['addProductsToCart ' ]['cart ' ]['items ' ][0 ]);
151+
152+ $ this ->assertEquals (
153+ $ response ['addProductsToCart ' ]['cart ' ]['items ' ],
154+ $ this ->getExpectedResponseForDifferentOptionValues ($ optionUid , $ sku )
155+ );
156+
157+ // Assert if product options for item in the cart are present in the response
158+ $ query = $ this ->getCartQueryForDifferentOptionValues ($ cartId );
159+ $ response = $ this ->graphQlQuery ($ query );
160+ $ this ->assertEquals (
161+ $ this ->getExpectedResponseForDifferentOptionValues ($ optionUid , $ sku ),
162+ $ response ['cart ' ]['items ' ]
163+ );
164+ }
165+
108166 #[
109167 DataFixture(GuestCart::class, as: 'quote ' ),
110168 DataFixture(QuoteIdMask::class, ['cart_id ' => '$quote.id$ ' ], 'quoteIdMask ' ),
@@ -268,7 +326,68 @@ private function getAddProductWithOptionMutation(string $cartId, string $sku, st
268326 }
269327 }
270328 }
271- }
329+ }
330+ QRY ;
331+ }
332+
333+ /**
334+ * Returns mutation for the test with different option values
335+ *
336+ * @param string $cartId
337+ * @param string $sku
338+ * @param string $optionUid
339+ * @return string
340+ */
341+ private function getAddProductWithDifferentOptionValuesMutation (
342+ string $ cartId ,
343+ string $ sku ,
344+ string $ optionUid
345+ ): string
346+ {
347+ return <<<QRY
348+ mutation {
349+ addProductsToCart(
350+ cartId: " {$ cartId }"
351+ cartItems: [
352+ {
353+ quantity:1
354+ sku: " {$ sku }"
355+ entered_options: [{
356+ uid:" {$ optionUid }",
357+ value:"value1"
358+ }]
359+ }
360+ {
361+ quantity:1
362+ sku: " {$ sku }"
363+ entered_options: [{
364+ uid:" {$ optionUid }",
365+ value:"value2"
366+ }]
367+ }
368+ ]
369+ ) {
370+ cart {
371+ id
372+ items {
373+ quantity
374+ product {
375+ sku
376+ }
377+ ... on SimpleCartItem {
378+ customizable_options {
379+ customizable_option_uid
380+ label
381+ values {
382+ customizable_option_value_uid
383+ value
384+ }
385+ }
386+ }
387+ }
388+ }
389+ }
390+ }
272391QRY ;
273392 }
274393
@@ -307,6 +426,39 @@ private function getCartQuery(string $cartId): string
307426QRY ;
308427 }
309428
429+ /**
430+ * Returns query to get cart with information about item and associated product with different option values
431+ *
432+ * @param string $cartId
433+ * @return string
434+ */
435+ private function getCartQueryForDifferentOptionValues (string $ cartId ): string
436+ {
437+ return <<<QRY
438+ query {
439+ cart(cart_id: " {$ cartId }")
440+ {
441+ items {
442+ quantity
443+ product {
444+ sku
445+ }
446+ ... on SimpleCartItem {
447+ customizable_options {
448+ customizable_option_uid
449+ label
450+ values {
451+ customizable_option_value_uid
452+ value
453+ }
454+ }
455+ }
456+ }
457+ }
458+ }
459+ QRY ;
460+ }
461+
310462 /**
311463 * Formats and returns expected response
312464 *
@@ -406,4 +558,49 @@ private function getExpectedResponse(string $selectedOptionUid, array $productOp
406558 ]
407559 ];
408560 }
561+
562+ /**
563+ * Returns formatted response for test with different option values
564+ *
565+ * @param string $selectedOptionUid
566+ * @param array $productOptions
567+ * @return array
568+ */
569+ private function getExpectedResponseForDifferentOptionValues (string $ optionUid , string $ sku ): array
570+ {
571+ return [
572+ 0 => [
573+ "quantity " => 1 ,
574+ "product " => ["sku " => "{$ sku }" ],
575+ "customizable_options " => [
576+ 0 => [
577+ "customizable_option_uid " => "{$ optionUid }" ,
578+ "label " => "option1 " ,
579+ "values " => [
580+ 0 => [
581+ "customizable_option_value_uid " => "{$ optionUid }" ,
582+ "value " => "value1 "
583+ ]
584+ ]
585+ ]
586+ ]
587+ ],
588+ 1 => [
589+ "quantity " => 1 ,
590+ "product " => ["sku " => "{$ sku }" ],
591+ "customizable_options " => [
592+ 0 => [
593+ "customizable_option_uid " => "{$ optionUid }" ,
594+ "label " => "option1 " ,
595+ "values " => [
596+ 0 => [
597+ "customizable_option_value_uid " => "{$ optionUid }" ,
598+ "value " => "value2 "
599+ ]
600+ ]
601+ ]
602+ ]
603+ ],
604+ ];
605+ }
409606}
0 commit comments