|
| 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\CatalogInventory; |
| 9 | + |
| 10 | +use Magento\TestFramework\Helper\Bootstrap; |
| 11 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 12 | +use Magento\Quote\Model\QuoteFactory; |
| 13 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 14 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 15 | + |
| 16 | +class AddProductToCartTest extends GraphQlAbstract |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var QuoteResource |
| 20 | + */ |
| 21 | + private $quoteResource; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var QuoteFactory |
| 25 | + */ |
| 26 | + private $quoteFactory; |
| 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->quoteFactory = $objectManager->get(QuoteFactory::class); |
| 41 | + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @magentoApiDataFixture Magento/Catalog/_files/products.php |
| 46 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php |
| 47 | + * @expectedException \Exception |
| 48 | + * @expectedExceptionMessage The requested qty is not available |
| 49 | + */ |
| 50 | + public function testAddProductIfQuantityIsNotAvailable() |
| 51 | + { |
| 52 | + $sku = 'simple'; |
| 53 | + $qty = 200; |
| 54 | + |
| 55 | + $maskedQuoteId = $this->getMaskedQuoteId(); |
| 56 | + $query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty); |
| 57 | + $this->graphQlQuery($query); |
| 58 | + self::fail('Should be "The requested qty is not available" error message.'); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @magentoApiDataFixture Magento/Catalog/_files/products.php |
| 63 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php |
| 64 | + * @magentoConfigFixture default cataloginventory/item_options/max_sale_qty 5 |
| 65 | + * @expectedException \Exception |
| 66 | + * @expectedExceptionMessage The most you may purchase is 5. |
| 67 | + */ |
| 68 | + public function testAddMoreProductsThatAllowed() |
| 69 | + { |
| 70 | + $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/167'); |
| 71 | + |
| 72 | + $sku = 'custom-design-simple-product'; |
| 73 | + $qty = 7; |
| 74 | + |
| 75 | + $maskedQuoteId = $this->getMaskedQuoteId(); |
| 76 | + $query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty); |
| 77 | + $this->graphQlQuery($query); |
| 78 | + self::fail('Should be "The most you may purchase is 5." error message.'); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return string |
| 83 | + */ |
| 84 | + public function getMaskedQuoteId() : string |
| 85 | + { |
| 86 | + $quote = $this->quoteFactory->create(); |
| 87 | + $this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id'); |
| 88 | + |
| 89 | + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param string $maskedQuoteId |
| 94 | + * @param string $sku |
| 95 | + * @param int $qty |
| 96 | + * @return string |
| 97 | + */ |
| 98 | + public function getAddSimpleProductQuery(string $maskedQuoteId, string $sku, int $qty) : string |
| 99 | + { |
| 100 | + return <<<QUERY |
| 101 | +mutation { |
| 102 | + addSimpleProductsToCart( |
| 103 | + input: { |
| 104 | + cart_id: "{$maskedQuoteId}", |
| 105 | + cartItems: [ |
| 106 | + { |
| 107 | + data: { |
| 108 | + qty: $qty |
| 109 | + sku: "$sku" |
| 110 | + } |
| 111 | + } |
| 112 | + ] |
| 113 | + } |
| 114 | + ) { |
| 115 | + cart { |
| 116 | + items { |
| 117 | + qty |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | +QUERY; |
| 123 | + } |
| 124 | +} |
0 commit comments