9 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
28 private $stockItemMock;
33 private $stockConfigurationMock;
43 private $stockStateProviderMock;
48 private $stockItemResourceMock;
53 private $stockItemFactoryMock;
58 private $stockItemCollectionMock;
63 private $productFactoryMock;
68 private $queryBuilderFactoryMock;
78 private $localeDateMock;
83 private $indexProcessorMock;
93 private $stockRegistryStorage;
100 $this->stockItemMock = $this->getMockBuilder(\
Magento\CatalogInventory\Model\Stock\Item::class)
101 ->disableOriginalConstructor()
107 'setStockStatusChangedAutomaticallyFlag',
108 'getStockStatusChangedAutomaticallyFlag',
111 'setStockStatusChangedAuto',
112 'hasStockStatusChangedAutomaticallyFlag',
121 $this->stockConfigurationMock = $this->getMockBuilder(
122 \
Magento\CatalogInventory\Api\StockConfigurationInterface::class
124 ->disableOriginalConstructor()
126 $this->stockStateProviderMock = $this->getMockBuilder(
127 \
Magento\CatalogInventory\Model\Spi\StockStateProviderInterface::class
129 ->disableOriginalConstructor()
131 $this->stockItemResourceMock = $this->getMockBuilder(
134 ->disableOriginalConstructor()
136 $this->stockItemFactoryMock = $this->getMockBuilder(
137 \
Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory::class
139 ->setMethods([
'create'])
140 ->disableOriginalConstructor()
142 $this->stockItemCollectionMock = $this->getMockBuilder(
143 \
Magento\CatalogInventory\Api\Data\StockItemCollectionInterfaceFactory::class
145 ->setMethods([
'create'])
146 ->disableOriginalConstructor()
148 $this->productFactoryMock = $this->getMockBuilder(\
Magento\Catalog\Model\ProductFactory::class)
149 ->disableOriginalConstructor()
150 ->setMethods([
'load',
'create'])
152 $this->productMock = $this->getMockBuilder(\
Magento\Catalog\Model\Product::class)
153 ->disableOriginalConstructor()
154 ->setMethods([
'load',
'getId',
'getTypeId',
'__wakeup'])
157 $this->productFactoryMock->expects($this->any())->method(
'create')->willReturn($this->productMock);
159 $this->queryBuilderFactoryMock = $this->getMockBuilder(\
Magento\Framework\DB\QueryBuilderFactory::class)
160 ->setMethods([
'create'])
161 ->disableOriginalConstructor()
163 $this->mapperMock = $this->getMockBuilder(\
Magento\Framework\DB\MapperFactory::class)
164 ->disableOriginalConstructor()
166 $this->localeDateMock = $this->getMockBuilder(\
Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)
167 ->disableOriginalConstructor()
169 $this->indexProcessorMock = $this->createPartialMock(
170 \
Magento\CatalogInventory\Model\Indexer\Stock\Processor::class,
173 $this->dateTime = $this->createPartialMock(\
Magento\Framework\Stdlib\DateTime\DateTime::class, [
'gmtDate']);
174 $this->stockRegistryStorage = $this->getMockBuilder(StockRegistryStorage::class)
175 ->disableOriginalConstructor()
180 )->disableOriginalConstructor()->getMock();
184 $productCollection->expects($this->any())->method(
'addFieldToSelect')->willReturnSelf();
185 $productCollection->expects($this->any())->method(
'getFirstItem')->willReturn($this->productMock);
187 $productCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
188 ->setMethods([
'create'])
189 ->disableOriginalConstructor()
192 $productCollectionFactory->expects($this->any())->method(
'create')->willReturn(
$productCollection);
195 StockItemRepository::class,
197 'stockConfiguration' => $this->stockConfigurationMock,
198 'stockStateProvider' => $this->stockStateProviderMock,
199 'resource' => $this->stockItemResourceMock,
200 'stockItemFactory' => $this->stockItemFactoryMock,
201 'stockItemCollectionFactory' => $this->stockItemCollectionMock,
202 'productFactory' => $this->productFactoryMock,
203 'queryBuilderFactory' => $this->queryBuilderFactoryMock,
204 'mapperFactory' => $this->mapperMock,
205 'localeDate' => $this->localeDateMock,
206 'indexProcessor' => $this->indexProcessorMock,
207 'dateTime' => $this->dateTime,
208 'stockRegistryStorage' => $this->stockRegistryStorage,
209 'productCollectionFactory' => $productCollectionFactory,
217 $this->stockItemMock->expects($this->atLeastOnce())->method(
'getProductId')->willReturn(
$productId);
218 $this->stockRegistryStorage->expects($this->once())->method(
'removeStockItem')->with(
$productId);
219 $this->stockRegistryStorage->expects($this->once())->method(
'removeStockStatus')->with(
$productId);
221 $this->stockItemResourceMock->expects($this->once())
223 ->with($this->stockItemMock)
226 $this->assertTrue($this->model->delete($this->stockItemMock));
234 $this->stockItemResourceMock->expects($this->once())
236 ->with($this->stockItemMock)
237 ->willThrowException(
new \Exception());
239 $this->model->delete($this->stockItemMock);
246 $this->stockItemFactoryMock->expects($this->once())->method(
'create')->willReturn($this->stockItemMock);
247 $this->stockItemResourceMock->expects($this->once())->method(
'load')->with($this->stockItemMock,
$id);
248 $this->stockItemMock->expects($this->once())->method(
'getItemId')->willReturn(
$id);
250 $this->assertTrue($this->model->deleteById(
$id));
261 $this->stockItemFactoryMock->expects($this->once())->method(
'create')->willReturn($this->stockItemMock);
262 $this->stockItemResourceMock->expects($this->once())->method(
'load')->with($this->stockItemMock,
$id);
263 $this->stockItemMock->expects($this->once())->method(
'getItemId')->willReturn(
null);
265 $this->assertTrue($this->model->deleteById(
$id));
272 $this->stockItemMock->expects($this->any())->method(
'getProductId')->willReturn(
$productId);
273 $this->productMock->expects($this->once())->method(
'getId')->willReturn(
$productId);
274 $this->productMock->expects($this->once())->method(
'getTypeId')->willReturn(
'typeId');
275 $this->stockConfigurationMock->expects($this->once())->method(
'isQty')->with(
'typeId')->willReturn(
true);
276 $this->stockStateProviderMock->expects($this->once())
277 ->method(
'verifyStock')
278 ->with($this->stockItemMock)
280 $this->stockItemMock->expects($this->once())->method(
'getManageStock')->willReturn(
true);
281 $this->stockItemMock->expects($this->once())->method(
'setIsInStock')->with(
false)->willReturnSelf();
282 $this->stockItemMock->expects($this->once())
283 ->method(
'setStockStatusChangedAutomaticallyFlag')
286 $this->stockItemMock->expects($this->any())->method(
'setLowStockDate')->willReturnSelf();
287 $this->stockStateProviderMock->expects($this->once())
288 ->method(
'verifyNotification')
289 ->with($this->stockItemMock)
291 $this->dateTime->expects($this->once())
293 $this->stockItemMock->expects($this->atLeastOnce())->method(
'setStockStatusChangedAuto')->willReturnSelf();
294 $this->stockItemMock->expects($this->once())
295 ->method(
'hasStockStatusChangedAutomaticallyFlag')
297 $this->stockItemMock->expects($this->once())
298 ->method(
'getStockStatusChangedAutomaticallyFlag')
300 $this->stockItemMock->expects($this->once())->method(
'getWebsiteId')->willReturn(1);
301 $this->stockItemMock->expects($this->once())->method(
'setWebsiteId')->with(1)->willReturnSelf();
302 $this->stockItemMock->expects($this->once())->method(
'getStockId')->willReturn(1);
303 $this->stockItemMock->expects($this->once())->method(
'setStockId')->with(1)->willReturnSelf();
304 $this->stockItemResourceMock->expects($this->once())
306 ->with($this->stockItemMock)
309 $this->assertEquals($this->stockItemMock, $this->model->save($this->stockItemMock));
316 $this->stockItemMock->expects($this->any())->method(
'getProductId')->willReturn(
$productId);
317 $this->productMock->expects($this->once())->method(
'getId')->willReturn(
null);
318 $this->stockRegistryStorage->expects($this->never())->method(
'removeStockItem');
319 $this->stockRegistryStorage->expects($this->never())->method(
'removeStockStatus');
321 $this->assertEquals($this->stockItemMock, $this->model->save($this->stockItemMock));
331 $this->stockItemMock->expects($this->any())->method(
'getProductId')->willReturn(
$productId);
332 $this->productMock->expects($this->once())->method(
'getId')->willReturn(
$productId);
333 $this->productMock->expects($this->once())->method(
'getTypeId')->willReturn(
'typeId');
334 $this->stockConfigurationMock->expects($this->once())->method(
'isQty')->with(
'typeId')->willReturn(
false);
335 $this->stockItemMock->expects($this->once())->method(
'setQty')->with(0)->willReturnSelf();
336 $this->stockItemMock->expects($this->once())->method(
'getWebsiteId')->willReturn(1);
337 $this->stockItemMock->expects($this->once())->method(
'setWebsiteId')->with(1)->willReturnSelf();
338 $this->stockItemMock->expects($this->once())->method(
'getStockId')->willReturn(1);
339 $this->stockItemMock->expects($this->once())->method(
'setStockId')->with(1)->willReturnSelf();
340 $this->stockItemResourceMock->expects($this->once())
342 ->with($this->stockItemMock)
343 ->willThrowException(
new \Exception());
345 $this->model->save($this->stockItemMock);
350 $criteriaMock = $this->getMockBuilder(\
Magento\CatalogInventory\Api\StockItemCriteriaInterface::class)
352 $queryBuilderMock = $this->getMockBuilder(\
Magento\Framework\DB\QueryBuilder::class)
353 ->disableOriginalConstructor()
354 ->setMethods([
'setCriteria',
'setResource',
'create'])
356 $queryMock = $this->getMockBuilder(\
Magento\Framework\DB\QueryInterface::class)
358 $queryCollectionMock = $this->getMockBuilder(
359 \
Magento\CatalogInventory\Api\Data\StockItemCollectionInterface::class
362 $this->queryBuilderFactoryMock->expects($this->once())->method(
'create')->willReturn($queryBuilderMock);
363 $queryBuilderMock->expects($this->once())->method(
'setCriteria')->with($criteriaMock)->willReturnSelf();
364 $queryBuilderMock->expects($this->once())
365 ->method(
'setResource')
366 ->with($this->stockItemResourceMock)
368 $queryBuilderMock->expects($this->once())->method(
'create')->willReturn($queryMock);
369 $this->stockItemCollectionMock->expects($this->once())->method(
'create')->willReturn($queryCollectionMock);
371 $this->assertEquals($queryCollectionMock, $this->model->getList($criteriaMock));
testSaveWithoutProductId()
testDeleteByIdException()