BackorderConditionTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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\InventorySales\Test\Integration\GetStockItemData;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
  10. use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
  11. use Magento\Framework\Api\SearchCriteriaBuilder;
  12. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  13. use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
  14. use Magento\InventoryApi\Api\SourceItemsSaveInterface;
  15. use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
  16. use Magento\TestFramework\Helper\Bootstrap;
  17. use PHPUnit\Framework\TestCase;
  18. /**
  19. * @magentoAppIsolation enabled
  20. * @magentoDbIsolation disabled
  21. */
  22. class BackorderConditionTest extends TestCase
  23. {
  24. /**
  25. * @var GetStockItemDataInterface
  26. */
  27. private $getStockItemData;
  28. /**
  29. * @var ProductRepositoryInterface
  30. */
  31. private $productRepository;
  32. /**
  33. * @var StockItemRepositoryInterface
  34. */
  35. private $stockItemRepository;
  36. /**
  37. * @var StockItemCriteriaInterfaceFactory
  38. */
  39. private $stockItemCriteriaFactory;
  40. /**
  41. * @var SourceItemsSaveInterface
  42. */
  43. private $sourceItemsSave;
  44. /**
  45. * @var SourceItemRepositoryInterface
  46. */
  47. private $sourceItemRepository;
  48. /**
  49. * @var SearchCriteriaBuilder
  50. */
  51. private $searchCriteriaBuilder;
  52. /**
  53. * @inheritdoc
  54. */
  55. protected function setUp()
  56. {
  57. parent::setUp();
  58. $this->getStockItemData = Bootstrap::getObjectManager()->get(GetStockItemDataInterface::class);
  59. $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
  60. $this->stockItemRepository = Bootstrap::getObjectManager()->get(StockItemRepositoryInterface::class);
  61. $this->stockItemCriteriaFactory = Bootstrap::getObjectManager()->get(
  62. StockItemCriteriaInterfaceFactory::class
  63. );
  64. $this->sourceItemRepository = Bootstrap::getObjectManager()->get(SourceItemRepositoryInterface::class);
  65. $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
  66. $this->sourceItemsSave = Bootstrap::getObjectManager()->get(SourceItemsSaveInterface::class);
  67. }
  68. /**
  69. * Tests inventory_stock_* is_salable value when backorders are globally disabled.
  70. *
  71. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  72. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  73. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  74. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  75. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  76. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  77. * @magentoConfigFixture current_store cataloginventory/item_options/backorders 0
  78. * @dataProvider backordersDisabledDataProvider
  79. *
  80. * @param string $sku
  81. * @param int $stockId
  82. * @param array|null $expectedData
  83. */
  84. public function testBackordersDisabled(string $sku, int $stockId, $expectedData)
  85. {
  86. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  87. self::assertEquals($expectedData, $stockItemData);
  88. }
  89. /**
  90. * Tests inventory_stock_* is_salable value when backorders are globally enabled.
  91. *
  92. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  93. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  94. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  95. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  96. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  97. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  98. * @magentoConfigFixture current_store cataloginventory/item_options/backorders 1
  99. * @dataProvider backordersEnabledDataProvider
  100. *
  101. * @param string $sku
  102. * @param int $stockId
  103. * @param array|null $expectedData
  104. */
  105. public function testGlobalBackordersEnabled(string $sku, int $stockId, $expectedData)
  106. {
  107. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  108. self::assertEquals($expectedData, $stockItemData);
  109. }
  110. /**
  111. * Tests inventory_stock_* is_salable value when backorders for stock items are disabled.
  112. *
  113. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  114. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  115. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  116. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  117. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  118. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  119. * @magentoConfigFixture current_store cataloginventory/item_options/backorders 1
  120. * @dataProvider backordersDisabledDataProvider
  121. *
  122. * @param string $sku
  123. * @param int $stockId
  124. * @param array|null $expectedData
  125. */
  126. public function testStockItemBackordersDisabled(string $sku, int $stockId, $expectedData)
  127. {
  128. $this->setStockItemBackorders($sku, 0);
  129. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  130. self::assertEquals($expectedData, $stockItemData);
  131. }
  132. /**
  133. * Tests inventory_stock_* is_salable value when backorders for stock items are enabled.
  134. *
  135. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  136. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  137. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  138. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  139. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  140. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  141. * @magentoConfigFixture current_store cataloginventory/item_options/backorders 0
  142. * @dataProvider backordersEnabledDataProvider
  143. *
  144. * @param string $sku
  145. * @param int $stockId
  146. * @param array|null $expectedData
  147. */
  148. public function testStockItemBackordersEnabled(string $sku, int $stockId, $expectedData)
  149. {
  150. $this->setStockItemBackorders($sku, 1);
  151. $stockItemData = $this->getStockItemData->execute($sku, $stockId);
  152. self::assertEquals($expectedData, $stockItemData);
  153. }
  154. /**
  155. * Data provider for test with enabled backorders.
  156. *
  157. * @return array
  158. */
  159. public function backordersEnabledDataProvider(): array
  160. {
  161. return [
  162. ['SKU-1', 10, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  163. ['SKU-2', 10, null],
  164. ['SKU-3', 10, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 1]],
  165. ];
  166. }
  167. /**
  168. * Data provider for test with disabled backorders.
  169. *
  170. * @return array
  171. */
  172. public function backordersDisabledDataProvider(): array
  173. {
  174. return [
  175. ['SKU-1', 10, [GetStockItemDataInterface::QUANTITY => 8.5, GetStockItemDataInterface::IS_SALABLE => 1]],
  176. ['SKU-2', 10, null],
  177. ['SKU-3', 10, [GetStockItemDataInterface::QUANTITY => 0, GetStockItemDataInterface::IS_SALABLE => 0]],
  178. ];
  179. }
  180. /**
  181. * Set products backorder status.
  182. *
  183. * @param string $sku
  184. * @param int $backordersStatus
  185. */
  186. private function setStockItemBackorders(string $sku, int $backordersStatus): void
  187. {
  188. $product = $this->productRepository->get($sku);
  189. $stockItemSearchCriteria = $this->stockItemCriteriaFactory->create();
  190. $stockItemSearchCriteria->setProductsFilter($product->getId());
  191. $stockItemsCollection = $this->stockItemRepository->getList($stockItemSearchCriteria);
  192. /** @var StockItemInterface $legacyStockItem */
  193. $legacyStockItem = current($stockItemsCollection->getItems());
  194. $legacyStockItem->setBackorders($backordersStatus);
  195. $legacyStockItem->setUseConfigBackorders(0);
  196. $this->stockItemRepository->save($legacyStockItem);
  197. $sourceItem = $this->getSourceItemBySku($sku);
  198. $this->sourceItemsSave->execute([$sourceItem]);
  199. }
  200. /**
  201. * Get source item by products sku.
  202. *
  203. * @param string $sku
  204. * @return SourceItemInterface
  205. */
  206. private function getSourceItemBySku(string $sku): SourceItemInterface
  207. {
  208. $searchCriteria = $this->searchCriteriaBuilder
  209. ->addFilter('sku', $sku)
  210. ->create();
  211. $sourceItemSearchResult = $this->sourceItemRepository->getList($searchCriteria);
  212. return current($sourceItemSearchResult->getItems());
  213. }
  214. }