StockIndexerTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryConfigurableProductIndexer\Test\Integration;
  8. use Magento\ConfigurableProduct\Api\LinkManagementInterface;
  9. use Magento\InventoryApi\Api\GetSourceItemsBySkuInterface;
  10. use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
  11. use Magento\InventoryIndexer\Indexer\Stock\StockIndexer;
  12. use Magento\InventoryIndexer\Model\ResourceModel\GetStockItemData;
  13. use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
  14. use Magento\Store\Model\StoreManagerInterface;
  15. use Magento\Store\Model\StoreRepository;
  16. use Magento\TestFramework\Helper\Bootstrap;
  17. use PHPUnit\Framework\TestCase;
  18. use Magento\InventoryIndexer\Test\Integration\Indexer\RemoveIndexData;
  19. use Magento\InventoryApi\Api\SourceItemsSaveInterface;
  20. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  21. use Magento\Framework\Api\SearchCriteriaBuilder;
  22. use Magento\Catalog\Api\ProductRepositoryInterface;
  23. /**
  24. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  25. */
  26. class StockIndexerTest extends TestCase
  27. {
  28. /**
  29. * @var StockIndexer
  30. */
  31. private $stockIndexer;
  32. /**
  33. * @var GetStockItemData
  34. */
  35. private $getStockItemData;
  36. /**
  37. * @var RemoveIndexData
  38. */
  39. private $removeIndexData;
  40. /**
  41. * @var LinkManagementInterface
  42. */
  43. private $linkManagement;
  44. /**
  45. * @var GetSourceItemsBySkuInterface
  46. */
  47. private $getSourceItemsBySku;
  48. /**
  49. * @var SourceItemsSaveInterface
  50. */
  51. private $sourceItemSave;
  52. /**
  53. * @var SourceItemRepositoryInterface
  54. */
  55. private $sourceItemRepository;
  56. /**
  57. * @var SearchCriteriaBuilder
  58. */
  59. private $searchCriteriaBuilder;
  60. /**
  61. * @var ProductRepositoryInterface
  62. */
  63. private $productRepository;
  64. /**
  65. * @var StoreManagerInterface
  66. */
  67. private $storeManager;
  68. /**
  69. * @var StoreRepository
  70. */
  71. private $storeRepository;
  72. /**
  73. * @inheritdoc
  74. */
  75. protected function setUp()
  76. {
  77. parent::setUp();
  78. $this->stockIndexer = Bootstrap::getObjectManager()->get(StockIndexer::class);
  79. $this->getStockItemData = Bootstrap::getObjectManager()->get(GetStockItemData::class);
  80. $this->linkManagement = Bootstrap::getObjectManager()->get(LinkManagementInterface::class);
  81. $this->getSourceItemsBySku = Bootstrap::getObjectManager()->get(GetSourceItemsBySkuInterface::class);
  82. $this->sourceItemSave = Bootstrap::getObjectManager()->get(SourceItemsSaveInterface::class);
  83. $this->sourceItemRepository = Bootstrap::getObjectManager()->get(SourceItemRepositoryInterface::class);
  84. $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
  85. $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
  86. $this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
  87. $this->storeRepository = Bootstrap::getObjectManager()->get(StoreRepository::class);
  88. $this->removeIndexData = Bootstrap::getObjectManager()->get(RemoveIndexData::class);
  89. $this->removeIndexData->execute([10, 20, 30]);
  90. }
  91. // @codingStandardsIgnoreStart
  92. /**
  93. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  94. * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
  95. * @magentoDataFixture ../../../../app/code/Magento/InventoryConfigurableProductIndexer/Test/_files/product_configurable_multiple.php
  96. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  97. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  98. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  99. * @magentoDataFixture ../../../../app/code/Magento/InventoryConfigurableProductIndexer/Test/_files/source_items_configurable_multiple.php
  100. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  101. *
  102. * @magentoDbIsolation disabled
  103. */
  104. // @codingStandardsIgnoreEnd
  105. public function testReindexList()
  106. {
  107. $configurableSku = 'configurable_1';
  108. $this->stockIndexer->executeList([10, 20, 30]);
  109. $stockItemData = $this->getStockItemData->execute($configurableSku, 10);
  110. self::assertEquals(1, $stockItemData[GetStockItemDataInterface::IS_SALABLE]);
  111. $stockItemData = $this->getStockItemData->execute($configurableSku, 20);
  112. self::assertEquals(1, $stockItemData[GetStockItemDataInterface::IS_SALABLE]);
  113. $stockItemData = $this->getStockItemData->execute($configurableSku, 30);
  114. self::assertEquals(1, $stockItemData[GetStockItemDataInterface::IS_SALABLE]);
  115. }
  116. // @codingStandardsIgnoreStart
  117. /**
  118. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  119. * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
  120. * @magentoDataFixture ../../../../app/code/Magento/InventoryConfigurableProductIndexer/Test/_files/product_configurable_multiple.php
  121. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  122. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  123. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  124. * @magentoDataFixture ../../../../app/code/Magento/InventoryConfigurableProductIndexer/Test/_files/source_items_configurable_multiple.php
  125. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  126. *
  127. * @magentoDbIsolation disabled
  128. */
  129. // @codingStandardsIgnoreEnd
  130. public function testReindexListSetAllSimplesOutOfStock()
  131. {
  132. $configurableSku = 'configurable_1';
  133. $store = $this->storeRepository->get('store_for_us_website');
  134. $this->storeManager->setCurrentStore($store->getId());
  135. $children = $this->linkManagement->getChildren($configurableSku);
  136. foreach ($children as $child) {
  137. $sku = $child->getSku();
  138. $sourceItems = $this->getSourceItemsBySku->execute($sku);
  139. $changesSourceItems = [];
  140. foreach ($sourceItems as $sourceItem) {
  141. $sourceItem->setStatus(SourceItemInterface::STATUS_OUT_OF_STOCK);
  142. $changesSourceItems[] = $sourceItem;
  143. }
  144. $this->sourceItemSave->execute($changesSourceItems);
  145. }
  146. $this->removeIndexData->execute([10, 20, 30]);
  147. $this->stockIndexer->executeList([10, 20, 30]);
  148. $stockItemData = $this->getStockItemData->execute($configurableSku, 10);
  149. self::assertEquals(0, $stockItemData[GetStockItemDataInterface::IS_SALABLE]);
  150. $stockItemData = $this->getStockItemData->execute($configurableSku, 20);
  151. self::assertEquals(0, $stockItemData[GetStockItemDataInterface::IS_SALABLE]);
  152. $stockItemData = $this->getStockItemData->execute($configurableSku, 30);
  153. self::assertEquals(0, $stockItemData[GetStockItemDataInterface::IS_SALABLE]);
  154. }
  155. // @codingStandardsIgnoreStart
  156. /**
  157. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  158. * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
  159. * @magentoDataFixture ../../../../app/code/Magento/InventoryConfigurableProductIndexer/Test/_files/product_configurable_multiple.php
  160. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  161. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  162. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  163. * @magentoDataFixture ../../../../app/code/Magento/InventoryConfigurableProductIndexer/Test/_files/source_items_configurable_multiple.php
  164. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  165. *
  166. * @magentoDbIsolation disabled
  167. */
  168. // @codingStandardsIgnoreEnd
  169. public function testReindexListSetAllEuSimplesOutOfStock()
  170. {
  171. $configurableSku = 'configurable_1';
  172. $sourceCodes = ['eu-1', 'eu-2', 'eu-3'];
  173. $store = $this->storeRepository->get('store_for_us_website');
  174. $this->storeManager->setCurrentStore($store->getId());
  175. $children = $this->linkManagement->getChildren($configurableSku);
  176. foreach ($children as $child) {
  177. $sku = $child->getSku();
  178. $searchCriteria = $this->searchCriteriaBuilder
  179. ->addFilter(SourceItemInterface::SKU, $sku)
  180. ->addFilter(SourceItemInterface::SOURCE_CODE, $sourceCodes, 'in')
  181. ->create();
  182. $sourceItems = $this->sourceItemRepository->getList($searchCriteria);
  183. $changesSourceItems = [];
  184. foreach ($sourceItems->getItems() as $sourceItem) {
  185. $sourceItem->setStatus(SourceItemInterface::STATUS_OUT_OF_STOCK);
  186. $changesSourceItems[] = $sourceItem;
  187. }
  188. $this->sourceItemSave->execute($changesSourceItems);
  189. }
  190. $this->removeIndexData->execute([10, 20, 30]);
  191. $this->stockIndexer->executeList([10, 20, 30]);
  192. $stockItemData = $this->getStockItemData->execute($configurableSku, 10);
  193. self::assertEquals(0, $stockItemData[GetStockItemDataInterface::IS_SALABLE]);
  194. $stockItemData = $this->getStockItemData->execute($configurableSku, 20);
  195. self::assertEquals(1, $stockItemData[GetStockItemDataInterface::IS_SALABLE]);
  196. $stockItemData = $this->getStockItemData->execute($configurableSku, 30);
  197. self::assertEquals(1, $stockItemData[GetStockItemDataInterface::IS_SALABLE]);
  198. }
  199. }