Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateDefaultSourceItemAtLegacyStockItemSaveTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use PHPUnit\Framework\TestCase;
14 
16 {
20  private $stockRegistry;
21 
25  private $getDefaultSourceItemBySku;
26 
27  protected function setUp()
28  {
29  parent::setUp();
30 
31  $this->stockRegistry = Bootstrap::getObjectManager()->create(StockRegistryInterface::class);
32  $this->getDefaultSourceItemBySku = Bootstrap::getObjectManager()->get(GetDefaultSourceItemBySku::class);
33  }
34 
44  {
45  $stockItem = $this->stockRegistry->getStockItemBySku('SKU-1');
46  $stockItem->setQty(10);
47  $this->stockRegistry->updateStockItemBySku('SKU-1', $stockItem);
48 
49  $defaultSourceItem = $this->getDefaultSourceItemBySku->execute('SKU-1');
50  self::assertEquals(
51  10,
52  $defaultSourceItem->getQuantity(),
53  'Quantity is not updated in default source when legacy stock is updated and product was'
54  . 'previously assigned to default source'
55  );
56  }
57 
67  {
68  $stockItem = $this->stockRegistry->getStockItemBySku('SKU-2');
69  $stockItem->setQty(10);
70  $this->stockRegistry->updateStockItemBySku('SKU-2', $stockItem);
71 
72  $defaultSourceItem = $this->getDefaultSourceItemBySku->execute('SKU-2');
73  self::assertEquals(
74  10,
75  $defaultSourceItem->getQuantity(),
76  'Quantity is not updated in default source when legacy stock is updated'
77  );
78 
79  // SKU-3 is out of stock and not assigned to default source
80  $stockItem = $this->stockRegistry->getStockItemBySku('SKU-3');
81  $stockItem->setQty(10);
82  $this->stockRegistry->updateStockItemBySku('SKU-3', $stockItem);
83 
84  $defaultSourceItem = $this->getDefaultSourceItemBySku->execute('SKU-3');
85  self::assertEquals(
86  10,
87  $defaultSourceItem->getQuantity(),
88  'Quantity is not updated in default source when legacy stock is updated and product was not '
89  . 'previously assigned to default source'
90  );
91  }
92 
101  {
102  // SKU-3 is out of stock and not assigned to default source
103  $stockItem = $this->stockRegistry->getStockItemBySku('SKU-3');
104  $stockItem->setQty(0);
105  $stockItem->setIsInStock(false);
106  $this->stockRegistry->updateStockItemBySku('SKU-3', $stockItem);
107 
108  $defaultSourceItem = $this->getDefaultSourceItemBySku->execute('SKU-3');
109  self::assertNull(
110  $defaultSourceItem,
111  'Product is assigned to default source on legacy stock item save even if it should not be'
112  );
113 
114  // SKU-5 is out of stock and not assigned to default source
115  $stockItem = $this->stockRegistry->getStockItemBySku('SKU-5');
116  $stockItem->setQty(1);
117  $stockItem->setIsInStock(true);
118  $this->stockRegistry->updateStockItemBySku('SKU-5', $stockItem);
119 
120  $defaultSourceItem = $this->getDefaultSourceItemBySku->execute('SKU-5');
121  self::assertNotNull(
122  $defaultSourceItem,
123  'Product is not assigned to default source on legacy stock item save even if it should be'
124  );
125  }
126 }