|
| 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\Quote; |
| 9 | + |
| 10 | +use Magento\TestFramework\Helper\Bootstrap; |
| 11 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 12 | +use Magento\Quote\Model\Quote; |
| 13 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 14 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 15 | + |
| 16 | +class AddConfigurableProductToCartTest extends GraphQlAbstract |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var QuoteResource |
| 20 | + */ |
| 21 | + private $quoteResource; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var Quote |
| 25 | + */ |
| 26 | + private $quote; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 30 | + */ |
| 31 | + private $quoteIdToMaskedId; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritdoc |
| 35 | + */ |
| 36 | + protected function setUp() |
| 37 | + { |
| 38 | + $objectManager = Bootstrap::getObjectManager(); |
| 39 | + $this->quoteResource = $objectManager->get(QuoteResource::class); |
| 40 | + $this->quote = $objectManager->create(Quote::class); |
| 41 | + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products.php |
| 46 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php |
| 47 | + */ |
| 48 | + public function testAddConfigurableProductToCart() |
| 49 | + { |
| 50 | + $variantSku = 'simple_41'; |
| 51 | + $qty = 2; |
| 52 | + |
| 53 | + $maskedQuoteId = $this->getMaskedQuoteId(); |
| 54 | + |
| 55 | + $query = $this->getAddConfigurableProductMutationQuery($maskedQuoteId, $variantSku, $qty); |
| 56 | + |
| 57 | + $response = $this->graphQlQuery($query); |
| 58 | + $cartItems = $response['addConfigurableProductsToCart']['cart']['items']; |
| 59 | + self::assertEquals($qty, $cartItems[0]['qty']); |
| 60 | + self::assertEquals($variantSku, $cartItems[0]['product']['sku']); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products.php |
| 65 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php |
| 66 | + * @expectedException \Exception |
| 67 | + * @expectedExceptionMessage The requested qty is not available |
| 68 | + */ |
| 69 | + public function testAddProductIfQuantityIsNotAvailable() |
| 70 | + { |
| 71 | + $variantSku = 'simple_41'; |
| 72 | + $qty = 200; |
| 73 | + |
| 74 | + $maskedQuoteId = $this->getMaskedQuoteId(); |
| 75 | + $query = $this->getAddConfigurableProductMutationQuery($maskedQuoteId, $variantSku, $qty); |
| 76 | + |
| 77 | + $this->graphQlQuery($query); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @magentoApiDataFixture Magento/Framework/Search/_files/product_configurable.php |
| 82 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php |
| 83 | + * @expectedException \Exception |
| 84 | + * @expectedExceptionMessage Product that you are trying to add is not available. |
| 85 | + */ |
| 86 | + public function testAddOutOfStockProduct() |
| 87 | + { |
| 88 | + $variantSku = 'simple_1010'; |
| 89 | + $qty = 1; |
| 90 | + $maskedQuoteId = $this->getMaskedQuoteId(); |
| 91 | + $query = $this->getAddConfigurableProductMutationQuery($maskedQuoteId, $variantSku, $qty); |
| 92 | + |
| 93 | + $this->graphQlQuery($query); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php |
| 98 | + * @return string |
| 99 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 100 | + */ |
| 101 | + private function getMaskedQuoteId() |
| 102 | + { |
| 103 | + $this->quoteResource->load( |
| 104 | + $this->quote, |
| 105 | + 'test_order_1', |
| 106 | + 'reserved_order_id' |
| 107 | + ); |
| 108 | + return $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param string $maskedQuoteId |
| 113 | + * @param string $sku |
| 114 | + * @param int $qty |
| 115 | + * |
| 116 | + * @return string |
| 117 | + */ |
| 118 | + private function getAddConfigurableProductMutationQuery(string $maskedQuoteId, string $variantSku, int $qty): string |
| 119 | + { |
| 120 | + return <<<QUERY |
| 121 | +mutation { |
| 122 | + addConfigurableProductsToCart( |
| 123 | + input: { |
| 124 | + cart_id: "{$maskedQuoteId}" |
| 125 | + cartItems: [ |
| 126 | + { |
| 127 | + variant_sku: "{$variantSku}" |
| 128 | + data: { |
| 129 | + qty: {$qty} |
| 130 | + sku: "{$variantSku}" |
| 131 | + } |
| 132 | + } |
| 133 | + ] |
| 134 | + } |
| 135 | + ) { |
| 136 | + cart { |
| 137 | + items { |
| 138 | + id |
| 139 | + qty |
| 140 | + product { |
| 141 | + name |
| 142 | + sku |
| 143 | + } |
| 144 | + ... on ConfigurableCartItem { |
| 145 | + configurable_options { |
| 146 | + option_label |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | +QUERY; |
| 154 | + } |
| 155 | +} |
0 commit comments