Skip to content

Commit

Permalink
Merge pull request magento#970 from magento-engcom/MSI-918
Browse files Browse the repository at this point in the history
MSI-918: Need to refactor test ReservationPlacingDuringRegisterProductsSaleTest::testRegisterProductsSale as we don't make source deduction at the time of RegisterSales Anymore.
  • Loading branch information
maghamed authored Apr 21, 2018
2 parents 3a195cf + bc8aba0 commit d1ece47
Showing 1 changed file with 184 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,130 @@

namespace Magento\InventorySales\Test\Integration\StockManagement;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\CatalogInventory\Model\StockManagement;
use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Registry;
use Magento\InventoryReservations\Model\CleanupReservationsInterface;
use Magento\InventoryReservations\Model\GetReservationsQuantityInterface;
use Magento\InventoryReservationsApi\Api\AppendReservationsInterface;
use Magento\InventoryReservationsApi\Api\Data\ReservationInterface;
use Magento\InventoryReservationsApi\Api\ReservationBuilderInterface;
use Magento\InventorySalesApi\Api\Data\SalesEventInterface;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\Store\Api\WebsiteRepositoryInterface;
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\Model\StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Tests correct Product Salable Quantity decreasing after Order placing.
*
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ReservationPlacingDuringRegisterProductsSaleTest extends TestCase
{
/**
* @var AppendReservationsInterface
*/
private $appendReservations;

/**
* @var CartItemInterfaceFactory
*/
private $cartItemFactory;

/**
* @var CartManagementInterface
*/
private $cartManagement;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @var CleanupReservationsInterface
*/
private $cleanupReservations;

/**
* @var GetProductSalableQtyInterface
*/
private $getProductSalableQty;

/**
* @var ProductRepositoryInterface
* @var GetReservationsQuantityInterface
*/
private $productRepository;
private $getReservationsQuantity;

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

/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* @var StockRepositoryInterface
* @var ProductRepositoryInterface
*/
private $stockRepository;
private $productRepository;

/**
* @var WebsiteRepositoryInterface
* @var Registry
*/
private $websiteRepository;
private $registry;

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

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

/**
* @var CleanupReservationsInterface
* @var SearchCriteriaBuilder
*/
private $cleanupReservations;
private $searchCriteriaBuilder;

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

protected function setUp()
{
$this->appendReservations = Bootstrap::getObjectManager()->get(AppendReservationsInterface::class);
$this->cartItemFactory = Bootstrap::getObjectManager()->get(CartItemInterfaceFactory::class);
$this->cartManagement = Bootstrap::getObjectManager()->get(CartManagementInterface::class);
$this->cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
$this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
$this->getProductSalableQty = Bootstrap::getObjectManager()->get(GetProductSalableQtyInterface::class);
$this->getReservationsQuantity = Bootstrap::getObjectManager()->get(GetReservationsQuantityInterface::class);
$this->orderManagement = Bootstrap::getObjectManager()->get(OrderManagementInterface::class);
$this->orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class);
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
$this->stockRepository = Bootstrap::getObjectManager()->get(StockRepositoryInterface::class);
$this->websiteRepository = Bootstrap::getObjectManager()->get(WebsiteRepositoryInterface::class);
$this->registry = Bootstrap::getObjectManager()->get(Registry::class);
$this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class);
$this->appendReservations = Bootstrap::getObjectManager()->get(AppendReservationsInterface::class);
$this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
$this->stockManagement = Bootstrap::getObjectManager()->get(StockManagement::class);
$this->resourceConnection = Bootstrap::getObjectManager()->get(ResourceConnection::class);
$this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
$this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
}

/**
Expand All @@ -81,6 +142,8 @@ protected function tearDown()
}

/**
* Tests correct Product Salable Quantity decreasing after Order placing.
*
* @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
Expand All @@ -89,21 +152,114 @@ protected function tearDown()
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.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
*/
public function testRegisterProductsSale()
{
$this->markTestSkipped('https://github.com/magento-engcom/msi/issues/918');
$this->storeManager->setCurrentStore('store_for_eu_website');
$sku = 'SKU-1';
$quoteItemQty = 3.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::assertEquals(8.5, $this->getProductSalableQty->execute('SKU-1', 10));
self::assertEquals(0, $this->getReservationsQuantity->execute('SKU-1', 10));

$product = $this->productRepository->get('SKU-1');
$website = $this->websiteRepository->get('eu_website');
$this->stockManagement->registerProductsSale([$product->getId() => 3.5], $website->getId());
$orderId = $this->cartManagement->placeOrder($cart->getId());

self::assertEquals(5, $this->getProductSalableQty->execute('SKU-1', 10));
self::assertEquals(-3.5, $this->getReservationsQuantity->execute('SKU-1', 10));
self::assertEquals(
sprintf(
'%s:%s:%d',
SalesEventInterface::EVENT_ORDER_PLACED,
SalesEventInterface::OBJECT_TYPE_ORDER,
$orderId
),
$this->getReservationMetadata()
);

//cleanup
$this->deleteOrderById((int)$orderId);
}

/**
* Get Cart for placing Order.
*
* @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);

$this->appendReservations->execute([
// unreserved 3.5 units for cleanup
$this->reservationBuilder->setStockId(10)->setSku('SKU-1')->setQuantity(3.5)->build(),
]);
return $cart;
}

/**
* Create Cart Item from Product and Quantity.
*
* @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;
}

/**
* Rollback created Order.
*
* @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);
}

/**
* Get "metadata" field value of last created Inventory Reservation.
*
* @return string
*/
private function getReservationMetadata(): string
{
$connection = $this->resourceConnection->getConnection();
$select = $connection->select()->from(
['inventory_reservation' => $this->resourceConnection->getTableName('inventory_reservation')],
['metadata']
)->order(
ReservationInterface::RESERVATION_ID . ' DESC'
);
$result = $connection->fetchOne($select);
return $result;
}
}

0 comments on commit d1ece47

Please sign in to comment.