StockIndexerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\Stock\StockIndexer;
  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 StockIndexerTest extends TestCase
  14. {
  15. /**
  16. * @var StockIndexer
  17. */
  18. private $stockIndexer;
  19. /**
  20. * @var GetStockItemData
  21. */
  22. private $getStockItemData;
  23. /**
  24. * @var RemoveIndexData
  25. */
  26. private $removeIndexData;
  27. protected function setUp()
  28. {
  29. $this->stockIndexer = Bootstrap::getObjectManager()->get(StockIndexer::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. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  43. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  44. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  45. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  46. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  47. *
  48. * @param string $sku
  49. * @param int $stockId
  50. * @param array|null $expectedData
  51. *
  52. * @dataProvider reindexRowDataProvider
  53. *
  54. * @magentoDbIsolation disabled
  55. */
  56. public function testReindexRow(string $sku, int $stockId, $expectedData)
  57. {
  58. $this->stockIndexer->executeRow(10);
  59. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  60. self::assertEquals($expectedData, $stockItemData);
  61. }
  62. /**
  63. * @return array
  64. */
  65. public function reindexRowDataProvider(): array
  66. {
  67. return [
  68. ['SKU-1', 10, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  69. ['SKU-2', 10, null],
  70. ['SKU-3', 10, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  71. ];
  72. }
  73. /**
  74. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  75. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  76. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  77. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  78. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  79. *
  80. * @param string $sku
  81. * @param int $stockId
  82. * @param array|null $expectedData
  83. *
  84. * @dataProvider reindexListDataProvider
  85. *
  86. * @magentoDbIsolation disabled
  87. */
  88. public function testReindexList(string $sku, int $stockId, $expectedData)
  89. {
  90. $this->stockIndexer->executeList([10, 20]);
  91. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  92. self::assertEquals($expectedData, $stockItemData);
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function reindexListDataProvider(): array
  98. {
  99. return [
  100. ['SKU-1', 10, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  101. ['SKU-1', 20, null],
  102. ['SKU-2', 10, null],
  103. ['SKU-2', 20, [GetStockItemDataInterface::QUANTITY => 5, GetStockItemDataInterface::IS_SALABLE => 1]],
  104. ['SKU-3', 10, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  105. ['SKU-3', 20, null],
  106. ];
  107. }
  108. /**
  109. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  110. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  111. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  112. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  113. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  114. *
  115. * @param string $sku
  116. * @param int $stockId
  117. * @param array|null $expectedData
  118. *
  119. * @dataProvider reindexAllDataProvider
  120. *
  121. * @magentoDbIsolation disabled
  122. */
  123. public function testReindexAll(string $sku, int $stockId, $expectedData)
  124. {
  125. $this->stockIndexer->executeFull();
  126. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  127. self::assertEquals($expectedData, $stockItemData);
  128. }
  129. /**
  130. * @return array
  131. */
  132. public function reindexAllDataProvider(): array
  133. {
  134. return [
  135. ['SKU-1', 10, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  136. ['SKU-1', 20, null],
  137. ['SKU-1', 30, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  138. ['SKU-2', 10, null],
  139. ['SKU-2', 20, [GetStockItemDataInterface::QUANTITY => 5, GetStockItemDataInterface::IS_SALABLE => 1]],
  140. ['SKU-2', 30, [GetStockItemDataInterface::QUANTITY => 5, GetStockItemDataInterface::IS_SALABLE => 1]],
  141. ['SKU-3', 10, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  142. ['SKU-3', 20, null],
  143. ['SKU-3', 30, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  144. ];
  145. }
  146. }