registry = Bootstrap::getObjectManager()->get(Registry::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->defaultStockProvider = Bootstrap::getObjectManager()->get(DefaultStockProviderInterface::class); $this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class); $this->orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class); $this->orderManagement = Bootstrap::getObjectManager()->get(OrderManagementInterface::class); } /** * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php * @magentoDataFixture ../../../../app/code/Magento/InventoryCatalog/Test/_files/source_items_on_default_source.php * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/quote.php * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php */ public function testPlaceOrderWithInStockProduct() { $sku = 'SKU-1'; $quoteItemQty = 4; $cart = $this->getCart(); $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/InventoryCatalog/Test/_files/source_items_on_default_source.php * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/quote.php * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php */ public function testPlaceOrderWithOutOffStockProduct() { $sku = 'SKU-1'; $quoteItemQty = 8.5; $cart = $this->getCart(); $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/InventoryCatalog/Test/_files/source_items_on_default_source.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/backorders 1 */ public function testPlaceOrderWithOutOffStockProductAndBackOrdersTurnedOn() { $sku = 'SKU-1'; $quoteItemQty = 8.5; $cart = $this->getCart(); $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/InventoryCatalog/Test/_files/source_items_on_default_source.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 */ public function testPlaceOrderWithOutOffStockProductAndManageStockTurnedOff() { $sku = 'SKU-1'; $quoteItemQty = 8; $cart = $this->getCart(); $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); } /** * @return CartInterface */ private function getCart(): CartInterface { $searchCriteria = $this->searchCriteriaBuilder ->addFilter('reserved_order_id', 'test_order_1') ->create(); /** @var CartInterface $cart */ $cart = current($this->cartRepository->getList($searchCriteria)->getItems()); $cart->setStoreId(1); 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; } protected function tearDown() { $this->cleanupReservations->execute(); } }