From 944d80327f98459606fa73090c7526fb24d25a42 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Thu, 26 Oct 2017 13:46:21 +0200 Subject: [PATCH 1/7] Added plugin for getting stock status for items with backorders enabled --- .../InventoryApi/Test/_files/products.php | 1 + .../Model/BackorderStockStatusPlugin.php | 69 +++++++++++++ .../Stock/IsBackorderedProductInStockTest.php | 99 +++++++++++++++++++ app/code/Magento/InventorySales/etc/di.xml | 12 +++ 4 files changed, 181 insertions(+) create mode 100644 app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php create mode 100644 app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php create mode 100644 app/code/Magento/InventorySales/etc/di.xml diff --git a/app/code/Magento/InventoryApi/Test/_files/products.php b/app/code/Magento/InventoryApi/Test/_files/products.php index 2a37fd7e69fc..4c04fdd9c642 100644 --- a/app/code/Magento/InventoryApi/Test/_files/products.php +++ b/app/code/Magento/InventoryApi/Test/_files/products.php @@ -14,6 +14,7 @@ $productFactory = $objectManager->get(ProductInterfaceFactory::class); /** @var ProductRepositoryInterface $productRepository */ $productRepository = $objectManager->get(ProductRepositoryInterface::class); +$productRepository->cleanCache(); for ($i = 1; $i <= 3; $i++) { $product = $productFactory->create(); diff --git a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php b/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php new file mode 100644 index 000000000000..111d0fece8eb --- /dev/null +++ b/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php @@ -0,0 +1,69 @@ +stockItemRepository = $stockItemRepository; + $this->stockItemCriteriaFactory = $stockItemCriteriaFactory; + $this->productRepository = $productRepository; + } + + /** + * Return true status if backorders is enabled for the item + * + * @param IsProductInStockInterface $subject + * @param callable $proceed + * @param string $sku + * @param int $stockId + * @return bool + */ + public function aroundExecute(IsProductInStockInterface $subject, callable $proceed, string $sku, int $stockId) + { + $productData = $this->productRepository->get($sku); + $productId = $productData->getId(); + + $stockItemCriteria = $this->stockItemCriteriaFactory->create(); + $stockItemCriteria->setProductsFilter($productId); + $stockItemsCollection = $this->stockItemRepository->getList($stockItemCriteria); + + /** @var Item $stockItem */ + $stockItem = current($stockItemsCollection->getItems()); + + if ($stockItem->getData('backorders') > 0) { + return true; + } + + return $proceed($sku, $stockId); + } +} diff --git a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php b/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php new file mode 100644 index 000000000000..f81f50747fc1 --- /dev/null +++ b/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php @@ -0,0 +1,99 @@ +productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class); + $this->stockItemRepository = Bootstrap::getObjectManager()->create(StockItemRepositoryInterface::class); + $this->stockItemCriteriaInterfaceFactory = Bootstrap::getObjectManager()->create( + StockItemCriteriaInterfaceFactory::class + ); + $this->isProductInStock = Bootstrap::getObjectManager()->create( + IsProductInStockInterface::class + ); + } + + /** + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php + * @magentoDbIsolation disabled + */ + public function testBackorderedZeroQtyProductIsInStock() + { + /** @var ProductInterface $product */ + $product = $this->productRepository->get('SKU-1'); + $stockItemSearchCriteria = $this->stockItemCriteriaInterfaceFactory->create(); + $stockItemSearchCriteria->setProductsFilter($product->getId()); + $stockItemsCollection = $this->stockItemRepository->getList($stockItemSearchCriteria); + + /** @var StockItemInterface $stockItem */ + $stockItem = current($stockItemsCollection->getItems()); + + $stockItem->setBackorders(1); + $stockItem->setUseConfigBackorders(1); + $stockItem->setQty(-15); + + $this->stockItemRepository->save($stockItem); + + $this->assertTrue($this->isProductInStock->execute('SKU-1', 1)); + } + + /** + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php + * @magentoDbIsolation disabled + * @magentoAppIsolation enabled + */ + public function testZeroQtyProductIsOutOfStock() + { + $this->productRepository->cleanCache(); + /** @var ProductInterface $product */ + $product = $this->productRepository->get('SKU-1'); + $stockItemSearchCriteria = $this->stockItemCriteriaInterfaceFactory->create(); + $stockItemSearchCriteria->setProductsFilter($product->getId()); + $stockItemsCollection = $this->stockItemRepository->getList($stockItemSearchCriteria); + + /** @var StockItemInterface $stockItem */ + $stockItem = current($stockItemsCollection->getItems()); + + $stockItem->setBackorders(0); + $stockItem->setUseConfigBackorders(0); + $stockItem->setQty(0); + + $this->stockItemRepository->save($stockItem); + + $this->assertFalse($this->isProductInStock->execute('SKU-1', 1)); + } +} diff --git a/app/code/Magento/InventorySales/etc/di.xml b/app/code/Magento/InventorySales/etc/di.xml new file mode 100644 index 000000000000..28a2824699b8 --- /dev/null +++ b/app/code/Magento/InventorySales/etc/di.xml @@ -0,0 +1,12 @@ + + + + + + + From 34027e6d2a182eab2de859c49b1ccace143a2d2f Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Thu, 26 Oct 2017 14:00:55 +0200 Subject: [PATCH 2/7] Header copyright added to the test --- .../Integration/Stock/IsBackorderedProductInStockTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php b/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php index f81f50747fc1..b5adefa89f43 100644 --- a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php +++ b/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php @@ -1,5 +1,8 @@ Date: Thu, 26 Oct 2017 14:27:54 +0200 Subject: [PATCH 3/7] Added strict types to the plugin --- .../Plugin/Model/BackorderStockStatusPlugin.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php b/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php index 111d0fece8eb..e3d3fc0dbce4 100644 --- a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php +++ b/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace Magento\InventorySales\Plugin\Model; @@ -48,8 +49,12 @@ public function __construct( * @param int $stockId * @return bool */ - public function aroundExecute(IsProductInStockInterface $subject, callable $proceed, string $sku, int $stockId) - { + public function aroundExecute( + IsProductInStockInterface $subject, + callable $proceed, + string $sku, + int $stockId + ): bool { $productData = $this->productRepository->get($sku); $productId = $productData->getId(); From bd49c0c2181f61b2c9c52f32f1e0ee158036b07c Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Thu, 26 Oct 2017 14:54:07 +0200 Subject: [PATCH 4/7] Properties types changed --- .../Plugin/Model/BackorderStockStatusPlugin.php | 6 +++--- .../Integration/Stock/IsBackorderedProductInStockTest.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php b/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php index e3d3fc0dbce4..2c58d5a6f4e7 100644 --- a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php +++ b/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php @@ -18,17 +18,17 @@ class BackorderStockStatusPlugin /** * @var StockItemRepository */ - protected $stockItemRepository; + private $stockItemRepository; /** * @var StockItemCriteriaInterfaceFactory */ - protected $stockItemCriteriaFactory; + private $stockItemCriteriaFactory; /** * @var ProductRepository */ - protected $productRepository; + private $productRepository; public function __construct( StockItemRepository $stockItemRepository, diff --git a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php b/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php index b5adefa89f43..a3f6d0ef822a 100644 --- a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php +++ b/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php @@ -21,17 +21,17 @@ class IsBackorderedProductInStockTest extends TestCase /** * @var ProductRepository */ - protected $productRepository; + private $productRepository; /** * @var GetProductQuantityInStockInterface */ - protected $isProductInStock; + private $isProductInStock; /** * @var StockItemRepositoryInterface */ - protected $stockItemRepository; + private $stockItemRepository; /** * @var StockItemCriteriaInterfaceFactory From ed34aa9787183b14dabe3ed0a822eafc90c009f4 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Thu, 26 Oct 2017 16:47:49 +0200 Subject: [PATCH 5/7] Improved integration tests --- .../Model/BackorderStockStatusPlugin.php | 1 + .../Stock/IsBackorderedProductInStockTest.php | 98 ++++++++++++++----- 2 files changed, 76 insertions(+), 23 deletions(-) diff --git a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php b/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php index 2c58d5a6f4e7..f77a1f5aa259 100644 --- a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php +++ b/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php @@ -48,6 +48,7 @@ public function __construct( * @param string $sku * @param int $stockId * @return bool + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundExecute( IsProductInStockInterface $subject, diff --git a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php b/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php index a3f6d0ef822a..64fda415d61a 100644 --- a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php +++ b/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php @@ -11,13 +11,42 @@ use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; use Magento\CatalogInventory\Api\StockItemRepositoryInterface; +use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\Framework\Api\SearchCriteriaInterface; +use Magento\Framework\Indexer\IndexerInterface; +use Magento\Inventory\Indexer\StockItemIndexerInterface; +use Magento\InventoryApi\Api\Data\SourceItemInterface; use Magento\InventoryApi\Api\GetProductQuantityInStockInterface; -use PHPUnit\Framework\TestCase; -use Magento\TestFramework\Helper\Bootstrap; use Magento\InventoryApi\Api\IsProductInStockInterface; +use Magento\InventoryApi\Api\SourceItemRepositoryInterface; +use Magento\InventoryApi\Api\SourceItemsSaveInterface; +use Magento\TestFramework\Helper\Bootstrap; +use PHPUnit\Framework\TestCase; class IsBackorderedProductInStockTest extends TestCase { + const PRODUCT_SKU = 'SKU-2'; + + /** + * @var SourceItemRepositoryInterface + */ + private $sourceItemRepository; + + /** + * @var SearchCriteriaBuilder + */ + private $searchCriteriaBuilder; + + /** + * @var SourceItemsSaveInterface + */ + private $sourceItemsSaveInterface; + + /** + * @var IndexerInterface + */ + private $indexer; + /** * @var ProductRepository */ @@ -45,6 +74,11 @@ protected function setUp() $this->stockItemCriteriaInterfaceFactory = Bootstrap::getObjectManager()->create( StockItemCriteriaInterfaceFactory::class ); + $this->sourceItemRepository = Bootstrap::getObjectManager()->create(SourceItemRepositoryInterface::class); + $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class); + $this->sourceItemsSaveInterface = Bootstrap::getObjectManager()->create(SourceItemsSaveInterface::class); + $this->indexer = Bootstrap::getObjectManager()->create(IndexerInterface::class); + $this->indexer->load(StockItemIndexerInterface::INDEXER_ID); $this->isProductInStock = Bootstrap::getObjectManager()->create( IsProductInStockInterface::class ); @@ -52,51 +86,69 @@ protected function setUp() /** * @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/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php * @magentoDbIsolation disabled */ public function testBackorderedZeroQtyProductIsInStock() { /** @var ProductInterface $product */ - $product = $this->productRepository->get('SKU-1'); + $product = $this->productRepository->get(self::PRODUCT_SKU); $stockItemSearchCriteria = $this->stockItemCriteriaInterfaceFactory->create(); $stockItemSearchCriteria->setProductsFilter($product->getId()); $stockItemsCollection = $this->stockItemRepository->getList($stockItemSearchCriteria); /** @var StockItemInterface $stockItem */ $stockItem = current($stockItemsCollection->getItems()); - $stockItem->setBackorders(1); - $stockItem->setUseConfigBackorders(1); - $stockItem->setQty(-15); - $this->stockItemRepository->save($stockItem); - $this->assertTrue($this->isProductInStock->execute('SKU-1', 1)); + $sourceItem = $this->getSourceItemBySKU(self::PRODUCT_SKU); + $this->changeSourceItemQty($sourceItem, -15); + + $this->assertTrue($this->isProductInStock->execute(self::PRODUCT_SKU, 1)); } /** - * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php * @magentoDbIsolation disabled - * @magentoAppIsolation enabled + * @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/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php */ public function testZeroQtyProductIsOutOfStock() { - $this->productRepository->cleanCache(); - /** @var ProductInterface $product */ - $product = $this->productRepository->get('SKU-1'); - $stockItemSearchCriteria = $this->stockItemCriteriaInterfaceFactory->create(); - $stockItemSearchCriteria->setProductsFilter($product->getId()); - $stockItemsCollection = $this->stockItemRepository->getList($stockItemSearchCriteria); + $sourceItem = $this->getSourceItemBySKU(self::PRODUCT_SKU); + $this->changeSourceItemQty($sourceItem, 0); - /** @var StockItemInterface $stockItem */ - $stockItem = current($stockItemsCollection->getItems()); + $this->assertFalse($this->isProductInStock->execute(self::PRODUCT_SKU, 1)); + } - $stockItem->setBackorders(0); - $stockItem->setUseConfigBackorders(0); - $stockItem->setQty(0); + /** + * @param string $sku + * @return SourceItemInterface + */ + private function getSourceItemBySKU(string $sku) + { + /** @var SearchCriteriaInterface $sourceItemSearchCriteria */ + $sourceItemSearchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $sku)->create(); + $sourceItemSearchResult = $this->sourceItemRepository->getList($sourceItemSearchCriteria); - $this->stockItemRepository->save($stockItem); + /** @var SourceItemInterface $sourceItem */ + return current($sourceItemSearchResult->getItems()); + } - $this->assertFalse($this->isProductInStock->execute('SKU-1', 1)); + /** + * @param SourceItemInterface $sourceItem + * @param $qty + */ + private function changeSourceItemQty(SourceItemInterface $sourceItem, $qty) + { + $sourceItem->setQuantity($qty); + $this->sourceItemsSaveInterface->execute([$sourceItem]); + $this->indexer->reindexRow(5); } } From 0b26fcb6c74cccb26d1251cfe97e173e0ee61e0f Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Thu, 26 Oct 2017 17:10:22 +0200 Subject: [PATCH 6/7] Minor code style fixes --- .../Model/BackorderStockStatusPlugin.php | 2 +- .../Stock/IsBackorderedProductInStockTest.php | 18 +++++++++--------- app/code/Magento/InventorySales/etc/di.xml | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php b/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php index f77a1f5aa259..6fe93844a95e 100644 --- a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php +++ b/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php @@ -66,7 +66,7 @@ public function aroundExecute( /** @var Item $stockItem */ $stockItem = current($stockItemsCollection->getItems()); - if ($stockItem->getData('backorders') > 0) { + if ($stockItem->getBackorders() > 0) { return true; } diff --git a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php b/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php index 64fda415d61a..a7e90b54588d 100644 --- a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php +++ b/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php @@ -3,11 +3,10 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\InventorySales\Test\Integration\Stock; +namespace Magento\InventorySales\Test\Integration\Stock; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\ProductRepositoryInterface; -use Magento\Catalog\Model\ProductRepository; use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; use Magento\CatalogInventory\Api\StockItemRepositoryInterface; @@ -48,9 +47,9 @@ class IsBackorderedProductInStockTest extends TestCase private $indexer; /** - * @var ProductRepository + * @var ProductRepositoryInterface */ - private $productRepository; + private $productRepositoryInterface; /** * @var GetProductQuantityInStockInterface @@ -69,7 +68,7 @@ class IsBackorderedProductInStockTest extends TestCase protected function setUp() { - $this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class); + $this->productRepositoryInterface = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class); $this->stockItemRepository = Bootstrap::getObjectManager()->create(StockItemRepositoryInterface::class); $this->stockItemCriteriaInterfaceFactory = Bootstrap::getObjectManager()->create( StockItemCriteriaInterfaceFactory::class @@ -95,7 +94,7 @@ protected function setUp() public function testBackorderedZeroQtyProductIsInStock() { /** @var ProductInterface $product */ - $product = $this->productRepository->get(self::PRODUCT_SKU); + $product = $this->productRepositoryInterface->get(self::PRODUCT_SKU); $stockItemSearchCriteria = $this->stockItemCriteriaInterfaceFactory->create(); $stockItemSearchCriteria->setProductsFilter($product->getId()); $stockItemsCollection = $this->stockItemRepository->getList($stockItemSearchCriteria); @@ -103,6 +102,7 @@ public function testBackorderedZeroQtyProductIsInStock() /** @var StockItemInterface $stockItem */ $stockItem = current($stockItemsCollection->getItems()); $stockItem->setBackorders(1); + $stockItem->setUseConfigBackorders(0); $this->stockItemRepository->save($stockItem); $sourceItem = $this->getSourceItemBySKU(self::PRODUCT_SKU); @@ -131,7 +131,7 @@ public function testZeroQtyProductIsOutOfStock() * @param string $sku * @return SourceItemInterface */ - private function getSourceItemBySKU(string $sku) + private function getSourceItemBySKU(string $sku): SourceItemInterface { /** @var SearchCriteriaInterface $sourceItemSearchCriteria */ $sourceItemSearchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $sku)->create(); @@ -143,9 +143,9 @@ private function getSourceItemBySKU(string $sku) /** * @param SourceItemInterface $sourceItem - * @param $qty + * @param float $qty */ - private function changeSourceItemQty(SourceItemInterface $sourceItem, $qty) + private function changeSourceItemQty(SourceItemInterface $sourceItem, float $qty) { $sourceItem->setQuantity($qty); $this->sourceItemsSaveInterface->execute([$sourceItem]); diff --git a/app/code/Magento/InventorySales/etc/di.xml b/app/code/Magento/InventorySales/etc/di.xml index 28a2824699b8..99d085a43085 100644 --- a/app/code/Magento/InventorySales/etc/di.xml +++ b/app/code/Magento/InventorySales/etc/di.xml @@ -7,6 +7,6 @@ --> - + From f872393f179b9b6bce003096002b0b6faa7e087b Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Wed, 1 Nov 2017 16:18:20 +0200 Subject: [PATCH 7/7] MSI: Inventory sales backorder functionality -- slight refactoring --- .../BackorderStockStatusPlugin.php | 33 ++++++---- .../IsBackorderedProductInStockTest.php | 60 ++++++++----------- app/code/Magento/InventorySales/etc/di.xml | 2 +- 3 files changed, 46 insertions(+), 49 deletions(-) rename app/code/Magento/InventorySales/Plugin/{Model => InventoryApi}/BackorderStockStatusPlugin.php (65%) rename app/code/Magento/InventorySales/Test/Integration/{Stock => }/IsBackorderedProductInStockTest.php (67%) diff --git a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php b/app/code/Magento/InventorySales/Plugin/InventoryApi/BackorderStockStatusPlugin.php similarity index 65% rename from app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php rename to app/code/Magento/InventorySales/Plugin/InventoryApi/BackorderStockStatusPlugin.php index 6fe93844a95e..110a68270c5d 100644 --- a/app/code/Magento/InventorySales/Plugin/Model/BackorderStockStatusPlugin.php +++ b/app/code/Magento/InventorySales/Plugin/InventoryApi/BackorderStockStatusPlugin.php @@ -5,18 +5,21 @@ */ declare(strict_types=1); -namespace Magento\InventorySales\Plugin\Model; +namespace Magento\InventorySales\Plugin\InventoryApi; -use Magento\CatalogInventory\Model\Stock\Item; -use Magento\InventoryApi\Api\IsProductInStockInterface; -use Magento\CatalogInventory\Model\Stock\StockItemRepository; +use Magento\Catalog\Api\ProductRepositoryInterface; +use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; -use Magento\Catalog\Model\ProductRepository; +use Magento\CatalogInventory\Api\StockItemRepositoryInterface; +use Magento\InventoryApi\Api\IsProductInStockInterface; +/** + * Adapt backorders to IsProductInStockInterface + */ class BackorderStockStatusPlugin { /** - * @var StockItemRepository + * @var StockItemRepositoryInterface */ private $stockItemRepository; @@ -26,14 +29,19 @@ class BackorderStockStatusPlugin private $stockItemCriteriaFactory; /** - * @var ProductRepository + * @var ProductRepositoryInterface */ private $productRepository; + /** + * @param StockItemRepositoryInterface $stockItemRepository + * @param StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory + * @param ProductRepositoryInterface $productRepository + */ public function __construct( - StockItemRepository $stockItemRepository, + StockItemRepositoryInterface $stockItemRepository, StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory, - ProductRepository $productRepository + ProductRepositoryInterface $productRepository ) { $this->stockItemRepository = $stockItemRepository; $this->stockItemCriteriaFactory = $stockItemCriteriaFactory; @@ -63,13 +71,12 @@ public function aroundExecute( $stockItemCriteria->setProductsFilter($productId); $stockItemsCollection = $this->stockItemRepository->getList($stockItemCriteria); - /** @var Item $stockItem */ - $stockItem = current($stockItemsCollection->getItems()); + /** @var StockItemInterface $legacyStockItem */ + $legacyStockItem = current($stockItemsCollection->getItems()); - if ($stockItem->getBackorders() > 0) { + if ($legacyStockItem->getBackorders() > 0) { return true; } - return $proceed($sku, $stockId); } } diff --git a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php b/app/code/Magento/InventorySales/Test/Integration/IsBackorderedProductInStockTest.php similarity index 67% rename from app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php rename to app/code/Magento/InventorySales/Test/Integration/IsBackorderedProductInStockTest.php index a7e90b54588d..18babb3e9f62 100644 --- a/app/code/Magento/InventorySales/Test/Integration/Stock/IsBackorderedProductInStockTest.php +++ b/app/code/Magento/InventorySales/Test/Integration/IsBackorderedProductInStockTest.php @@ -3,19 +3,16 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\InventorySales\Test\Integration\Stock; +namespace Magento\InventorySales\Test\Integration; -use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; use Magento\CatalogInventory\Api\StockItemRepositoryInterface; use Magento\Framework\Api\SearchCriteriaBuilder; -use Magento\Framework\Api\SearchCriteriaInterface; use Magento\Framework\Indexer\IndexerInterface; use Magento\Inventory\Indexer\StockItemIndexerInterface; use Magento\InventoryApi\Api\Data\SourceItemInterface; -use Magento\InventoryApi\Api\GetProductQuantityInStockInterface; use Magento\InventoryApi\Api\IsProductInStockInterface; use Magento\InventoryApi\Api\SourceItemRepositoryInterface; use Magento\InventoryApi\Api\SourceItemsSaveInterface; @@ -24,8 +21,6 @@ class IsBackorderedProductInStockTest extends TestCase { - const PRODUCT_SKU = 'SKU-2'; - /** * @var SourceItemRepositoryInterface */ @@ -39,7 +34,7 @@ class IsBackorderedProductInStockTest extends TestCase /** * @var SourceItemsSaveInterface */ - private $sourceItemsSaveInterface; + private $sourceItemsSave; /** * @var IndexerInterface @@ -49,10 +44,10 @@ class IsBackorderedProductInStockTest extends TestCase /** * @var ProductRepositoryInterface */ - private $productRepositoryInterface; + private $productRepository; /** - * @var GetProductQuantityInStockInterface + * @var IsProductInStockInterface */ private $isProductInStock; @@ -64,23 +59,21 @@ class IsBackorderedProductInStockTest extends TestCase /** * @var StockItemCriteriaInterfaceFactory */ - protected $stockItemCriteriaInterfaceFactory; + private $stockItemCriteriaFactory; protected function setUp() { - $this->productRepositoryInterface = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class); + $this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class); $this->stockItemRepository = Bootstrap::getObjectManager()->create(StockItemRepositoryInterface::class); - $this->stockItemCriteriaInterfaceFactory = Bootstrap::getObjectManager()->create( + $this->stockItemCriteriaFactory = Bootstrap::getObjectManager()->create( StockItemCriteriaInterfaceFactory::class ); $this->sourceItemRepository = Bootstrap::getObjectManager()->create(SourceItemRepositoryInterface::class); $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class); - $this->sourceItemsSaveInterface = Bootstrap::getObjectManager()->create(SourceItemsSaveInterface::class); + $this->sourceItemsSave = Bootstrap::getObjectManager()->create(SourceItemsSaveInterface::class); $this->indexer = Bootstrap::getObjectManager()->create(IndexerInterface::class); $this->indexer->load(StockItemIndexerInterface::INDEXER_ID); - $this->isProductInStock = Bootstrap::getObjectManager()->create( - IsProductInStockInterface::class - ); + $this->isProductInStock = Bootstrap::getObjectManager()->create(IsProductInStockInterface::class); } /** @@ -89,30 +82,27 @@ protected function setUp() * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php - * @magentoDbIsolation disabled */ public function testBackorderedZeroQtyProductIsInStock() { - /** @var ProductInterface $product */ - $product = $this->productRepositoryInterface->get(self::PRODUCT_SKU); - $stockItemSearchCriteria = $this->stockItemCriteriaInterfaceFactory->create(); + $product = $this->productRepository->get('SKU-2'); + $stockItemSearchCriteria = $this->stockItemCriteriaFactory->create(); $stockItemSearchCriteria->setProductsFilter($product->getId()); $stockItemsCollection = $this->stockItemRepository->getList($stockItemSearchCriteria); - /** @var StockItemInterface $stockItem */ - $stockItem = current($stockItemsCollection->getItems()); - $stockItem->setBackorders(1); - $stockItem->setUseConfigBackorders(0); - $this->stockItemRepository->save($stockItem); + /** @var StockItemInterface $legacyStockItem */ + $legacyStockItem = current($stockItemsCollection->getItems()); + $legacyStockItem->setBackorders(1); + $legacyStockItem->setUseConfigBackorders(0); + $this->stockItemRepository->save($legacyStockItem); - $sourceItem = $this->getSourceItemBySKU(self::PRODUCT_SKU); + $sourceItem = $this->getSourceItemBySku('SKU-2'); $this->changeSourceItemQty($sourceItem, -15); - $this->assertTrue($this->isProductInStock->execute(self::PRODUCT_SKU, 1)); + $this->assertTrue($this->isProductInStock->execute('SKU-2', 1)); } /** - * @magentoDbIsolation disabled * @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 @@ -121,23 +111,23 @@ public function testBackorderedZeroQtyProductIsInStock() */ public function testZeroQtyProductIsOutOfStock() { - $sourceItem = $this->getSourceItemBySKU(self::PRODUCT_SKU); + $sourceItem = $this->getSourceItemBySku('SKU-2'); $this->changeSourceItemQty($sourceItem, 0); - $this->assertFalse($this->isProductInStock->execute(self::PRODUCT_SKU, 1)); + $this->assertFalse($this->isProductInStock->execute('SKU-2', 1)); } /** * @param string $sku * @return SourceItemInterface */ - private function getSourceItemBySKU(string $sku): SourceItemInterface + private function getSourceItemBySku(string $sku): SourceItemInterface { - /** @var SearchCriteriaInterface $sourceItemSearchCriteria */ - $sourceItemSearchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $sku)->create(); + $sourceItemSearchCriteria = $this->searchCriteriaBuilder + ->addFilter('sku', $sku) + ->create(); $sourceItemSearchResult = $this->sourceItemRepository->getList($sourceItemSearchCriteria); - /** @var SourceItemInterface $sourceItem */ return current($sourceItemSearchResult->getItems()); } @@ -148,7 +138,7 @@ private function getSourceItemBySKU(string $sku): SourceItemInterface private function changeSourceItemQty(SourceItemInterface $sourceItem, float $qty) { $sourceItem->setQuantity($qty); - $this->sourceItemsSaveInterface->execute([$sourceItem]); + $this->sourceItemsSave->execute([$sourceItem]); $this->indexer->reindexRow(5); } } diff --git a/app/code/Magento/InventorySales/etc/di.xml b/app/code/Magento/InventorySales/etc/di.xml index 99d085a43085..a9b8d9c35c1d 100644 --- a/app/code/Magento/InventorySales/etc/di.xml +++ b/app/code/Magento/InventorySales/etc/di.xml @@ -7,6 +7,6 @@ --> - +