|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\ConfigurableProduct; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | + |
| 15 | +/** |
| 16 | + * Add configurable product to cart testcases |
| 17 | + */ |
| 18 | +class AddConfigurableProductsWithDifferentParentsToCartTest extends GraphQlAbstract |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 22 | + */ |
| 23 | + private $getMaskedQuoteIdByReservedOrderId; |
| 24 | + |
| 25 | + /** |
| 26 | + * @inheritdoc |
| 27 | + */ |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $objectManager = Bootstrap::getObjectManager(); |
| 31 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_products.php |
| 36 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php |
| 37 | + */ |
| 38 | + public function testAddMultipleConfigurableProductsWithDifferentParentsToCart() |
| 39 | + { |
| 40 | + $searchResponse = $this->graphQlQuery($this->getFetchProductQuery('configurable')); |
| 41 | + $productOne = current($searchResponse['products']['items']); |
| 42 | + $searchResponse = $this->graphQlQuery($this->getFetchProductQuery('configurable_12345')); |
| 43 | + $productTwo = current($searchResponse['products']['items']); |
| 44 | + |
| 45 | + $quantityOne = 1; |
| 46 | + $quantityTwo = 2; |
| 47 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1'); |
| 48 | + $parentSkuOne = $productOne['sku']; |
| 49 | + $parentSkuTwo = $productTwo['sku']; |
| 50 | + $skuOne = 'simple_10'; |
| 51 | + $skuTwo = 'simple_30'; |
| 52 | + |
| 53 | + $query = <<<QUERY |
| 54 | + mutation { |
| 55 | + addConfigurableProductsToCart(input:{ |
| 56 | + cart_id:"{$maskedQuoteId}" |
| 57 | + cart_items:[ |
| 58 | + { |
| 59 | + parent_sku:"{$parentSkuOne}" |
| 60 | + data:{ |
| 61 | + sku:"{$skuOne}" |
| 62 | + quantity:{$quantityOne} |
| 63 | + } |
| 64 | + } |
| 65 | + { |
| 66 | + parent_sku:"{$parentSkuTwo}" |
| 67 | + data:{ |
| 68 | + sku:"{$skuTwo}" |
| 69 | + quantity:{$quantityTwo} |
| 70 | + } |
| 71 | + } |
| 72 | + ] |
| 73 | + }) { |
| 74 | + cart { |
| 75 | + items { |
| 76 | + id |
| 77 | + quantity |
| 78 | + product { |
| 79 | + sku |
| 80 | + } |
| 81 | + ... on ConfigurableCartItem { |
| 82 | + configurable_options { |
| 83 | + option_label |
| 84 | + value_label |
| 85 | + value_id |
| 86 | + } |
| 87 | + configured_variant { |
| 88 | + sku |
| 89 | + varchar_attribute |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + QUERY; |
| 97 | + |
| 98 | + $response = $this->graphQlMutation($query); |
| 99 | + |
| 100 | + $cartItems = $response['addConfigurableProductsToCart']['cart']['items']; |
| 101 | + self::assertCount(2, $cartItems); |
| 102 | + $firstCartItem = $cartItems[0]; |
| 103 | + self::assertEquals($quantityOne, $firstCartItem['quantity']); |
| 104 | + $secondCartItem = $cartItems[1]; |
| 105 | + self::assertEquals($quantityTwo, $secondCartItem['quantity']); |
| 106 | + } |
| 107 | + |
| 108 | + private function getFetchProductQuery(string $sku): string |
| 109 | + { |
| 110 | + return <<<QUERY |
| 111 | + { |
| 112 | + products( |
| 113 | + filter: {sku: {eq: "$sku"}} |
| 114 | + pageSize:1 |
| 115 | + ) { |
| 116 | + items { |
| 117 | + sku |
| 118 | + ... on ConfigurableProduct { |
| 119 | + configurable_options { |
| 120 | + attribute_id |
| 121 | + attribute_code |
| 122 | + id |
| 123 | + label |
| 124 | + position |
| 125 | + product_id |
| 126 | + use_default |
| 127 | + values { |
| 128 | + default_label |
| 129 | + label |
| 130 | + store_label |
| 131 | + use_default_value |
| 132 | + value_index |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + QUERY; |
| 140 | + } |
| 141 | +} |
0 commit comments