|
| 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\Catalog\Test\Fixture\Product as ProductFixture; |
| 11 | +use Magento\ConfigurableProduct\Test\Fixture\AddProductToCart as AddConfigurableProductToCartFixture; |
| 12 | +use Magento\ConfigurableProduct\Test\Fixture\Attribute as AttributeFixture; |
| 13 | +use Magento\ConfigurableProduct\Test\Fixture\Product as ConfigurableProductFixture; |
| 14 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 15 | +use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture; |
| 16 | +use Magento\TestFramework\Fixture\DataFixture; |
| 17 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 18 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 21 | + |
| 22 | +/** |
| 23 | + * Configurable product in cart testcases |
| 24 | + */ |
| 25 | +class QuoteConfigurableProductInCartTest extends GraphQlAbstract |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var DataFixtureStorage |
| 29 | + */ |
| 30 | + private $fixtures; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 34 | + */ |
| 35 | + private $quoteIdToMaskedQuoteIdInterface; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var \Magento\Catalog\Model\ProductRepository |
| 39 | + */ |
| 40 | + private $productRepository; |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritdoc |
| 44 | + */ |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + $objectManager = Bootstrap::getObjectManager(); |
| 48 | + $this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 49 | + $this->quoteIdToMaskedQuoteIdInterface = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); |
| 50 | + $this->productRepository = $objectManager->get(\Magento\Catalog\Model\ProductRepository::class); |
| 51 | + } |
| 52 | + |
| 53 | + #[ |
| 54 | + DataFixture(ProductFixture::class, as: 'p1'), |
| 55 | + DataFixture(ProductFixture::class, as: 'p2'), |
| 56 | + DataFixture(AttributeFixture::class, as: 'attr'), |
| 57 | + DataFixture( |
| 58 | + ConfigurableProductFixture::class, |
| 59 | + ['_options' => ['$attr$'], '_links' => ['$p1$', '$p2$']], |
| 60 | + 'cp1' |
| 61 | + ), |
| 62 | + DataFixture(GuestCartFixture::class, as: 'cart'), |
| 63 | + DataFixture( |
| 64 | + AddConfigurableProductToCartFixture::class, |
| 65 | + ['cart_id' => '$cart.id$', 'product_id' => '$cp1.id$', 'child_product_id' => '$p1.id$', 'qty' => 1], |
| 66 | + ), |
| 67 | + ] |
| 68 | + public function testConfigurableProductInCartAfterGoesOutOfStock() |
| 69 | + { |
| 70 | + $product1 = $this->fixtures->get('p1'); |
| 71 | + $product1 = $this->productRepository->get($product1->getSku(), true); |
| 72 | + $stockItem = $product1->getExtensionAttributes()->getStockItem(); |
| 73 | + $stockItem->setQty(0); |
| 74 | + $stockItem->setIsInStock(false); |
| 75 | + $this->productRepository->save($product1); |
| 76 | + |
| 77 | + $product2 = $this->fixtures->get('p2'); |
| 78 | + $product2 = $this->productRepository->get($product2->getSku(), true); |
| 79 | + $stockItem = $product2->getExtensionAttributes()->getStockItem(); |
| 80 | + $stockItem->setQty(0); |
| 81 | + $stockItem->setIsInStock(false); |
| 82 | + $this->productRepository->save($product2); |
| 83 | + $cart = $this->fixtures->get('cart'); |
| 84 | + $maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int)$cart->getId()); |
| 85 | + |
| 86 | + $query = <<<'QUERY' |
| 87 | +query GetCartDetails($cartId: String!) { |
| 88 | + cart(cart_id: $cartId) { |
| 89 | + id |
| 90 | + items { |
| 91 | + uid |
| 92 | + product { |
| 93 | + uid |
| 94 | + name |
| 95 | + sku |
| 96 | + stock_status |
| 97 | + price_range { |
| 98 | + minimum_price { |
| 99 | + final_price { |
| 100 | + currency |
| 101 | + value |
| 102 | + } |
| 103 | + regular_price { |
| 104 | + currency |
| 105 | + value |
| 106 | + } |
| 107 | + } |
| 108 | + maximum_price { |
| 109 | + final_price { |
| 110 | + currency |
| 111 | + value |
| 112 | + } |
| 113 | + regular_price { |
| 114 | + currency |
| 115 | + value |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + prices { |
| 121 | + price { |
| 122 | + currency |
| 123 | + value |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | +QUERY; |
| 130 | + |
| 131 | + $variables = [ |
| 132 | + 'cartId' => $maskedQuoteId |
| 133 | + ]; |
| 134 | + |
| 135 | + $response = $this->graphQlQuery($query, $variables); |
| 136 | + $this->assertEquals($maskedQuoteId, $response['cart']['id'], 'Assert that correct quote is queried'); |
| 137 | + $this->assertEquals( |
| 138 | + 'OUT_OF_STOCK', |
| 139 | + $response['cart']['items'][0]['product']['stock_status'], |
| 140 | + 'Assert product is out of stock' |
| 141 | + ); |
| 142 | + $this->assertEquals( |
| 143 | + 0, |
| 144 | + $response['cart']['items'][0]['product']['price_range']['minimum_price']['final_price']['value'], |
| 145 | + 'Assert that minimum price equals to 0' |
| 146 | + ); |
| 147 | + $this->assertEquals( |
| 148 | + 0, |
| 149 | + $response['cart']['items'][0]['product']['price_range']['maximum_price']['final_price']['value'], |
| 150 | + 'Assert that maximum price equals to 0' |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
0 commit comments