/** * Test add stock status to collection with 'display out of stock' option enabled */ public function testAddStockStatusEnabledShow() { $this->_scopeConfigMock->expects($this->once())->method('isSetFlag')->with('cataloginventory/options/show_out_of_stock')->will($this->returnValue(false)); $collectionMock = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Product\\Collection', array(), array(), '', false); $this->_stockStatusMock->expects($this->once())->method('addIsInStockFilterToCollection')->with($collectionMock); $subjectMock = $this->getMock('\\Magento\\Catalog\\Model\\Layer', array(), array(), '', false); $this->_model->beforePrepareProductCollection($subjectMock, $collectionMock); }
/** * @param string $productSku * @param int $productId * @param int $websiteId * @param array $productStockStatusArray * @param int $stockQty * @param array $array * @dataProvider getProductStockStatusBySkuDataProvider */ public function testGetProductStockStatusBySku($productSku, $productId, $websiteId, $productStockStatusArray, $stockQty, $array) { // 1. Create mocks /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */ $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); /** @var \Magento\Framework\App\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject $scope */ $scope = $this->getMockBuilder('Magento\\Framework\\App\\ScopeInterface')->disableOriginalConstructor()->getMock(); /** * @var \Magento\CatalogInventory\Service\V1\Data\StockStatus|\PHPUnit_Framework_MockObject_MockObject $scope */ $stockStatusDataObject = $this->getMockBuilder('Magento\\CatalogInventory\\Service\\V1\\Data\\StockStatus')->disableOriginalConstructor()->getMock(); // 2. Set fixtures $this->productLoader->expects($this->any())->method('load')->will($this->returnValueMap([[$productSku, $product]])); $product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $this->scopeResolver->expects($this->any())->method('getScope')->will($this->returnValue($scope)); $scope->expects($this->any())->method('getId')->will($this->returnValue($websiteId)); $this->stockStatusBuilder->expects($this->any())->method('create')->will($this->returnValue($stockStatusDataObject)); // 3. Set expectations $this->stockStatus->expects($this->any())->method('getProductStockStatus')->with([$productId], $websiteId)->will($this->returnValue($productStockStatusArray)); $this->stockItemService->expects($this->any())->method('getStockQty')->will($this->returnValueMap([[$productId, $stockQty]])); $this->stockStatusBuilder->expects($this->any())->method('populateWithArray')->with($array); // 4. Run tested method $result = $this->model->getProductStockStatusBySku($productSku); // 5. Compare actual result with expected result $this->assertEquals($stockStatusDataObject, $result); }
public function testAddItemsToProducts() { $storeId = 3; $productOneId = 1; $productOneStatus = \Magento\CatalogInventory\Model\Stock\Status::STATUS_IN_STOCK; $productTwoId = 2; $productThreeId = 3; $stockItemProductId = $productOneId; $stockItemStockId = \Magento\CatalogInventory\Model\Stock::DEFAULT_STOCK_ID; $productCollection = $this->getMockBuilder('Magento\\Catalog\\Model\\Resource\\Product\\Collection')->disableOriginalConstructor()->setMethods(['getStoreId', 'getIterator'])->getMock(); $stockItem = $this->getMockBuilder('Magento\\CatalogInventory\\Model\\Stock\\Item')->disableOriginalConstructor()->getMock(); $stockItem->expects($this->atLeastOnce())->method('getProductId')->will($this->returnValue($stockItemProductId)); $stockItem->expects($this->atLeastOnce())->method('getStockId')->will($this->returnValue($stockItemStockId)); $itemCollection = $this->getMockBuilder('Magento\\CatalogInventory\\Model\\Resource\\Stock\\Item\\Collection')->disableOriginalConstructor()->getMock(); $itemCollection->expects($this->atLeastOnce())->method('addStockFilter')->with(Stock::DEFAULT_STOCK_ID)->will($this->returnSelf()); $itemCollection->expects($this->atLeastOnce())->method('addProductsFilter')->with($productCollection)->will($this->returnSelf()); $itemCollection->expects($this->atLeastOnce())->method('joinStockStatus')->with($storeId)->will($this->returnSelf()); $itemCollection->expects($this->atLeastOnce())->method('load')->will($this->returnValue([$stockItem])); $this->collectionFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($itemCollection)); $productOne = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['getId', 'getStockStatus', '__wakeup'])->getMock(); $productOne->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($productOneId)); $productOne->expects($this->atLeastOnce())->method('getStockStatus')->will($this->returnValue($productOneStatus)); $productTwo = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $productTwo->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($productTwoId)); $productThree = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $productThree->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($productThreeId)); $productCollection->expects($this->atLeastOnce())->method('getStoreId')->will($this->returnValue($storeId)); $productCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$productOne, $productTwo, $productThree]))); $this->stockStatus->expects($this->once())->method('assignProduct')->with($productOne, $stockItemStockId, $productOneStatus); $this->assertEquals($this->model, $this->model->addItemsToProducts($productCollection)); }
public function testDelete() { $productId = 1; $this->stockStatusMock->expects($this->atLeastOnce())->method('getProductId')->willReturn($productId); $this->stockRegistryStorage->expects($this->once())->method('removeStockStatus')->with($productId); $this->stockStatusResourceMock->expects($this->once())->method('delete')->with($this->stockStatusMock)->willReturnSelf(); $this->assertTrue($this->model->delete($this->stockStatusMock)); }
public function testAddStockStatusToCollection() { $requireStockItems = false; $productCollection = $this->getMockBuilder('Magento\\Catalog\\Model\\Resource\\Product\\Collection')->disableOriginalConstructor()->setMethods(['hasFlag'])->getMock(); $this->event->expects($this->once())->method('getCollection')->will($this->returnValue($productCollection)); $productCollection->expects($this->once())->method('hasFlag')->with('require_stock_items')->will($this->returnValue($requireStockItems)); $this->stockStatus->expects($this->once())->method('addStockStatusToProducts')->with($productCollection)->will($this->returnSelf()); $this->assertEquals($this->model, $this->model->addStockStatusToCollection($this->eventObserver)); }