SourceItemIndexerTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\InventoryIndexer\Test\Integration\Indexer;
  8. use Magento\Framework\Api\SearchCriteriaBuilder;
  9. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  10. use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
  11. use Magento\InventoryIndexer\Indexer\SourceItem\GetSourceItemIds;
  12. use Magento\InventoryIndexer\Indexer\SourceItem\SourceItemIndexer;
  13. use Magento\InventoryIndexer\Model\ResourceModel\GetStockItemData;
  14. use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
  15. use Magento\TestFramework\Helper\Bootstrap;
  16. use PHPUnit\Framework\TestCase;
  17. class SourceItemIndexerTest extends TestCase
  18. {
  19. /**
  20. * @var SourceItemIndexer
  21. */
  22. private $sourceItemIndexer;
  23. /**
  24. * @var GetStockItemData
  25. */
  26. private $getStockItemData;
  27. /**
  28. * @var GetSourceItemIds
  29. */
  30. private $getSourceItemIds;
  31. /**
  32. * @var RemoveIndexData
  33. */
  34. private $removeIndexData;
  35. /**
  36. * @var SourceItemRepositoryInterface
  37. */
  38. private $sourceItemRepository;
  39. /**
  40. * @var SearchCriteriaBuilder
  41. */
  42. private $searchCriteriaBuilder;
  43. protected function setUp()
  44. {
  45. $this->sourceItemIndexer = Bootstrap::getObjectManager()->get(SourceItemIndexer::class);
  46. $this->getStockItemData = Bootstrap::getObjectManager()->get(GetStockItemData::class);
  47. $this->getSourceItemIds = Bootstrap::getObjectManager()->get(GetSourceItemIds::class);
  48. $this->sourceItemRepository = Bootstrap::getObjectManager()->get(SourceItemRepositoryInterface::class);
  49. $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
  50. $this->removeIndexData = Bootstrap::getObjectManager()->get(RemoveIndexData::class);
  51. $this->removeIndexData->execute([10, 20, 30]);
  52. }
  53. /**
  54. * We broke transaction during indexation so we need to clean db state manually
  55. */
  56. protected function tearDown()
  57. {
  58. $this->removeIndexData->execute([10, 20, 30]);
  59. }
  60. /**
  61. * Source 'eu-1' is assigned on EU-stock(id:10) and Global-stock(id:30)
  62. * Thus these stocks stocks be reindexed only for SKU-1
  63. *
  64. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  65. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  66. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  67. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  68. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  69. *
  70. * @param string $sku
  71. * @param int $stockId
  72. * @param array|null $expectedData
  73. *
  74. * @dataProvider reindexRowDataProvider
  75. *
  76. * @magentoDbIsolation disabled
  77. */
  78. public function testReindexRow(string $sku, int $stockId, $expectedData)
  79. {
  80. $sourceItem = $this->getSourceItem('SKU-1', 'eu-1');
  81. $sourceItemIds = $this->getSourceItemIds->execute([$sourceItem]);
  82. foreach ($sourceItemIds as $sourceItemId) {
  83. $this->sourceItemIndexer->executeRow((int)$sourceItemId);
  84. }
  85. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  86. self::assertEquals($expectedData, $stockItemData);
  87. }
  88. /**
  89. * @return array
  90. */
  91. public function reindexRowDataProvider(): array
  92. {
  93. return [
  94. ['SKU-1', 10, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  95. ['SKU-1', 30, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  96. ['SKU-2', 10, null],
  97. ['SKU-2', 30, null],
  98. ['SKU-3', 10, null],
  99. ['SKU-3', 30, null],
  100. ];
  101. }
  102. /**
  103. * Source 'eu-1' and 'us-1' are assigned on EU-stock(id:10), US-stock(id:20) and Global-stock(id:30)
  104. * Thus these stocks should be reindexed only for SKU-1 and for SKU-2
  105. *
  106. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  107. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  108. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  109. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  110. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  111. *
  112. * @param string $sku
  113. * @param int $stockId
  114. * @param array|null $expectedData
  115. *
  116. * @dataProvider reindexListDataProvider
  117. *
  118. * @magentoDbIsolation disabled
  119. */
  120. public function testReindexList(string $sku, int $stockId, $expectedData)
  121. {
  122. $sourceItemIds = $this->getSourceItemIds->execute(
  123. [
  124. $this->getSourceItem('SKU-1', 'eu-1'),
  125. $this->getSourceItem('SKU-2', 'us-1'),
  126. ]
  127. );
  128. $this->sourceItemIndexer->executeList($sourceItemIds);
  129. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  130. self::assertEquals($expectedData, $stockItemData);
  131. }
  132. /**
  133. * @return array
  134. */
  135. public function reindexListDataProvider(): array
  136. {
  137. return [
  138. ['SKU-1', 10, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  139. ['SKU-1', 20, null],
  140. ['SKU-1', 30, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  141. ['SKU-2', 10, null],
  142. ['SKU-2', 20, [GetStockItemDataInterface::QUANTITY => 5, GetStockItemDataInterface::IS_SALABLE => 1]],
  143. ['SKU-2', 30, [GetStockItemDataInterface::QUANTITY => 5, GetStockItemDataInterface::IS_SALABLE => 1]],
  144. ['SKU-3', 10, null],
  145. ['SKU-3', 20, null],
  146. ['SKU-3', 30, null],
  147. ];
  148. }
  149. /**
  150. * All of stocks should be reindexed for all of skus
  151. *
  152. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  153. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  154. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  155. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  156. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  157. *
  158. * @param string $sku
  159. * @param int $stockId
  160. * @param array|null $expectedData
  161. *
  162. * @dataProvider reindexAllDataProvider
  163. *
  164. * @magentoDbIsolation disabled
  165. */
  166. public function testReindexAll(string $sku, int $stockId, $expectedData)
  167. {
  168. $this->sourceItemIndexer->executeFull();
  169. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  170. self::assertEquals($expectedData, $stockItemData);
  171. }
  172. /**
  173. * @return array
  174. */
  175. public function reindexAllDataProvider(): array
  176. {
  177. return [
  178. ['SKU-1', 10, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  179. ['SKU-1', 20, null],
  180. ['SKU-1', 30, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  181. ['SKU-2', 10, null],
  182. ['SKU-2', 20, [GetStockItemDataInterface::QUANTITY => 5, GetStockItemDataInterface::IS_SALABLE => 1]],
  183. ['SKU-2', 30, [GetStockItemDataInterface::QUANTITY => 5, GetStockItemDataInterface::IS_SALABLE => 1]],
  184. ['SKU-3', 10, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  185. ['SKU-3', 20, null],
  186. ['SKU-3', 30, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  187. ];
  188. }
  189. /**
  190. * @param string $sku
  191. * @param string $sourceCode
  192. * @return SourceItemInterface
  193. */
  194. private function getSourceItem(string $sku, string $sourceCode): SourceItemInterface
  195. {
  196. $searchCriteria = $this->searchCriteriaBuilder
  197. ->addFilter(SourceItemInterface::SKU, $sku)
  198. ->addFilter(SourceItemInterface::SOURCE_CODE, $sourceCode)
  199. ->create();
  200. $sourceItems = $this->sourceItemRepository->getList($searchCriteria)->getItems();
  201. return reset($sourceItems);
  202. }
  203. }