Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductAlertTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 use Magento\ProductAlert\Model\ResourceModel\Stock\CollectionFactory as StockCollectionFactory;
17 use PHPUnit\Framework\TestCase;
18 
22 class ProductAlertTest extends TestCase
23 {
27  private $observer;
28 
32  private $sourceItemRepository;
33 
37  private $searchCriteriaBuilder;
38 
42  private $sourceItemsSaveInterface;
43 
47  private $stockCollectionFactory;
48 
52  public function setUp()
53  {
54  $this->observer = Bootstrap::getObjectManager()->create(Observer::class);
55  $this->sourceItemRepository = Bootstrap::getObjectManager()->get(SourceItemRepositoryInterface::class);
56  $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
57  $this->sourceItemsSaveInterface = Bootstrap::getObjectManager()->get(SourceItemsSaveInterface::class);
58  $this->stockCollectionFactory = Bootstrap::getObjectManager()->get(StockCollectionFactory::class);
59  }
60 
80  public function testAlertsBothSourceItemsOutOfStock()
81  {
82  $this->observer->process();
83  $stockCollection = $this->stockCollectionFactory->create();
84  $count = 0;
86  foreach ($stockCollection as $stock) {
87  $count += $stock->getSendCount();
88  }
89  $this->assertEquals(0, $count);
90  }
91 
111  public function testAlertsOneSourceItemInStock()
112  {
113  $this->observer->process();
114  $stockCollection = $this->stockCollectionFactory->create();
115  $count = 0;
117  foreach ($stockCollection as $stock) {
118  $count += $stock->getSendCount();
119  }
120  $this->assertEquals(0, $count);
121 
122  $this->changeProductIsInStock('eu-2', 1);
123  $this->observer->process();
124 
125  $stockCollection = $this->stockCollectionFactory->create();
126  $count = 0;
128  foreach ($stockCollection as $stock) {
129  $count += $stock->getSendCount();
130  }
131  $this->assertEquals(1, $count);
132  }
133 
153  public function testAlertsBothSourceItemsInStock()
154  {
155  $this->changeProductIsInStock('eu-2', 1);
156  $this->changeProductIsInStock('default', 1);
157  $this->observer->process();
158 
159  $stockCollection = $this->stockCollectionFactory->create();
160  $count = 0;
162  foreach ($stockCollection as $stock) {
163  $count += $stock->getSendCount();
164  }
165  $this->assertEquals(2, $count);
166  }
167 
174  private function changeProductIsInStock(string $sourceCode, int $isInStock)
175  {
176  $searchCriteria = $this->searchCriteriaBuilder
177  ->addFilter(SourceItemInterface::SKU, 'SKU-3')
179  ->create();
180 
181  $items = $this->sourceItemRepository->getList($searchCriteria)->getItems();
183  $sourceItem = reset($items);
184  $sourceItem->setStatus($isInStock);
185  if ($isInStock) {
186  $sourceItem->setQuantity($sourceItem->getQuantity() ?: 1);
187  }
188  $this->sourceItemsSaveInterface->execute([$sourceItem]);
189  }
190 }
$count
Definition: recent.phtml:13
$searchCriteria
$sourceCode
Definition: inventory.phtml:11
$stock
$items