public function testAddStockStatusToProducts() { $storeId = 1; $productId = 2; $status = 'test'; $productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['setIsSalable', 'getId'])->getMock(); $productMock->expects($this->once())->method('setIsSalable')->with($status); $stockStatusMock = $this->getMockBuilder('Magento\\CatalogInventory\\Api\\Data\\StockStatusInterface')->disableOriginalConstructor()->getMock(); $stockStatusMock->expects($this->once())->method('getStockStatus')->willReturn($status); $productCollectionMock = $this->getMockBuilder('Magento\\Catalog\\Model\\ResourceModel\\Collection\\AbstractCollection')->disableOriginalConstructor()->getMock(); $productCollectionMock->expects($this->any())->method('getItemById')->with($productId)->willReturn($productMock); $productCollectionMock->expects($this->any())->method('getStoreId')->willReturn($storeId); $productMock->expects($this->any())->method('getId')->willReturn($productId); $iteratorMock = new \ArrayIterator([$productMock]); $productCollectionMock->expects($this->any())->method('getIterator')->willReturn($iteratorMock); $this->stockRegistryProviderMock->expects($this->once())->method('getStockStatus')->withAnyParameters()->willReturn($stockStatusMock); $this->assertNull($this->stock->addStockStatusToProducts($productCollectionMock)); }
/** * Add Stock items to product collection * * @param EventObserver $observer * @return $this */ public function addInventoryDataToCollection($observer) { $productCollection = $observer->getEvent()->getProductCollection(); $this->stockHelper->addStockStatusToProducts($productCollection); }
/** * Add information about product stock status to collection * Used in for product collection after load * * @param EventObserver $observer * @return void */ public function execute(EventObserver $observer) { $productCollection = $observer->getEvent()->getCollection(); $this->stockHelper->addStockStatusToProducts($productCollection); }