/** * @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); }
/** * {inheritdoc} */ public function getProductStockStatusBySku($sku) { $product = $this->productLoader->load($sku); $productId = $product->getId(); if (!$productId) { throw new NoSuchEntityException("Product with SKU \"{$sku}\" does not exist"); } $data = $this->stockStatus->getProductStockStatus([$productId], $this->scopeResolver->getScope()->getId()); $stockStatus = (bool) $data[$productId]; $result = [Data\StockStatus::STOCK_STATUS => $stockStatus, Data\StockStatus::STOCK_QTY => $this->stockItemService->getStockQty($productId)]; $this->stockStatusBuilder->populateWithArray($result); return $this->stockStatusBuilder->create(); }