123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\InventorySales\Test\Integration\Order;
- use Magento\Catalog\Api\Data\ProductInterface;
- use Magento\Framework\Api\SearchCriteriaBuilder;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Registry;
- use Magento\InventoryApi\Api\Data\StockInterface;
- use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
- use Magento\InventoryConfigurationApi\Api\SaveStockItemConfigurationInterface;
- use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
- use Magento\Quote\Api\CartManagementInterface;
- use Magento\Quote\Api\CartRepositoryInterface;
- use Magento\Quote\Api\Data\CartInterface;
- use Magento\Quote\Api\Data\CartItemInterface;
- use Magento\Quote\Api\Data\CartItemInterfaceFactory;
- use Magento\Sales\Api\OrderManagementInterface;
- use Magento\Sales\Api\OrderRepositoryInterface;
- use Magento\Store\Api\Data\StoreInterface;
- use Magento\Store\Api\StoreRepositoryInterface;
- use Magento\Store\Model\StoreManagerInterface;
- use PHPUnit\Framework\TestCase;
- use Magento\TestFramework\Helper\Bootstrap;
- use Magento\InventoryReservationsApi\Model\CleanupReservationsInterface;
- use Magento\Catalog\Api\ProductRepositoryInterface;
- use Magento\InventoryApi\Api\StockRepositoryInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class PlaceOrderOnNotDefaultStockTest extends TestCase
- {
- /**
- * @var CleanupReservationsInterface
- */
- private $cleanupReservations;
- /**
- * @var ProductRepositoryInterface
- */
- private $productRepository;
- /**
- * @var CartManagementInterface
- */
- private $cartManagement;
- /**
- * @var CartRepositoryInterface
- */
- private $cartRepository;
- /**
- * @var CartItemInterfaceFactory
- */
- private $cartItemFactory;
- /**
- * @var StockRepositoryInterface
- */
- private $stockRepository;
- /**
- * @var StoreRepositoryInterface
- */
- private $storeRepository;
- /**
- * @var SearchCriteriaBuilder
- */
- private $searchCriteriaBuilder;
- /**
- * @var OrderRepositoryInterface
- */
- private $orderRepository;
- /**
- * @var Registry
- */
- private $registry;
- /**
- * @var OrderManagementInterface
- */
- private $orderManagement;
- /**
- * @var StoreManagerInterface
- */
- private $storeManager;
- /**
- * @var GetStockItemConfigurationInterface
- */
- private $getStockItemConfiguration;
- /**
- * @var SaveStockItemConfigurationInterface
- */
- private $saveStockItemConfiguration;
- protected function setUp()
- {
- $this->registry = Bootstrap::getObjectManager()->get(Registry::class);
- $this->stockRepository = Bootstrap::getObjectManager()->get(StockRepositoryInterface::class);
- $this->storeRepository = Bootstrap::getObjectManager()->get(StoreRepositoryInterface::class);
- $this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
- $this->cartManagement = Bootstrap::getObjectManager()->get(CartManagementInterface::class);
- $this->cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
- $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
- $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
- $this->cartItemFactory = Bootstrap::getObjectManager()->get(CartItemInterfaceFactory::class);
- $this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
- $this->orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class);
- $this->orderManagement = Bootstrap::getObjectManager()->get(OrderManagementInterface::class);
- $this->getStockItemConfiguration =
- Bootstrap::getObjectManager()->get(GetStockItemConfigurationInterface::class);
- $this->saveStockItemConfiguration =
- Bootstrap::getObjectManager()->get(SaveStockItemConfigurationInterface::class);
- }
- /**
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/quote.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
- *
- * @magentoDbIsolation disabled
- */
- public function testPlaceOrderWithInStockProduct()
- {
- $sku = 'SKU-2';
- $stockId = 30;
- $quoteItemQty = 2.2;
- $this->setStockItemConfigIsDecimal($sku, $stockId);
- $cart = $this->getCartByStockId($stockId);
- $product = $this->productRepository->get($sku);
- $cartItem = $this->getCartItem($product, $quoteItemQty, (int)$cart->getId());
- $cart->addItem($cartItem);
- $this->cartRepository->save($cart);
- $orderId = $this->cartManagement->placeOrder($cart->getId());
- self::assertNotNull($orderId);
- $this->deleteOrderById((int)$orderId);
- }
- /**
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/quote.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
- *
- * @magentoDbIsolation disabled
- */
- public function testPlaceOrderWithOutOffStockProduct()
- {
- $sku = 'SKU-2';
- $stockId = 30;
- $quoteItemQty = 6.2;
- $this->setStockItemConfigIsDecimal($sku, $stockId);
- $cart = $this->getCartByStockId($stockId);
- $product = $this->productRepository->get($sku);
- $cartItem = $this->getCartItem($product, $quoteItemQty, (int)$cart->getId());
- $cart->addItem($cartItem);
- $this->cartRepository->save($cart);
- self::expectException(LocalizedException::class);
- $orderId = $this->cartManagement->placeOrder($cart->getId());
- self::assertNull($orderId);
- }
- /**
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/quote.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
- * @magentoConfigFixture store_for_global_website_store cataloginventory/item_options/backorders 1
- *
- * @magentoDbIsolation disabled
- */
- public function testPlaceOrderWithOutOffStockProductAndBackOrdersTurnedOn()
- {
- $sku = 'SKU-2';
- $stockId = 30;
- $quoteItemQty = 6.5;
- $this->setStockItemConfigIsDecimal($sku, $stockId);
- $cart = $this->getCartByStockId($stockId);
- $product = $this->productRepository->get($sku);
- $cartItem = $this->getCartItem($product, $quoteItemQty, (int)$cart->getId());
- $cart->addItem($cartItem);
- $this->cartRepository->save($cart);
- $orderId = $this->cartManagement->placeOrder($cart->getId());
- self::assertNotNull($orderId);
- //cleanup
- $this->deleteOrderById((int)$orderId);
- }
- /**
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
- * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/quote.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
- * @magentoConfigFixture current_store cataloginventory/item_options/manage_stock 0
- *
- * @magentoDbIsolation disabled
- */
- public function testPlaceOrderWithOutOffStockProductAndManageStockTurnedOff()
- {
- $sku = 'SKU-2';
- $stockId = 30;
- $quoteItemQty = 6.5;
- $this->setStockItemConfigIsDecimal($sku, $stockId);
- $cart = $this->getCartByStockId($stockId);
- $product = $this->productRepository->get($sku);
- $cartItem = $this->getCartItem($product, $quoteItemQty, (int)$cart->getId());
- $cart->addItem($cartItem);
- $this->cartRepository->save($cart);
- $orderId = $this->cartManagement->placeOrder($cart->getId());
- self::assertNotNull($orderId);
- //cleanup
- $this->deleteOrderById((int)$orderId);
- }
- /**
- * @param int $stockId
- * @return CartInterface
- */
- private function getCartByStockId(int $stockId): CartInterface
- {
- $searchCriteria = $this->searchCriteriaBuilder
- ->addFilter('reserved_order_id', 'test_order_1')
- ->create();
- /** @var CartInterface $cart */
- $cart = current($this->cartRepository->getList($searchCriteria)->getItems());
- /** @var StockInterface $stock */
- $stock = $this->stockRepository->get($stockId);
- /** @var SalesChannelInterface[] $salesChannels */
- $salesChannels = $stock->getExtensionAttributes()->getSalesChannels();
- $storeCode = 'store_for_';
- foreach ($salesChannels as $salesChannel) {
- if ($salesChannel->getType() == SalesChannelInterface::TYPE_WEBSITE) {
- $storeCode .= $salesChannel->getCode();
- break;
- }
- }
- /** @var StoreInterface $store */
- $store = $this->storeRepository->get($storeCode);
- $this->storeManager->setCurrentStore($storeCode);
- $cart->setStoreId($store->getId());
- return $cart;
- }
- /**
- * @param int $orderId
- */
- private function deleteOrderById(int $orderId)
- {
- $this->registry->unregister('isSecureArea');
- $this->registry->register('isSecureArea', true);
- $this->orderManagement->cancel($orderId);
- $this->orderRepository->delete($this->orderRepository->get($orderId));
- $this->registry->unregister('isSecureArea');
- $this->registry->register('isSecureArea', false);
- }
- /**
- * @param ProductInterface $product
- * @param float $quoteItemQty
- * @param int $cartId
- * @return CartItemInterface
- */
- private function getCartItem(ProductInterface $product, float $quoteItemQty, int $cartId): CartItemInterface
- {
- /** @var CartItemInterface $cartItem */
- $cartItem =
- $this->cartItemFactory->create(
- [
- 'data' => [
- CartItemInterface::KEY_SKU => $product->getSku(),
- CartItemInterface::KEY_QTY => $quoteItemQty,
- CartItemInterface::KEY_QUOTE_ID => $cartId,
- 'product_id' => $product->getId(),
- 'product' => $product
- ]
- ]
- );
- return $cartItem;
- }
- /**
- * @param $sku
- * @param $stockId
- */
- private function setStockItemConfigIsDecimal(string $sku, int $stockId): void
- {
- $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
- $stockItemConfiguration->setIsQtyDecimal(true);
- $this->saveStockItemConfiguration->execute($sku, $stockId, $stockItemConfiguration);
- }
- protected function tearDown()
- {
- $this->cleanupReservations->execute();
- }
- }
|