|
| 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 Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 12 | +use Magento\Quote\Model\Quote\Item; |
| 13 | +use Magento\Quote\Model\QuoteFactory; |
| 14 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 17 | + |
| 18 | +/** |
| 19 | + * Remove configurable product from cart testcases |
| 20 | + */ |
| 21 | +class RemoveConfigurableProductFromCartTest extends GraphQlAbstract |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 25 | + */ |
| 26 | + private $getMaskedQuoteIdByReservedOrderId; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ProductRepositoryInterface |
| 30 | + */ |
| 31 | + private $productRepository; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var QuoteFactory |
| 35 | + */ |
| 36 | + private $quoteFactory; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var QuoteResource |
| 40 | + */ |
| 41 | + private $quoteResource; |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritdoc |
| 45 | + */ |
| 46 | + protected function setUp() |
| 47 | + { |
| 48 | + $objectManager = Bootstrap::getObjectManager(); |
| 49 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 50 | + $this->quoteFactory = $objectManager->get(QuoteFactory::class); |
| 51 | + $this->quoteResource = $objectManager->get(QuoteResource::class); |
| 52 | + $this->productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @magentoApiDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php |
| 57 | + */ |
| 58 | + public function testRemoveConfigurableProductFromCart() |
| 59 | + { |
| 60 | + $configurableOptionSku = 'simple_10'; |
| 61 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_cart_with_configurable'); |
| 62 | + $quoteItemId = $this->getQuoteItemIdBySku($configurableOptionSku); |
| 63 | + $query = $this->getQuery($maskedQuoteId, $quoteItemId); |
| 64 | + $response = $this->graphQlMutation($query); |
| 65 | + |
| 66 | + $this->assertArrayHasKey('cart', $response['removeItemFromCart']); |
| 67 | + $this->assertArrayHasKey('items', $response['removeItemFromCart']['cart']); |
| 68 | + $this->assertEquals(0, count($response['removeItemFromCart']['cart']['items'])); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param string $maskedQuoteId |
| 73 | + * @param int $itemId |
| 74 | + * @return string |
| 75 | + */ |
| 76 | + private function getQuery(string $maskedQuoteId, int $itemId): string |
| 77 | + { |
| 78 | + return <<<QUERY |
| 79 | +mutation { |
| 80 | + removeItemFromCart( |
| 81 | + input: { |
| 82 | + cart_id: "{$maskedQuoteId}" |
| 83 | + cart_item_id: {$itemId} |
| 84 | + } |
| 85 | + ) { |
| 86 | + cart { |
| 87 | + items { |
| 88 | + quantity |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +QUERY; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns quote item ID by product's SKU |
| 98 | + * |
| 99 | + * @param string $sku |
| 100 | + * @return int |
| 101 | + */ |
| 102 | + private function getQuoteItemIdBySku(string $sku): int |
| 103 | + { |
| 104 | + $quote = $this->quoteFactory->create(); |
| 105 | + $this->quoteResource->load($quote, 'test_cart_with_configurable', 'reserved_order_id'); |
| 106 | + /** @var Item $quoteItem */ |
| 107 | + $quoteItemsCollection = $quote->getItemsCollection(); |
| 108 | + foreach ($quoteItemsCollection->getItems() as $item) { |
| 109 | + if ($item->getSku() == $sku) { |
| 110 | + return (int)$item->getId(); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments