SourceIndexerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\InventoryIndexer\Indexer\Source\SourceIndexer;
  9. use Magento\InventoryIndexer\Model\ResourceModel\GetStockItemData;
  10. use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. use PHPUnit\Framework\TestCase;
  13. class SourceIndexerTest extends TestCase
  14. {
  15. /**
  16. * @var SourceIndexer
  17. */
  18. private $sourceIndexer;
  19. /**
  20. * @var GetStockItemData
  21. */
  22. private $getStockItemData;
  23. /**
  24. * @var RemoveIndexData
  25. */
  26. private $removeIndexData;
  27. protected function setUp()
  28. {
  29. $this->sourceIndexer = Bootstrap::getObjectManager()->get(SourceIndexer::class);
  30. $this->getStockItemData = Bootstrap::getObjectManager()->get(GetStockItemData::class);
  31. $this->removeIndexData = Bootstrap::getObjectManager()->get(RemoveIndexData::class);
  32. $this->removeIndexData->execute([10, 20, 30]);
  33. }
  34. /**
  35. * We broke transaction during indexation so we need to clean db state manually
  36. */
  37. protected function tearDown()
  38. {
  39. $this->removeIndexData->execute([10, 20, 30]);
  40. }
  41. /**
  42. * Source 'eu-1' is assigned on EU-stock(id:10) and Global-stock(id:30)
  43. * Thus these stocks stocks be reindexed
  44. *
  45. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  46. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  47. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  48. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  49. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  50. *
  51. * @param string $sku
  52. * @param int $stockId
  53. * @param array|null $expectedData
  54. *
  55. * @dataProvider reindexRowDataProvider
  56. *
  57. * @magentoDbIsolation disabled
  58. */
  59. public function testReindexRow(string $sku, int $stockId, $expectedData)
  60. {
  61. $this->sourceIndexer->executeRow('eu-1');
  62. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  63. self::assertEquals($expectedData, $stockItemData);
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function reindexRowDataProvider(): array
  69. {
  70. return [
  71. ['SKU-1', 10, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  72. ['SKU-1', 30, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  73. ['SKU-2', 10, null],
  74. ['SKU-2', 30, [GetStockItemDataInterface::QUANTITY => 5, GetStockItemDataInterface::IS_SALABLE => 1]],
  75. ['SKU-3', 10, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  76. ['SKU-3', 30, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  77. ];
  78. }
  79. /**
  80. * Source 'eu-1' and 'us-1' are assigned on EU-stock(id:10), US-stock(id:20) and Global-stock(id:30)
  81. * Thus these stocks should be reindexed
  82. *
  83. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  84. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  85. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  86. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  87. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  88. *
  89. * @param string $sku
  90. * @param int $stockId
  91. * @param array|null $expectedData
  92. *
  93. * @dataProvider reindexListDataProvider
  94. *
  95. * @magentoDbIsolation disabled
  96. */
  97. public function testReindexList(string $sku, int $stockId, $expectedData)
  98. {
  99. $this->sourceIndexer->executeList(['eu-1', 'us-1']);
  100. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  101. self::assertEquals($expectedData, $stockItemData);
  102. }
  103. /**
  104. * All of stocks should be reindexed
  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 testReindexAll(string $sku, int $stockId, $expectedData)
  121. {
  122. $this->sourceIndexer->executeFull();
  123. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  124. self::assertEquals($expectedData, $stockItemData);
  125. }
  126. /**
  127. * @return array
  128. */
  129. public function reindexListDataProvider(): array
  130. {
  131. return [
  132. ['SKU-1', 10, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  133. ['SKU-1', 20, null],
  134. ['SKU-1', 30, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  135. ['SKU-2', 10, null],
  136. ['SKU-2', 20, [GetStockItemDataInterface::QUANTITY => 5, GetStockItemDataInterface::IS_SALABLE => 1]],
  137. ['SKU-2', 30, [GetStockItemDataInterface::QUANTITY => 5, GetStockItemDataInterface::IS_SALABLE => 1]],
  138. ['SKU-3', 10, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  139. ['SKU-3', 20, null],
  140. ['SKU-3', 30, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  141. ];
  142. }
  143. }