ProductAlertTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\InventoryProductAlert\Test\Integration;
  8. use Magento\Framework\Api\SearchCriteriaBuilder;
  9. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  10. use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
  11. use Magento\InventoryApi\Api\SourceItemsSaveInterface;
  12. use Magento\ProductAlert\Model\Observer;
  13. use Magento\ProductAlert\Model\ResourceModel\Stock\CollectionFactory as StockCollectionFactory;
  14. use Magento\TestFramework\Helper\Bootstrap;
  15. use PHPUnit\Framework\TestCase;
  16. /**
  17. * Check product alerts work on multi source.
  18. */
  19. class ProductAlertTest extends TestCase
  20. {
  21. /**
  22. * @var Observer
  23. */
  24. private $observer;
  25. /**
  26. * @var SourceItemRepositoryInterface
  27. */
  28. private $sourceItemRepository;
  29. /**
  30. * @var SearchCriteriaBuilder
  31. */
  32. private $searchCriteriaBuilder;
  33. /**
  34. * @var SourceItemsSaveInterface
  35. */
  36. private $sourceItemsSaveInterface;
  37. /**
  38. * @var StockCollectionFactory
  39. */
  40. private $stockCollectionFactory;
  41. /**
  42. * @inheritdoc
  43. */
  44. public function setUp()
  45. {
  46. $this->observer = Bootstrap::getObjectManager()->create(Observer::class);
  47. $this->sourceItemRepository = Bootstrap::getObjectManager()->get(SourceItemRepositoryInterface::class);
  48. $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
  49. $this->sourceItemsSaveInterface = Bootstrap::getObjectManager()->get(SourceItemsSaveInterface::class);
  50. $this->stockCollectionFactory = Bootstrap::getObjectManager()->get(StockCollectionFactory::class);
  51. }
  52. /**
  53. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  54. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  55. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  56. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  57. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  58. * @magentoDataFixture ../../../../app/code/Magento/InventoryCatalog/Test/_files/source_items_on_default_source.php
  59. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  60. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  61. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  62. * @magentoDataFixture Magento/Customer/_files/customer.php
  63. * @magentoDataFixture ../../../../app/code/Magento/InventoryProductAlert/Test/_files/product_alert_customer.php
  64. * @magentoDataFixture ../../../../app/code/Magento/InventoryProductAlert/Test/_files/customer_eu_website_id.php
  65. * @magentoDataFixture ../../../../app/code/Magento/InventoryProductAlert/Test/_files/product_alert_eu_website_customer.php
  66. * @magentoConfigFixture default_store catalog/productalert/allow_stock 1
  67. * @magentoConfigFixture store_for_eu_website_store catalog/productalert/allow_stock 1
  68. *
  69. * @magentoDbIsolation disabled
  70. */
  71. public function testAlertsBothSourceItemsOutOfStock()
  72. {
  73. $this->observer->process();
  74. $stockCollection = $this->stockCollectionFactory->create();
  75. $count = 0;
  76. /** @var \Magento\ProductAlert\Model\Stock $stock */
  77. foreach ($stockCollection as $stock) {
  78. $count += $stock->getSendCount();
  79. }
  80. $this->assertEquals(0, $count);
  81. }
  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/stock_source_links.php
  87. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  88. * @magentoDataFixture ../../../../app/code/Magento/InventoryCatalog/Test/_files/source_items_on_default_source.php
  89. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  90. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  91. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  92. * @magentoDataFixture Magento/Customer/_files/customer.php
  93. * @magentoDataFixture ../../../../app/code/Magento/InventoryProductAlert/Test/_files/product_alert_customer.php
  94. * @magentoDataFixture ../../../../app/code/Magento/InventoryProductAlert/Test/_files/customer_eu_website_id.php
  95. * @magentoDataFixture ../../../../app/code/Magento/InventoryProductAlert/Test/_files/product_alert_eu_website_customer.php
  96. * @magentoConfigFixture default_store catalog/productalert/allow_stock 1
  97. * @magentoConfigFixture store_for_eu_website_store catalog/productalert/allow_stock 1
  98. *
  99. * @magentoDbIsolation disabled
  100. */
  101. public function testAlertsOneSourceItemInStock()
  102. {
  103. $this->observer->process();
  104. $stockCollection = $this->stockCollectionFactory->create();
  105. $count = 0;
  106. /** @var \Magento\ProductAlert\Model\Stock $stock */
  107. foreach ($stockCollection as $stock) {
  108. $count += $stock->getSendCount();
  109. }
  110. $this->assertEquals(0, $count);
  111. $this->changeProductIsInStock('eu-2', 1);
  112. $this->observer->process();
  113. $stockCollection = $this->stockCollectionFactory->create();
  114. $count = 0;
  115. /** @var \Magento\ProductAlert\Model\Stock $stock */
  116. foreach ($stockCollection as $stock) {
  117. $count += $stock->getSendCount();
  118. }
  119. $this->assertEquals(1, $count);
  120. }
  121. /**
  122. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  123. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  124. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  125. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  126. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  127. * @magentoDataFixture ../../../../app/code/Magento/InventoryCatalog/Test/_files/source_items_on_default_source.php
  128. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  129. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  130. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  131. * @magentoDataFixture Magento/Customer/_files/customer.php
  132. * @magentoDataFixture ../../../../app/code/Magento/InventoryProductAlert/Test/_files/product_alert_customer.php
  133. * @magentoDataFixture ../../../../app/code/Magento/InventoryProductAlert/Test/_files/customer_eu_website_id.php
  134. * @magentoDataFixture ../../../../app/code/Magento/InventoryProductAlert/Test/_files/product_alert_eu_website_customer.php
  135. * @magentoConfigFixture default_store catalog/productalert/allow_stock 1
  136. * @magentoConfigFixture store_for_eu_website_store catalog/productalert/allow_stock 1
  137. *
  138. * @magentoDbIsolation disabled
  139. */
  140. public function testAlertsBothSourceItemsInStock()
  141. {
  142. $this->changeProductIsInStock('eu-2', 1);
  143. $this->changeProductIsInStock('default', 1);
  144. $this->observer->process();
  145. $stockCollection = $this->stockCollectionFactory->create();
  146. $count = 0;
  147. /** @var \Magento\ProductAlert\Model\Stock $stock */
  148. foreach ($stockCollection as $stock) {
  149. $count += $stock->getSendCount();
  150. }
  151. $this->assertEquals(2, $count);
  152. }
  153. /**
  154. * @param string $sourceCode
  155. * @param int $isInStock
  156. *
  157. * @return void
  158. */
  159. private function changeProductIsInStock(string $sourceCode, int $isInStock)
  160. {
  161. $searchCriteria = $this->searchCriteriaBuilder
  162. ->addFilter(SourceItemInterface::SKU, 'SKU-3')
  163. ->addFilter(SourceItemInterface::SOURCE_CODE, $sourceCode)
  164. ->create();
  165. $items = $this->sourceItemRepository->getList($searchCriteria)->getItems();
  166. /** @var SourceItemInterface $sourceItem */
  167. $sourceItem = reset($items);
  168. $sourceItem->setStatus($isInStock);
  169. if ($isInStock) {
  170. $sourceItem->setQuantity($sourceItem->getQuantity() ?: 1);
  171. }
  172. $this->sourceItemsSaveInterface->execute([$sourceItem]);
  173. }
  174. }