Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockRegistryProviderTest.php
Go to the documentation of this file.
1 <?php
7 
9 
16 class StockRegistryProviderTest extends \PHPUnit\Framework\TestCase
17 {
20 
25 
29  protected $stock;
30 
34  protected $stockItem;
35 
39  protected $stockStatus;
40 
45 
49  protected $stockItemFactory;
50 
54  protected $stockFactory;
55 
59  protected $stockRepository;
60 
65 
70 
75 
80 
85 
89  protected $stockCriteria;
90 
94  protected $stockItemCriteria;
95 
100 
101  protected $productId = 111;
102  protected $productSku = 'simple';
103  protected $scopeId = 111;
104 
108  protected function setUp()
109  {
110  $this->objectManagerHelper = new ObjectManagerHelper($this);
111 
112  $this->stock = $this->getMockForAbstractClass(
113  \Magento\CatalogInventory\Api\Data\StockInterface::class,
114  ['__wakeup', 'getStockId'],
115  '',
116  false
117  );
118  $this->stockItem = $this->getMockForAbstractClass(
119  \Magento\CatalogInventory\Api\Data\StockItemInterface::class,
120  ['__wakeup', 'getItemId'],
121  '',
122  false
123  );
124  $this->stockStatus = $this->getMockForAbstractClass(
125  \Magento\CatalogInventory\Api\Data\StockStatusInterface::class,
126  ['__wakeup', 'getProductId'],
127  '',
128  false
129  );
130 
131  $this->stockFactory = $this->createPartialMock(
132  \Magento\CatalogInventory\Api\Data\StockInterfaceFactory::class,
133  ['create']
134  );
135  $this->stockFactory->expects($this->any())->method('create')->willReturn($this->stock);
136 
137  $this->stockItemFactory = $this->createPartialMock(
138  \Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory::class,
139  ['create']
140  );
141  $this->stockItemFactory->expects($this->any())->method('create')->willReturn($this->stockItem);
142 
143  $this->stockStatusFactory = $this->createPartialMock(
144  \Magento\CatalogInventory\Api\Data\StockStatusInterfaceFactory::class,
145  ['create']
146  );
147  $this->stockStatusFactory->expects($this->any())->method('create')->willReturn($this->stockStatus);
148 
149  $this->stockRepository = $this->getMockBuilder(\Magento\CatalogInventory\Api\StockRepositoryInterface::class)
150  ->disableOriginalConstructor()
151  ->getMock();
152 
153  $this->stockItemRepository = $this->getMockBuilder(
154  \Magento\CatalogInventory\Api\StockItemRepositoryInterface::class
155  )
156  ->disableOriginalConstructor()
157  ->getMock();
158 
159  $this->stockStatusRepository = $this->getMockBuilder(
160  \Magento\CatalogInventory\Api\StockStatusRepositoryInterface::class
161  )
162  ->disableOriginalConstructor()
163  ->getMock();
164 
165  $this->stockCriteriaFactory = $this->createPartialMock(
166  \Magento\CatalogInventory\Api\StockCriteriaInterfaceFactory::class,
167  ['create']
168  );
169  $this->stockCriteria = $this->getMockForAbstractClass(
170  \Magento\CatalogInventory\Api\StockCriteriaInterface::class,
171  ['setScopeFilter'],
172  '',
173  false
174  );
175 
176  $this->stockItemCriteriaFactory = $this->createPartialMock(
177  \Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory::class,
178  ['create']
179  );
180  $this->stockItemCriteria = $this->getMockForAbstractClass(
181  \Magento\CatalogInventory\Api\StockItemCriteriaInterface::class,
182  ['setProductsFilter', 'setScopeFilter'],
183  '',
184  false
185  );
186 
187  $this->stockStatusCriteriaFactory = $this->createPartialMock(
188  \Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory::class,
189  ['create']
190  );
191  $this->stockStatusCriteria = $this->getMockForAbstractClass(
192  \Magento\CatalogInventory\Api\StockStatusCriteriaInterface::class,
193  ['setProductsFilter', 'setScopeFilter'],
194  '',
195  false
196  );
197 
198  $this->stockRegistryProvider = $this->objectManagerHelper->getObject(
199  \Magento\CatalogInventory\Model\StockRegistryProvider::class,
200  [
201  'stockRepository' => $this->stockRepository,
202  'stockFactory' => $this->stockFactory,
203  'stockItemRepository' => $this->stockItemRepository,
204  'stockItemFactory' => $this->stockItemFactory,
205  'stockStatusRepository' => $this->stockStatusRepository,
206  'stockStatusFactory' => $this->stockStatusFactory,
207 
208  'stockCriteriaFactory' => $this->stockCriteriaFactory,
209  'stockItemCriteriaFactory' => $this->stockItemCriteriaFactory,
210  'stockStatusCriteriaFactory' => $this->stockStatusCriteriaFactory,
211  'stockRegistryStorage' => $this->createMock(\Magento\CatalogInventory\Model\StockRegistryStorage::class)
212  ]
213  );
214  }
215 
216  protected function tearDown()
217  {
218  $this->stockRegistryProvider = null;
219  }
220 
221  public function testGetStock()
222  {
223  $this->stockCriteriaFactory->expects($this->once())->method('create')->willReturn($this->stockCriteria);
224  $this->stockCriteria->expects($this->once())->method('setScopeFilter')->willReturn(null);
225  $stockCollection = $this->createPartialMock(
226  \Magento\CatalogInventory\Model\ResourceModel\Stock\Collection::class,
227  ['getFirstItem', '__wakeup', 'getItems']
228  );
229  $stockCollection->expects($this->once())->method('getItems')->willReturn([$this->stock]);
230  $this->stockRepository->expects($this->once())->method('getList')->willReturn($stockCollection);
231  $this->stock->expects($this->once())->method('getStockId')->willReturn(true);
232  $this->assertEquals($this->stock, $this->stockRegistryProvider->getStock($this->scopeId));
233  }
234 
235  public function testGetStockItem()
236  {
237  $this->stockItemCriteriaFactory->expects($this->once())->method('create')->willReturn($this->stockItemCriteria);
238  $this->stockItemCriteria->expects($this->once())->method('setProductsFilter')->willReturn(null);
239  $stockItemCollection = $this->createPartialMock(
240  \Magento\CatalogInventory\Model\ResourceModel\Stock\Item\Collection::class,
241  ['getFirstItem', '__wakeup', 'getItems']
242  );
243  $stockItemCollection->expects($this->once())->method('getItems')->willReturn([$this->stockItem]);
244  $this->stockItemRepository->expects($this->once())->method('getList')->willReturn($stockItemCollection);
245  $this->stockItem->expects($this->once())->method('getItemId')->willReturn(true);
246  $this->assertEquals(
247  $this->stockItem,
248  $this->stockRegistryProvider->getStockItem($this->productId, $this->scopeId)
249  );
250  }
251 
252  public function testGetStockStatus()
253  {
254  $this->stockStatusCriteriaFactory->expects($this->once())
255  ->method('create')
256  ->willReturn($this->stockStatusCriteria);
257  $this->stockStatusCriteria->expects($this->once())->method('setScopeFilter')->willReturn(null);
258  $this->stockStatusCriteria->expects($this->once())->method('setProductsFilter')->willReturn(null);
259  $stockStatusCollection = $this->createPartialMock(
260  \Magento\CatalogInventory\Model\ResourceModel\Stock\Status\Collection::class,
261  ['getFirstItem', '__wakeup', 'getItems']
262  );
263  $stockStatusCollection->expects($this->once())->method('getItems')->willReturn([$this->stockStatus]);
264  $this->stockStatusRepository->expects($this->once())->method('getList')->willReturn($stockStatusCollection);
265  $this->stockStatus->expects($this->once())->method('getProductId')->willReturn($this->productId);
266  $this->assertEquals(
267  $this->stockStatus,
268  $this->stockRegistryProvider->getStockStatus($this->productId, $this->scopeId)
269  );
270  }
271 }