Skip to content

Commit

Permalink
Fixed place order on not default stock test
Browse files Browse the repository at this point in the history
  • Loading branch information
VitaliyBoyko committed Mar 20, 2018
1 parent b25ce41 commit f70c9d4
Showing 1 changed file with 22 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
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\InventoryReservations\Model\CleanupReservationsInterface;
use Magento\InventoryReservations\Model\ReservationBuilderInterface;
use Magento\InventoryReservationsApi\Api\AppendReservationsInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\InventoryApi\Api\StockRepositoryInterface;

Expand Down Expand Up @@ -62,16 +62,6 @@ class PlaceOrderOnNotDefaultStockTest extends TestCase
*/
private $cartItemFactory;

/**
* @var AppendReservationsInterface
*/
private $appendReservations;

/**
* @var ReservationBuilderInterface
*/
private $reservationBuilder;

/**
* @var StockRepositoryInterface
*/
Expand All @@ -97,20 +87,30 @@ class PlaceOrderOnNotDefaultStockTest extends TestCase
*/
private $registry;

/**
* @var OrderManagementInterface
*/
private $orderManagement;

/**
* @var StoreManagerInterface
*/
private $storeManager;

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->appendReservations = Bootstrap::getObjectManager()->get(AppendReservationsInterface::class);
$this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class);
$this->orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class);
$this->orderManagement = Bootstrap::getObjectManager()->get(OrderManagementInterface::class);
}

/**
Expand All @@ -130,13 +130,9 @@ protected function setUp()
*/
public function testPlaceOrderWithInStockProduct()
{
$this->markTestSkipped(
'Fix Integration Test for placing Order https://github.com/magento-engcom/msi/issues/687'
);
$sku = 'SKU-1';
$stockId = 10;
$quoteItemQty = 4;
$reservedDuringCheckoutQty = 1.5;
$sku = 'SKU-2';
$stockId = 30;
$quoteItemQty = 2.2;

$cart = $this->getCartByStockId($stockId);
$product = $this->productRepository->get($sku);
Expand All @@ -154,15 +150,10 @@ public function testPlaceOrderWithInStockProduct()
);
$cart->addItem($cartItem);
$this->cartRepository->save($cart);

$this->appendReservation($sku, -$reservedDuringCheckoutQty, $stockId);

$orderId = $this->cartManagement->placeOrder($cart->getId());

self::assertNotNull($orderId);

//cleanup
$this->appendReservation($sku, $reservedDuringCheckoutQty, $stockId);
$this->deleteOrderById((int)$orderId);
}

Expand All @@ -183,13 +174,9 @@ public function testPlaceOrderWithInStockProduct()
*/
public function testPlaceOrderWithOutOffStockProduct()
{
$this->markTestSkipped(
'Fix Integration Test for placing Order https://github.com/magento-engcom/msi/issues/687'
);
$sku = 'SKU-1';
$stockId = 10;
$quoteItemQty = 4;
$reservedDuringCheckoutQty = 3.8;
$sku = 'SKU-2';
$stockId = 30;
$quoteItemQty = 6.2;

$cart = $this->getCartByStockId($stockId);
$product = $this->productRepository->get($sku);
Expand All @@ -208,32 +195,10 @@ public function testPlaceOrderWithOutOffStockProduct()
$cart->addItem($cartItem);
$this->cartRepository->save($cart);

//append reservation during checkout to make laking quantity in stock
$this->appendReservation($sku, -$reservedDuringCheckoutQty, $stockId);

self::expectException(LocalizedException::class);
$orderId = $this->cartManagement->placeOrder($cart->getId());

self::assertNull($orderId);

//cleanup
$this->appendReservation($sku, $reservedDuringCheckoutQty, $stockId);
}

/**
* @param string $productSku
* @param float $qty
* @param int $stockId
* @return void
* @throws CouldNotSaveException
* @throws InputException
* @throws ValidationException
*/
private function appendReservation(string $productSku, float $qty, int $stockId)
{
$this->appendReservations->execute([
$this->reservationBuilder->setStockId($stockId)->setSku($productSku)->setQuantity($qty)->build(),
]);
}

/**
Expand Down Expand Up @@ -261,6 +226,7 @@ private function getCartByStockId(int $stockId): CartInterface
}
/** @var StoreInterface $store */
$store = $this->storeRepository->get($storeCode);
$this->storeManager->setCurrentStore($storeCode);
$cart->setStoreId($store->getId());

return $cart;
Expand All @@ -273,6 +239,7 @@ 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);
Expand Down

0 comments on commit f70c9d4

Please sign in to comment.