|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\GraphQl\ConfigurableProduct; |
| 10 | + |
| 11 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
| 12 | +use Magento\Framework\Exception\NoSuchEntityException as NoSuchEntityException; |
| 13 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 14 | +use Magento\Quote\Model\Quote\Item; |
| 15 | +use Magento\Quote\Model\QuoteFactory; |
| 16 | +use Magento\Quote\Model\QuoteIdMaskFactory; |
| 17 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 20 | + |
| 21 | +/** |
| 22 | + * checks that qty of configurable product is updated in cart |
| 23 | + */ |
| 24 | +class UpdateConfigurableCartItemsTest extends GraphQlAbstract |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var QuoteIdMaskFactory |
| 28 | + */ |
| 29 | + protected $quoteIdMaskFactory; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 33 | + */ |
| 34 | + private $getMaskedQuoteIdByReservedOrderId; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var QuoteFactory |
| 38 | + */ |
| 39 | + private $quoteFactory; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var QuoteResource |
| 43 | + */ |
| 44 | + private $quoteResource; |
| 45 | + |
| 46 | + /** |
| 47 | + * @magentoApiDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php |
| 48 | + */ |
| 49 | + public function testUpdateConfigurableCartItemQuantity() |
| 50 | + { |
| 51 | + $reservedOrderId = 'test_cart_with_configurable'; |
| 52 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId); |
| 53 | + |
| 54 | + $productSku = 'simple_10'; |
| 55 | + $newQuantity = 123; |
| 56 | + $quoteItem = $this->getQuoteItemBySku($productSku, $reservedOrderId); |
| 57 | + |
| 58 | + $query = $this->getQuery($maskedQuoteId, (int)$quoteItem->getId(), $newQuantity); |
| 59 | + $response = $this->graphQlMutation($query); |
| 60 | + |
| 61 | + self::assertArrayHasKey('updateCartItems', $response); |
| 62 | + self::assertArrayHasKey('quantity', $response['updateCartItems']['cart']['items']['0']); |
| 63 | + self::assertEquals($newQuantity, $response['updateCartItems']['cart']['items']['0']['quantity']); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @inheritdoc |
| 68 | + */ |
| 69 | + protected function setUp() |
| 70 | + { |
| 71 | + $objectManager = Bootstrap::getObjectManager(); |
| 72 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 73 | + $this->quoteFactory = $objectManager->get(QuoteFactory::class); |
| 74 | + $this->quoteResource = $objectManager->get(QuoteResource::class); |
| 75 | + $this->quoteIdMaskFactory = Bootstrap::getObjectManager()->get(QuoteIdMaskFactory::class); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param string $maskedQuoteId |
| 80 | + * @param int $quoteItemId |
| 81 | + * @param int $newQuantity |
| 82 | + * @return string |
| 83 | + */ |
| 84 | + private function getQuery(string $maskedQuoteId, int $quoteItemId, int $newQuantity): string |
| 85 | + { |
| 86 | + return <<<QUERY |
| 87 | +mutation { |
| 88 | + updateCartItems(input: { |
| 89 | + cart_id:"$maskedQuoteId" |
| 90 | + cart_items: [ |
| 91 | + { |
| 92 | + cart_item_id: $quoteItemId |
| 93 | + quantity: $newQuantity |
| 94 | + } |
| 95 | + ] |
| 96 | + }) { |
| 97 | + cart { |
| 98 | + items { |
| 99 | + quantity |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | +QUERY; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns quote item by product SKU |
| 109 | + * |
| 110 | + * @param string $sku |
| 111 | + * @return Item|bool |
| 112 | + * @throws NoSuchEntityException |
| 113 | + */ |
| 114 | + private function getQuoteItemBySku(string $sku, string $reservedOrderId) |
| 115 | + { |
| 116 | + $quote = $this->quoteFactory->create(); |
| 117 | + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); |
| 118 | + $item = false; |
| 119 | + foreach ($quote->getAllItems() as $quoteItem) { |
| 120 | + if ($quoteItem->getSku() == $sku && $quoteItem->getProductType() == Configurable::TYPE_CODE && |
| 121 | + !$quoteItem->getParentItemId()) { |
| 122 | + $item = $quoteItem; |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return $item; |
| 128 | + } |
| 129 | +} |
0 commit comments