From dece4748613fb0dd0c29b7f04ff6bf5556addaf3 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Sun, 29 Oct 2017 17:18:17 +0100 Subject: [PATCH 1/9] WIP MSI: #167 - Add arround plugin listen to reservations append function --- .../Model/LegacyCatalogInventoryPlugin.php | 74 +++++++++++++++++++ app/code/Magento/InventoryCatalog/etc/di.xml | 3 + 2 files changed, 77 insertions(+) create mode 100644 app/code/Magento/InventoryCatalog/Plugin/Model/LegacyCatalogInventoryPlugin.php diff --git a/app/code/Magento/InventoryCatalog/Plugin/Model/LegacyCatalogInventoryPlugin.php b/app/code/Magento/InventoryCatalog/Plugin/Model/LegacyCatalogInventoryPlugin.php new file mode 100644 index 000000000000..d6cdc646642a --- /dev/null +++ b/app/code/Magento/InventoryCatalog/Plugin/Model/LegacyCatalogInventoryPlugin.php @@ -0,0 +1,74 @@ +resourceConnection = $resourceConnection; + } + + /** + * Plugin method to fill the legacy tables. + * + * @param ReservationsAppendInterface $subject + * @param callable $callable + * @param ReservationInterface[] $reservations + * @return mixed + * @see ReservationsAppendInterface::execute + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function aroundExecute(ReservationsAppendInterface $subject, callable $callable, array $reservations) + { + $result = $callable($reservations); + $this->updateStockItemTable($reservations); + $this->updateStockStatusTable($reservations); + return $result; + } + + /** + * Update cataloginventory_stock_item qty with reservation information. + * + * @param ReservationInterface[] $reservations + * @return void + */ + private function updateStockItemTable(array $reservations) + { + // @todo impelemention reqiured + } + + /** + * Update cataloginventory_stock_status qty with reservation information. + * + * @param ReservationInterface[] $reservations + * @return void + */ + private function updateStockStatusTable(array $reservations) + { + // @todo impelemention reqiured + } +} diff --git a/app/code/Magento/InventoryCatalog/etc/di.xml b/app/code/Magento/InventoryCatalog/etc/di.xml index 997bbe26b3c4..9175dc65ec29 100644 --- a/app/code/Magento/InventoryCatalog/etc/di.xml +++ b/app/code/Magento/InventoryCatalog/etc/di.xml @@ -11,4 +11,7 @@ + + + From 0192187999a22b50c3c93e3dd33cb3d2e191f9d0 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Sun, 29 Oct 2017 18:11:14 +0100 Subject: [PATCH 2/9] wip --- .../Model/StockIdResolver.php | 23 +++++++++ .../Model/StockIdResolverInterface.php | 25 ++++++++++ .../LegacyCatalogInventoryPluginTest.php | 47 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 app/code/Magento/InventoryCatalog/Model/StockIdResolver.php create mode 100644 app/code/Magento/InventoryCatalog/Model/StockIdResolverInterface.php create mode 100644 app/code/Magento/InventoryCatalog/Test/Integration/LegacyCatalogInventoryPluginTest.php diff --git a/app/code/Magento/InventoryCatalog/Model/StockIdResolver.php b/app/code/Magento/InventoryCatalog/Model/StockIdResolver.php new file mode 100644 index 000000000000..82372f1e1fa3 --- /dev/null +++ b/app/code/Magento/InventoryCatalog/Model/StockIdResolver.php @@ -0,0 +1,23 @@ +reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class); + $this->reservationsAppend = Bootstrap::getObjectManager()->get(ReservationsAppendInterface::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 + */ + public function testUpdateStockItemTable() + { + $this->reservationsAppend->execute([ + $this->reservationBuilder->setStockId(1)->setSku('SKU-1')->setQuantity(-5)->build() + ]); + + + + + } +} From c143657352460abc1306e14747706ed506c4098d Mon Sep 17 00:00:00 2001 From: Enrique Guadalupe Date: Sun, 5 Nov 2017 13:04:15 +0100 Subject: [PATCH 3/9] Improve plugin to update legacy tables --- .../Model/LegacyCatalogInventoryPlugin.php | 74 ---------------- .../UpdateLegacyCatalogInventoryPlugin.php | 88 +++++++++++++++++++ .../LegacyCatalogInventoryPluginTest.php | 47 ---------- ...UpdateLegacyCatalogInventoryPluginTest.php | 79 +++++++++++++++++ app/code/Magento/InventoryCatalog/etc/di.xml | 4 +- 5 files changed, 169 insertions(+), 123 deletions(-) delete mode 100644 app/code/Magento/InventoryCatalog/Plugin/Model/LegacyCatalogInventoryPlugin.php create mode 100644 app/code/Magento/InventoryCatalog/Plugin/Model/UpdateLegacyCatalogInventoryPlugin.php delete mode 100644 app/code/Magento/InventoryCatalog/Test/Integration/LegacyCatalogInventoryPluginTest.php create mode 100644 app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php diff --git a/app/code/Magento/InventoryCatalog/Plugin/Model/LegacyCatalogInventoryPlugin.php b/app/code/Magento/InventoryCatalog/Plugin/Model/LegacyCatalogInventoryPlugin.php deleted file mode 100644 index d6cdc646642a..000000000000 --- a/app/code/Magento/InventoryCatalog/Plugin/Model/LegacyCatalogInventoryPlugin.php +++ /dev/null @@ -1,74 +0,0 @@ -resourceConnection = $resourceConnection; - } - - /** - * Plugin method to fill the legacy tables. - * - * @param ReservationsAppendInterface $subject - * @param callable $callable - * @param ReservationInterface[] $reservations - * @return mixed - * @see ReservationsAppendInterface::execute - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function aroundExecute(ReservationsAppendInterface $subject, callable $callable, array $reservations) - { - $result = $callable($reservations); - $this->updateStockItemTable($reservations); - $this->updateStockStatusTable($reservations); - return $result; - } - - /** - * Update cataloginventory_stock_item qty with reservation information. - * - * @param ReservationInterface[] $reservations - * @return void - */ - private function updateStockItemTable(array $reservations) - { - // @todo impelemention reqiured - } - - /** - * Update cataloginventory_stock_status qty with reservation information. - * - * @param ReservationInterface[] $reservations - * @return void - */ - private function updateStockStatusTable(array $reservations) - { - // @todo impelemention reqiured - } -} diff --git a/app/code/Magento/InventoryCatalog/Plugin/Model/UpdateLegacyCatalogInventoryPlugin.php b/app/code/Magento/InventoryCatalog/Plugin/Model/UpdateLegacyCatalogInventoryPlugin.php new file mode 100644 index 000000000000..69cf88c80578 --- /dev/null +++ b/app/code/Magento/InventoryCatalog/Plugin/Model/UpdateLegacyCatalogInventoryPlugin.php @@ -0,0 +1,88 @@ +resourceConnection = $resourceConnection; + $this->productRepository = $productRepository; + $this->stockRegistry = $stockRegistry; + } + + /** + * Plugin method to fill the legacy tables. + * + * @param ReservationsAppendInterface $subject + * @param void $result + * @param ReservationInterface[] $reservations + * + * @see ReservationsAppendInterface::execute + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @return void + */ + public function afterExecute(ReservationsAppendInterface $subject, $result, array $reservations) + { + $this->updateStockItemAndStatusTable($reservations); + return $result; + } + + /** + * Updates cataloginventory_stock_item and cataloginventory_stock_status qty with reservation information. + * + * @param ReservationInterface[] $reservations + * @return void + */ + private function updateStockItemAndStatusTable(array $reservations) + { + foreach ($reservations as $reservation) { + $sku = $reservation->getSku(); + $stockItem = $this->stockRegistry->getStockItemBySku($sku); + $stockItem->setQty($stockItem->getQty() + $reservation->getQuantity()); + $this->stockRegistry->updateStockItemBySku($sku, $stockItem); + + $stockStatus = $this->stockRegistry->getStockStatus($stockItem->getProductId()); + $stockStatus->setQty($stockStatus->getQty() + $reservation->getQuantity()); + $stockStatus->save(); + } + } +} diff --git a/app/code/Magento/InventoryCatalog/Test/Integration/LegacyCatalogInventoryPluginTest.php b/app/code/Magento/InventoryCatalog/Test/Integration/LegacyCatalogInventoryPluginTest.php deleted file mode 100644 index 0f3b9390b510..000000000000 --- a/app/code/Magento/InventoryCatalog/Test/Integration/LegacyCatalogInventoryPluginTest.php +++ /dev/null @@ -1,47 +0,0 @@ -reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class); - $this->reservationsAppend = Bootstrap::getObjectManager()->get(ReservationsAppendInterface::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 - */ - public function testUpdateStockItemTable() - { - $this->reservationsAppend->execute([ - $this->reservationBuilder->setStockId(1)->setSku('SKU-1')->setQuantity(-5)->build() - ]); - - - - - } -} diff --git a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php new file mode 100644 index 000000000000..36250697a937 --- /dev/null +++ b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php @@ -0,0 +1,79 @@ +reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class); + $this->reservationsAppend = Bootstrap::getObjectManager()->get(ReservationsAppendInterface::class); + $this->oldStockItemRepository = Bootstrap::getObjectManager()->get(StockItemRepositoryInterface::class); + $this->stockItemCriteriaFactory = Bootstrap::getObjectManager()->get(StockItemCriteriaInterfaceFactory::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/source_items.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php + * @magentoDataFixture ../../../../dev/tests/integration/testsuite/Magento/Catalog/_files/products.php + */ + public function testUpdateStockItemTable() + { + + /** @var StockItemCriteriaInterface $criteria */ + $criteria = $this->stockItemCriteriaFactory->create(); + $criteria->setProductsFilter([1]); + + /** @var StockItemCollectionInterface $collectionBeforeChange */ + $collectionBeforeChange = $this->oldStockItemRepository->getList($criteria); + /** @var StockItemInterface $oldStockItem */ + $oldStockItem = current($collectionBeforeChange->getItems()); + $initialQuantity = $oldStockItem->getQty(); + + $this->reservationsAppend->execute([ + $this->reservationBuilder->setStockId(1)->setSku('simple')->setQuantity(-5)->build() + ]); + + /** @var StockItemCollectionInterface $collectionAfterChange */ + $collectionAfterChange = $this->oldStockItemRepository->getList($criteria); + $oldStockItem = current($collectionAfterChange->getItems()); + $quantityAfterCheck = $oldStockItem->getQty(); + + $this->assertEquals(5, $initialQuantity - $quantityAfterCheck); + } +} diff --git a/app/code/Magento/InventoryCatalog/etc/di.xml b/app/code/Magento/InventoryCatalog/etc/di.xml index 9175dc65ec29..032143c5f2a6 100644 --- a/app/code/Magento/InventoryCatalog/etc/di.xml +++ b/app/code/Magento/InventoryCatalog/etc/di.xml @@ -9,9 +9,9 @@ - + - + From 2d7f35d3a6d2294c3b9cddac5533c29e1b66b1ab Mon Sep 17 00:00:00 2001 From: Enrique Guadalupe Date: Sun, 5 Nov 2017 13:09:56 +0100 Subject: [PATCH 4/9] Deleted Resolver id files unnecesary --- .../Model/StockIdResolver.php | 23 ----------------- .../Model/StockIdResolverInterface.php | 25 ------------------- 2 files changed, 48 deletions(-) delete mode 100644 app/code/Magento/InventoryCatalog/Model/StockIdResolver.php delete mode 100644 app/code/Magento/InventoryCatalog/Model/StockIdResolverInterface.php diff --git a/app/code/Magento/InventoryCatalog/Model/StockIdResolver.php b/app/code/Magento/InventoryCatalog/Model/StockIdResolver.php deleted file mode 100644 index 82372f1e1fa3..000000000000 --- a/app/code/Magento/InventoryCatalog/Model/StockIdResolver.php +++ /dev/null @@ -1,23 +0,0 @@ - Date: Sun, 5 Nov 2017 15:19:20 +0100 Subject: [PATCH 5/9] add integration test to check in the new sources the stock --- .../Test/_files/products_simple.php | 26 ++++++++ .../Test/_files/products_simple_rollback.php | 16 +++++ ...UpdateLegacyCatalogInventoryPluginTest.php | 65 ++++++++++++++++++- 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 app/code/Magento/InventoryApi/Test/_files/products_simple.php create mode 100644 app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php diff --git a/app/code/Magento/InventoryApi/Test/_files/products_simple.php b/app/code/Magento/InventoryApi/Test/_files/products_simple.php new file mode 100644 index 000000000000..f17b98291a97 --- /dev/null +++ b/app/code/Magento/InventoryApi/Test/_files/products_simple.php @@ -0,0 +1,26 @@ +create(\Magento\Catalog\Model\Product::class); +$product + ->setTypeId('simple') + ->setId(3) + ->setAttributeSetId(4) + ->setWebsiteIds([1]) + ->setName('Simple Product') + ->setSku('SKU-1') + ->setPrice(10) + ->setMetaTitle('meta title') + ->setMetaKeyword('meta keyword') + ->setMetaDescription('meta description') + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 8.5, 'is_in_stock' => 1]) + ->setQty(8.5) + ->save(); + diff --git a/app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php b/app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php new file mode 100644 index 000000000000..fc64538f0655 --- /dev/null +++ b/app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php @@ -0,0 +1,16 @@ +get(\Magento\Catalog\Api\ProductRepositoryInterface::class); +try { + $product = $productRepository->get('SKU-1', false, null, true); + $productRepository->delete($product); + +} catch (NoSuchEntityException $e) { +} \ No newline at end of file diff --git a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php index 36250697a937..e15c3ef6ee22 100644 --- a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php +++ b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php @@ -15,6 +15,9 @@ use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; use Magento\CatalogInventory\Api\Data\StockItemCollectionInterface; use Magento\CatalogInventory\Api\Data\StockItemInterface; +use Magento\InventoryApi\Api\GetProductQuantityInStockInterface; +use Magento\Indexer\Model\Indexer; +use Magento\Inventory\Indexer\StockItemIndexerInterface; class UpdateLegacyCatalogInventoryPluginTest extends TestCase { @@ -27,21 +30,38 @@ class UpdateLegacyCatalogInventoryPluginTest extends TestCase * @var ReservationsAppendInterface */ private $reservationsAppend; + /** * @var StockItemRepositoryInterface */ private $oldStockItemRepository; + /** * @var StockItemCriteriaInterfaceFactory */ private $stockItemCriteriaFactory; + /** + * @var GetProductQuantityInStockInterface + */ + private $getProductQtyInStock; + + /** + * @var IndexerInterface + */ + private $indexer; + + protected function setUp() { $this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class); $this->reservationsAppend = Bootstrap::getObjectManager()->get(ReservationsAppendInterface::class); $this->oldStockItemRepository = Bootstrap::getObjectManager()->get(StockItemRepositoryInterface::class); $this->stockItemCriteriaFactory = Bootstrap::getObjectManager()->get(StockItemCriteriaInterfaceFactory::class); + + $this->indexer = Bootstrap::getObjectManager()->get(Indexer::class); + $this->indexer->load(StockItemIndexerInterface::INDEXER_ID); + $this->getProductQtyInStock = Bootstrap::getObjectManager()->get(GetProductQuantityInStockInterface::class); } /** @@ -54,6 +74,7 @@ protected function setUp() */ public function testUpdateStockItemTable() { + $reservationQuantity = -5; /** @var StockItemCriteriaInterface $criteria */ $criteria = $this->stockItemCriteriaFactory->create(); @@ -66,7 +87,46 @@ public function testUpdateStockItemTable() $initialQuantity = $oldStockItem->getQty(); $this->reservationsAppend->execute([ - $this->reservationBuilder->setStockId(1)->setSku('simple')->setQuantity(-5)->build() + $this->reservationBuilder->setStockId(1)->setSku('simple')->setQuantity($reservationQuantity)->build() + ]); + + /** @var StockItemCollectionInterface $collectionAfterChange */ + $collectionAfterChange = $this->oldStockItemRepository->getList($criteria); + $oldStockItem = current($collectionAfterChange->getItems()); + $quantityAfterCheck = $oldStockItem->getQty(); + + $this->assertEquals($initialQuantity + $reservationQuantity, $quantityAfterCheck); + } + + /** + * @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 + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products_simple.php + */ + public function testThatReservationPlacedUpdatesBothOldAndNewStocks() + { + $this->markTestSkipped('finish later'); + $reservationQuantity = -5; + + $this->indexer->reindexAll(); + $this->assertEquals(8.5, $this->getProductQtyInStock->execute('SKU-1', 10)); + + /** @var StockItemCriteriaInterface $criteria */ + $criteria = $this->stockItemCriteriaFactory->create(); + $criteria->setProductsFilter([3]); + + /** @var StockItemCollectionInterface $collectionBeforeChange */ + $collectionBeforeChange = $this->oldStockItemRepository->getList($criteria); + /** @var StockItemInterface $oldStockItem */ + $oldStockItem = current($collectionBeforeChange->getItems()); + $initialQuantity = $oldStockItem->getQty(); + $this->assertEquals(8.5, $initialQuantity); + + $this->reservationsAppend->execute([ + $this->reservationBuilder->setStockId(1)->setSku('SKU-1')->setQuantity($reservationQuantity)->build() ]); /** @var StockItemCollectionInterface $collectionAfterChange */ @@ -74,6 +134,7 @@ public function testUpdateStockItemTable() $oldStockItem = current($collectionAfterChange->getItems()); $quantityAfterCheck = $oldStockItem->getQty(); - $this->assertEquals(5, $initialQuantity - $quantityAfterCheck); + + $this->assertEquals($this->getProductQtyInStock->execute('SKU-1', 10), $quantityAfterCheck); } } From 8e2e1bf95f9bfab4f17d167dae311c9ae7081f64 Mon Sep 17 00:00:00 2001 From: larsroettig Date: Thu, 9 Nov 2017 21:02:27 +0100 Subject: [PATCH 6/9] fix static tests --- app/code/Magento/InventoryApi/Test/_files/products_simple.php | 1 - .../InventoryApi/Test/_files/products_simple_rollback.php | 4 ++-- .../Integration/UpdateLegacyCatalogInventoryPluginTest.php | 2 -- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/InventoryApi/Test/_files/products_simple.php b/app/code/Magento/InventoryApi/Test/_files/products_simple.php index f17b98291a97..a5a4455ee280 100644 --- a/app/code/Magento/InventoryApi/Test/_files/products_simple.php +++ b/app/code/Magento/InventoryApi/Test/_files/products_simple.php @@ -23,4 +23,3 @@ ->setStockData(['use_config_manage_stock' => 1, 'qty' => 8.5, 'is_in_stock' => 1]) ->setQty(8.5) ->save(); - diff --git a/app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php b/app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php index fc64538f0655..8a618bb117b9 100644 --- a/app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php +++ b/app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + use Magento\Framework\Exception\NoSuchEntityException; /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ @@ -11,6 +12,5 @@ try { $product = $productRepository->get('SKU-1', false, null, true); $productRepository->delete($product); - } catch (NoSuchEntityException $e) { -} \ No newline at end of file +} diff --git a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php index e15c3ef6ee22..f5351838cee2 100644 --- a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php +++ b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php @@ -51,7 +51,6 @@ class UpdateLegacyCatalogInventoryPluginTest extends TestCase */ private $indexer; - protected function setUp() { $this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class); @@ -134,7 +133,6 @@ public function testThatReservationPlacedUpdatesBothOldAndNewStocks() $oldStockItem = current($collectionAfterChange->getItems()); $quantityAfterCheck = $oldStockItem->getQty(); - $this->assertEquals($this->getProductQtyInStock->execute('SKU-1', 10), $quantityAfterCheck); } } From f2ca93ab3858e13ab9d7bb092157fefc79ac085e Mon Sep 17 00:00:00 2001 From: Sinisa Nedeljkovic Date: Sun, 19 Nov 2017 12:33:07 +0100 Subject: [PATCH 7/9] Fixed integration test for checking old/new stock changes, improved data fixtures --- .../InventoryApi/Test/_files/products.php | 20 +++++++++++++ .../Test/_files/products_rollback.php | 15 ++++++++++ ...UpdateLegacyCatalogInventoryPluginTest.php | 28 ++++++++++++++----- 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/InventoryApi/Test/_files/products.php b/app/code/Magento/InventoryApi/Test/_files/products.php index 2a37fd7e69fc..08ce6888fd95 100644 --- a/app/code/Magento/InventoryApi/Test/_files/products.php +++ b/app/code/Magento/InventoryApi/Test/_files/products.php @@ -14,6 +14,25 @@ $productFactory = $objectManager->get(ProductInterfaceFactory::class); /** @var ProductRepositoryInterface $productRepository */ $productRepository = $objectManager->get(ProductRepositoryInterface::class); +$productRepository->cleanCache(); + +$stockData = [ + 'SKU-1' => [ + 'qty' => 8.5, + 'is_in_stock' => true, + 'manage_stock' => true + ], + 'SKU-2' => [ + 'qty' => 5, + 'is_in_stock' => true, + 'manage_stock' => true + ], + 'SKU-3' => [ + 'qty' => 0, + 'is_in_stock' => false, + 'manage_stock' => true + ] +]; for ($i = 1; $i <= 3; $i++) { $product = $productFactory->create(); @@ -22,6 +41,7 @@ ->setName('Simple Product ' . $i) ->setSku('SKU-' . $i) ->setPrice(10) + ->setStockData($stockData['SKU-' . $i]) ->setStatus(Status::STATUS_ENABLED); $productRepository->save($product); } diff --git a/app/code/Magento/InventoryApi/Test/_files/products_rollback.php b/app/code/Magento/InventoryApi/Test/_files/products_rollback.php index 021e40448a00..06de7fe3b82c 100644 --- a/app/code/Magento/InventoryApi/Test/_files/products_rollback.php +++ b/app/code/Magento/InventoryApi/Test/_files/products_rollback.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ use Magento\Catalog\Api\ProductRepositoryInterface; +use Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory; +use Magento\CatalogInventory\Api\StockStatusRepositoryInterface; use Magento\Framework\Registry; use Magento\TestFramework\Helper\Bootstrap; @@ -12,12 +14,25 @@ $productRepository = $objectManager->create(ProductRepositoryInterface::class); /** @var Registry $registry */ $registry = $objectManager->get(Registry::class); +/** @var StockStatusRepositoryInterface $stockStatusRepository */ +$stockStatusRepository = $objectManager->create(StockStatusRepositoryInterface::class); +/** @var StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory */ +$stockStatusCriteriaFactory = $objectManager->create(StockStatusCriteriaInterfaceFactory::class); $currentArea = $registry->registry('isSecureArea'); $registry->unregister('isSecureArea'); $registry->register('isSecureArea', true); for ($i = 1; $i <= 3; $i++) { + $product = $productRepository->get('SKU-' . $i); + /** @var \Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory **/ + $criteria = $stockStatusCriteriaFactory->create(); + $criteria->setProductsFilter($product->getId()); + + $result = $stockStatusRepository->getList($criteria); + $stockStatus = current($result->getItems()); + $stockStatusRepository->delete($stockStatus); + $productRepository->deleteById('SKU-' . $i); } diff --git a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php index f5351838cee2..eb7619d1bbbc 100644 --- a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php +++ b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php @@ -6,6 +6,8 @@ namespace Magento\InventoryCatalog\Test\Integration; +use Magento\Catalog\Api\ProductRepositoryInterface; +use Magento\Catalog\Model\Product; use Magento\InventoryApi\Api\ReservationBuilderInterface; use Magento\InventoryApi\Api\ReservationsAppendInterface; use PHPUnit\Framework\TestCase; @@ -51,6 +53,11 @@ class UpdateLegacyCatalogInventoryPluginTest extends TestCase */ private $indexer; + /** + * @var ProductRepositoryInterface + */ + private $productRepository; + protected function setUp() { $this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class); @@ -61,6 +68,7 @@ protected function setUp() $this->indexer = Bootstrap::getObjectManager()->get(Indexer::class); $this->indexer->load(StockItemIndexerInterface::INDEXER_ID); $this->getProductQtyInStock = Bootstrap::getObjectManager()->get(GetProductQuantityInStockInterface::class); + $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); } /** @@ -69,15 +77,17 @@ 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 - * @magentoDataFixture ../../../../dev/tests/integration/testsuite/Magento/Catalog/_files/products.php */ public function testUpdateStockItemTable() { $reservationQuantity = -5; + /** @var Product $product */ + $product = $this->productRepository->get('SKU-1'); + /** @var StockItemCriteriaInterface $criteria */ $criteria = $this->stockItemCriteriaFactory->create(); - $criteria->setProductsFilter([1]); + $criteria->setProductsFilter([$product->getId()]); /** @var StockItemCollectionInterface $collectionBeforeChange */ $collectionBeforeChange = $this->oldStockItemRepository->getList($criteria); @@ -86,7 +96,7 @@ public function testUpdateStockItemTable() $initialQuantity = $oldStockItem->getQty(); $this->reservationsAppend->execute([ - $this->reservationBuilder->setStockId(1)->setSku('simple')->setQuantity($reservationQuantity)->build() + $this->reservationBuilder->setStockId(1)->setSku('SKU-1')->setQuantity($reservationQuantity)->build() ]); /** @var StockItemCollectionInterface $collectionAfterChange */ @@ -103,29 +113,32 @@ public function testUpdateStockItemTable() * @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 - * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products_simple.php */ public function testThatReservationPlacedUpdatesBothOldAndNewStocks() { - $this->markTestSkipped('finish later'); + $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); $reservationQuantity = -5; $this->indexer->reindexAll(); $this->assertEquals(8.5, $this->getProductQtyInStock->execute('SKU-1', 10)); + /** @var Product $product */ + $product = $this->productRepository->get('SKU-1'); + /** @var StockItemCriteriaInterface $criteria */ $criteria = $this->stockItemCriteriaFactory->create(); - $criteria->setProductsFilter([3]); + $criteria->setProductsFilter([$product->getId()]); /** @var StockItemCollectionInterface $collectionBeforeChange */ $collectionBeforeChange = $this->oldStockItemRepository->getList($criteria); + /** @var StockItemInterface $oldStockItem */ $oldStockItem = current($collectionBeforeChange->getItems()); $initialQuantity = $oldStockItem->getQty(); $this->assertEquals(8.5, $initialQuantity); $this->reservationsAppend->execute([ - $this->reservationBuilder->setStockId(1)->setSku('SKU-1')->setQuantity($reservationQuantity)->build() + $this->reservationBuilder->setStockId(10)->setSku('SKU-1')->setQuantity($reservationQuantity)->build() ]); /** @var StockItemCollectionInterface $collectionAfterChange */ @@ -134,5 +147,6 @@ public function testThatReservationPlacedUpdatesBothOldAndNewStocks() $quantityAfterCheck = $oldStockItem->getQty(); $this->assertEquals($this->getProductQtyInStock->execute('SKU-1', 10), $quantityAfterCheck); + } } From 4c0ca749a5e2a88d903e476807bb17a7e062dcd6 Mon Sep 17 00:00:00 2001 From: Sinisa Nedeljkovic Date: Mon, 20 Nov 2017 10:26:29 +0100 Subject: [PATCH 8/9] Fixed code sniffer failure because of empty line at the end of the file --- .../Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php index eb7619d1bbbc..e77f8fdf690e 100644 --- a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php +++ b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php @@ -147,6 +147,5 @@ public function testThatReservationPlacedUpdatesBothOldAndNewStocks() $quantityAfterCheck = $oldStockItem->getQty(); $this->assertEquals($this->getProductQtyInStock->execute('SKU-1', 10), $quantityAfterCheck); - } } From 2e6976cdeb96741def2e0ee825b5a83b10b48983 Mon Sep 17 00:00:00 2001 From: Igor Miniailo Date: Wed, 22 Nov 2017 12:59:48 +0200 Subject: [PATCH 9/9] Fixed broken builds for GitHub issue #167 --- .../Test/_files/products_simple.php | 25 ------------------- .../Test/_files/products_simple_rollback.php | 16 ------------ .../UpdateLegacyCatalogInventoryPlugin.php | 2 +- ...UpdateLegacyCatalogInventoryPluginTest.php | 5 ++-- 4 files changed, 4 insertions(+), 44 deletions(-) delete mode 100644 app/code/Magento/InventoryApi/Test/_files/products_simple.php delete mode 100644 app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php diff --git a/app/code/Magento/InventoryApi/Test/_files/products_simple.php b/app/code/Magento/InventoryApi/Test/_files/products_simple.php deleted file mode 100644 index a5a4455ee280..000000000000 --- a/app/code/Magento/InventoryApi/Test/_files/products_simple.php +++ /dev/null @@ -1,25 +0,0 @@ -create(\Magento\Catalog\Model\Product::class); -$product - ->setTypeId('simple') - ->setId(3) - ->setAttributeSetId(4) - ->setWebsiteIds([1]) - ->setName('Simple Product') - ->setSku('SKU-1') - ->setPrice(10) - ->setMetaTitle('meta title') - ->setMetaKeyword('meta keyword') - ->setMetaDescription('meta description') - ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) - ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) - ->setStockData(['use_config_manage_stock' => 1, 'qty' => 8.5, 'is_in_stock' => 1]) - ->setQty(8.5) - ->save(); diff --git a/app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php b/app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php deleted file mode 100644 index 8a618bb117b9..000000000000 --- a/app/code/Magento/InventoryApi/Test/_files/products_simple_rollback.php +++ /dev/null @@ -1,16 +0,0 @@ -get(\Magento\Catalog\Api\ProductRepositoryInterface::class); -try { - $product = $productRepository->get('SKU-1', false, null, true); - $productRepository->delete($product); -} catch (NoSuchEntityException $e) { -} diff --git a/app/code/Magento/InventoryCatalog/Plugin/Model/UpdateLegacyCatalogInventoryPlugin.php b/app/code/Magento/InventoryCatalog/Plugin/Model/UpdateLegacyCatalogInventoryPlugin.php index 69cf88c80578..ba6de0ccac19 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/Model/UpdateLegacyCatalogInventoryPlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/Model/UpdateLegacyCatalogInventoryPlugin.php @@ -32,7 +32,7 @@ class UpdateLegacyCatalogInventoryPlugin /** * @var \Magento\CatalogInventory\Api\StockRegistryInterface */ - protected $stockRegistry; + private $stockRegistry; /** * @param ResourceConnection $resourceConnection diff --git a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php index e77f8fdf690e..d1be1262f809 100644 --- a/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php +++ b/app/code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php @@ -19,7 +19,7 @@ use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\InventoryApi\Api\GetProductQuantityInStockInterface; use Magento\Indexer\Model\Indexer; -use Magento\Inventory\Indexer\StockItemIndexerInterface; +use Magento\Inventory\Indexer\SourceItem\SourceItemIndexer; class UpdateLegacyCatalogInventoryPluginTest extends TestCase { @@ -66,7 +66,7 @@ protected function setUp() $this->stockItemCriteriaFactory = Bootstrap::getObjectManager()->get(StockItemCriteriaInterfaceFactory::class); $this->indexer = Bootstrap::getObjectManager()->get(Indexer::class); - $this->indexer->load(StockItemIndexerInterface::INDEXER_ID); + $this->indexer->load(SourceItemIndexer::INDEXER_ID); $this->getProductQtyInStock = Bootstrap::getObjectManager()->get(GetProductQuantityInStockInterface::class); $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); } @@ -146,6 +146,7 @@ public function testThatReservationPlacedUpdatesBothOldAndNewStocks() $oldStockItem = current($collectionAfterChange->getItems()); $quantityAfterCheck = $oldStockItem->getQty(); + $this->assertEquals(8.5 - 5, $this->getProductQtyInStock->execute('SKU-1', 10)); $this->assertEquals($this->getProductQtyInStock->execute('SKU-1', 10), $quantityAfterCheck); } }