|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\InventoryCatalog\Test\Integration; |
| 8 | + |
| 9 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\InventoryApi\Api\ReservationBuilderInterface; |
| 12 | +use Magento\InventoryApi\Api\ReservationsAppendInterface; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use Magento\CatalogInventory\Api\StockItemRepositoryInterface; |
| 16 | +use Magento\CatalogInventory\Api\StockItemCriteriaInterface; |
| 17 | +use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; |
| 18 | +use Magento\CatalogInventory\Api\Data\StockItemCollectionInterface; |
| 19 | +use Magento\CatalogInventory\Api\Data\StockItemInterface; |
| 20 | +use Magento\InventoryApi\Api\GetProductQuantityInStockInterface; |
| 21 | +use Magento\Indexer\Model\Indexer; |
| 22 | +use Magento\Inventory\Indexer\SourceItem\SourceItemIndexer; |
| 23 | + |
| 24 | +class UpdateLegacyCatalogInventoryPluginTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var ReservationBuilderInterface |
| 28 | + */ |
| 29 | + private $reservationBuilder; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ReservationsAppendInterface |
| 33 | + */ |
| 34 | + private $reservationsAppend; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var StockItemRepositoryInterface |
| 38 | + */ |
| 39 | + private $oldStockItemRepository; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var StockItemCriteriaInterfaceFactory |
| 43 | + */ |
| 44 | + private $stockItemCriteriaFactory; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var GetProductQuantityInStockInterface |
| 48 | + */ |
| 49 | + private $getProductQtyInStock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var IndexerInterface |
| 53 | + */ |
| 54 | + private $indexer; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var ProductRepositoryInterface |
| 58 | + */ |
| 59 | + private $productRepository; |
| 60 | + |
| 61 | + protected function setUp() |
| 62 | + { |
| 63 | + $this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class); |
| 64 | + $this->reservationsAppend = Bootstrap::getObjectManager()->get(ReservationsAppendInterface::class); |
| 65 | + $this->oldStockItemRepository = Bootstrap::getObjectManager()->get(StockItemRepositoryInterface::class); |
| 66 | + $this->stockItemCriteriaFactory = Bootstrap::getObjectManager()->get(StockItemCriteriaInterfaceFactory::class); |
| 67 | + |
| 68 | + $this->indexer = Bootstrap::getObjectManager()->get(Indexer::class); |
| 69 | + $this->indexer->load(SourceItemIndexer::INDEXER_ID); |
| 70 | + $this->getProductQtyInStock = Bootstrap::getObjectManager()->get(GetProductQuantityInStockInterface::class); |
| 71 | + $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php |
| 76 | + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php |
| 77 | + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php |
| 78 | + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php |
| 79 | + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php |
| 80 | + */ |
| 81 | + public function testUpdateStockItemTable() |
| 82 | + { |
| 83 | + $reservationQuantity = -5; |
| 84 | + |
| 85 | + /** @var Product $product */ |
| 86 | + $product = $this->productRepository->get('SKU-1'); |
| 87 | + |
| 88 | + /** @var StockItemCriteriaInterface $criteria */ |
| 89 | + $criteria = $this->stockItemCriteriaFactory->create(); |
| 90 | + $criteria->setProductsFilter([$product->getId()]); |
| 91 | + |
| 92 | + /** @var StockItemCollectionInterface $collectionBeforeChange */ |
| 93 | + $collectionBeforeChange = $this->oldStockItemRepository->getList($criteria); |
| 94 | + /** @var StockItemInterface $oldStockItem */ |
| 95 | + $oldStockItem = current($collectionBeforeChange->getItems()); |
| 96 | + $initialQuantity = $oldStockItem->getQty(); |
| 97 | + |
| 98 | + $this->reservationsAppend->execute([ |
| 99 | + $this->reservationBuilder->setStockId(1)->setSku('SKU-1')->setQuantity($reservationQuantity)->build() |
| 100 | + ]); |
| 101 | + |
| 102 | + /** @var StockItemCollectionInterface $collectionAfterChange */ |
| 103 | + $collectionAfterChange = $this->oldStockItemRepository->getList($criteria); |
| 104 | + $oldStockItem = current($collectionAfterChange->getItems()); |
| 105 | + $quantityAfterCheck = $oldStockItem->getQty(); |
| 106 | + |
| 107 | + $this->assertEquals($initialQuantity + $reservationQuantity, $quantityAfterCheck); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php |
| 112 | + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php |
| 113 | + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php |
| 114 | + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php |
| 115 | + * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php |
| 116 | + */ |
| 117 | + public function testThatReservationPlacedUpdatesBothOldAndNewStocks() |
| 118 | + { |
| 119 | + $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); |
| 120 | + $reservationQuantity = -5; |
| 121 | + |
| 122 | + $this->indexer->reindexAll(); |
| 123 | + $this->assertEquals(8.5, $this->getProductQtyInStock->execute('SKU-1', 10)); |
| 124 | + |
| 125 | + /** @var Product $product */ |
| 126 | + $product = $this->productRepository->get('SKU-1'); |
| 127 | + |
| 128 | + /** @var StockItemCriteriaInterface $criteria */ |
| 129 | + $criteria = $this->stockItemCriteriaFactory->create(); |
| 130 | + $criteria->setProductsFilter([$product->getId()]); |
| 131 | + |
| 132 | + /** @var StockItemCollectionInterface $collectionBeforeChange */ |
| 133 | + $collectionBeforeChange = $this->oldStockItemRepository->getList($criteria); |
| 134 | + |
| 135 | + /** @var StockItemInterface $oldStockItem */ |
| 136 | + $oldStockItem = current($collectionBeforeChange->getItems()); |
| 137 | + $initialQuantity = $oldStockItem->getQty(); |
| 138 | + $this->assertEquals(8.5, $initialQuantity); |
| 139 | + |
| 140 | + $this->reservationsAppend->execute([ |
| 141 | + $this->reservationBuilder->setStockId(10)->setSku('SKU-1')->setQuantity($reservationQuantity)->build() |
| 142 | + ]); |
| 143 | + |
| 144 | + /** @var StockItemCollectionInterface $collectionAfterChange */ |
| 145 | + $collectionAfterChange = $this->oldStockItemRepository->getList($criteria); |
| 146 | + $oldStockItem = current($collectionAfterChange->getItems()); |
| 147 | + $quantityAfterCheck = $oldStockItem->getQty(); |
| 148 | + |
| 149 | + $this->assertEquals(8.5 - 5, $this->getProductQtyInStock->execute('SKU-1', 10)); |
| 150 | + $this->assertEquals($this->getProductQtyInStock->execute('SKU-1', 10), $quantityAfterCheck); |
| 151 | + } |
| 152 | +} |
0 commit comments