/** * Before prepare product collection handler * * @param \Magento\Catalog\Model\Layer $subject * @param \Magento\Catalog\Model\Resource\Collection\AbstractCollection $collection * * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforePrepareProductCollection(\Magento\Catalog\Model\Layer $subject, \Magento\Catalog\Model\Resource\Collection\AbstractCollection $collection) { if ($this->_isEnabledShowOutOfStock()) { return; } $this->_stockStatus->addIsInStockFilterToCollection($collection); }
/** * 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); }
/** * Add stock status to prepare index select * * @param EventObserver $observer * @return $this */ public function addStockStatusToPrepareIndexSelect(EventObserver $observer) { $website = $observer->getEvent()->getWebsite(); $select = $observer->getEvent()->getSelect(); $this->_stockStatus->addStockStatusToSelect($select, $website); return $this; }
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)); }
/** * Add stock status limitation to catalog product price index select object * * @param EventObserver $observer * @return $this */ public function prepareCatalogProductIndexSelect(EventObserver $observer) { $select = $observer->getEvent()->getSelect(); $entity = $observer->getEvent()->getEntityField(); $website = $observer->getEvent()->getWebsiteField(); $this->_stockStatus->prepareCatalogProductIndexSelect($select, $entity, $website); return $this; }
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)); }
/** * Save Product Status per website * * @param Stock\Status $object * @param int $productId * @param int $status * @param float|int $qty * @param int $stockId * @param int|null $websiteId * @return $this */ public function saveProductStatus(Stock\Status $object, $productId, $status, $qty = 0, $stockId = 1, $websiteId = null) { $websites = array_keys($object->getWebsites($websiteId)); $adapter = $this->_getWriteAdapter(); foreach ($websites as $websiteId) { $select = $adapter->select()->from($this->getMainTable())->where('product_id = :product_id')->where('website_id = :website_id')->where('stock_id = :stock_id'); $bind = array(':product_id' => $productId, ':website_id' => $websiteId, ':stock_id' => $stockId); $row = $adapter->fetchRow($select, $bind); if ($row) { $bind = array('qty' => $qty, 'stock_status' => $status); $where = array($adapter->quoteInto('product_id=?', (int) $row['product_id']), $adapter->quoteInto('website_id=?', (int) $row['website_id']), $adapter->quoteInto('stock_id=?', (int) $row['stock_id'])); $adapter->update($this->getMainTable(), $bind, $where); } else { $bind = array('product_id' => $productId, 'website_id' => $websiteId, 'stock_id' => $stockId, 'qty' => $qty, 'stock_status' => $status); $adapter->insert($this->getMainTable(), $bind); } } return $this; }
/** * {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(); }
/** * Add stock item objects to products * * @param array $productCollection * @return $this */ public function addItemsToProducts($productCollection) { $items = $this->getItemCollection()->addProductsFilter($productCollection)->joinStockStatus($productCollection->getStoreId())->load(); $stockItems = array(); foreach ($items as $item) { $stockItems[$item->getProductId()] = $item; } foreach ($productCollection as $product) { if (isset($stockItems[$product->getId()])) { $this->stockStatus->assignProduct($product, $stockItems[$product->getId()]->getStockId(), $product->getStockStatus()); } } return $this; }