protected function setUp() { parent::setUp(); $this->stockRegistryMock = $this->getMockBuilder(StockRegistryInterface::class)->setMethods(['getStockItem'])->getMockForAbstractClass(); $this->storeMock = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock(); $this->stockItemMock = $this->getMockBuilder(StockItemInterface::class)->setMethods(['getData'])->getMockForAbstractClass(); $this->stockRegistryMock->expects($this->any())->method('getStockItem')->willReturn($this->stockItemMock); $this->productMock->expects($this->any())->method('getStore')->willReturn($this->storeMock); }
protected function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->registryMock = $this->getMock('Magento\\Framework\\Registry', [], [], '', false); $this->stockItem = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', ['getQtyIncrements'], '', false); $this->stockItem->expects($this->any())->method('getStockItem')->willReturn(1); $this->stockRegistry = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\StockRegistryInterface', ['getStockItem'], '', false); $this->stockRegistry->expects($this->any())->method('getStockItem')->willReturn($this->stockItem); $this->block = $objectManager->getObject('Magento\\CatalogInventory\\Block\\Qtyincrements', ['registry' => $this->registryMock, 'stockRegistry' => $this->stockRegistry]); }
public function testBuildWithCurrentProductStockItem() { $expectedData = ['use_config_min_qty' => 1, 'use_config_min_sale_qty' => 1, 'use_config_max_sale_qty' => 1, 'use_config_backorders' => 1, 'use_config_notify_stock_qty' => 1, 'use_config_enable_qty_inc' => 'use_config_enable_qty_inc', 'enable_qty_increments' => 'enable_qty_increments', 'use_config_qty_increments' => 'use_config_qty_increments', 'qty_increments' => 'qty_increments']; $this->stockRegistry->expects($this->once())->method('getStockItem')->will($this->returnValue($this->stockItemDoMock)); $this->stockItemDoMock->expects($this->any())->method('getItemId')->will($this->returnValue(50)); $this->stockItemDoMock->expects($this->any())->method('getUseConfigEnableQtyInc')->will($this->returnValue('use_config_enable_qty_inc')); $this->stockItemDoMock->expects($this->any())->method('getEnableQtyIncrements')->will($this->returnValue('enable_qty_increments')); $this->stockItemDoMock->expects($this->any())->method('getUseConfigQtyIncrements')->will($this->returnValue('use_config_qty_increments')); $this->stockItemDoMock->expects($this->any())->method('getQtyIncrements')->will($this->returnValue('qty_increments')); $this->duplicateMock->expects($this->once())->method('setStockData')->with($expectedData); $this->model->build($this->productMock, $this->duplicateMock); }
/** * Run test getProductStockQty method * * @return void */ public function testGetProductStockQty() { $productId = 10; $websiteId = 99; $qty = 100.0; $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getId', 'getStore'], [], '', false); $storeMock = $this->getMock('Magento\\Store\\Model\\Store', ['getWebsiteId'], [], '', false); $stockItemMock = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', [], '', false, true, true, ['getQty']); $productMock->expects($this->once())->method('getId')->will($this->returnValue($productId)); $productMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock)); $storeMock->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId)); $this->stockRegistryMock->expects($this->once())->method('getStockItem')->with($productId, $websiteId)->will($this->returnValue($stockItemMock)); $stockItemMock->expects($this->once())->method('getQty')->will($this->returnValue($qty)); $this->assertEquals($qty, $this->_block->getProductStockQty($productMock)); }
public function testSaveInventoryData() { $productId = 4; $websiteId = 5; $stockData = null; $websitesChanged = true; $statusChanged = true; $product = $this->getMock('Magento\\Catalog\\Model\\Product', ['getStockData', 'getIsChangedWebsites', 'dataHasChangedFor', 'getId', 'getStore', '__wakeup'], [], '', false); $product->expects($this->once())->method('getStockData')->will($this->returnValue($stockData)); if ($stockData === null) { $product->expects($this->any())->method('getIsChangedWebsites')->will($this->returnValue($websitesChanged)); $product->expects($this->any())->method('dataHasChangedFor')->will($this->returnValue($statusChanged)); if ($websitesChanged || $statusChanged) { $product->expects($this->once())->method('getId')->will($this->returnValue($productId)); $store = $this->getMock('Magento\\Store\\Model\\Store', ['getWebsiteId', '__wakeup'], [], '', false); $store->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId)); $product->expects($this->once())->method('getStore')->will($this->returnValue($store)); $this->stockIndex->expects($this->once())->method('rebuild')->will($this->returnValue(true)); } } else { $stockItem = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItem', ['__wakeup'], '', false); $this->stockRegistry->expects($this->once())->method('getStockItem')->with($productId, $websiteId)->will($this->returnValue($stockItem)); } $this->event->expects($this->once())->method('getProduct')->will($this->returnValue($product)); $this->eventObserver->expects($this->atLeastOnce())->method('getEvent')->will($this->returnValue($this->event)); $this->assertEquals($this->observer, $this->observer->saveInventoryData($this->eventObserver)); }
public function testAfterGetQuantityValidators() { $result = ['validate-item-quantity' => ['minAllowed' => 2, 'maxAllowed' => 5, 'qtyIncrements' => 3]]; $validators = []; $productViewBlock = $this->getMockBuilder('Magento\\Catalog\\Block\\Product\\View')->disableOriginalConstructor()->getMock(); $productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['_wakeup', 'getId', 'getStore'])->getMock(); $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->setMethods(['getWebsiteId', '_wakeup'])->getMock(); $productViewBlock->expects($this->any())->method('getProduct')->willReturn($productMock); $productMock->expects($this->once())->method('getId')->willReturn('productId'); $productMock->expects($this->once())->method('getStore')->willReturn($storeMock); $storeMock->expects($this->once())->method('getWebsiteId')->willReturn('websiteId'); $this->stockRegistry->expects($this->once())->method('getStockItem')->with('productId', 'websiteId')->willReturn($this->stockItem); $this->stockItem->expects($this->once())->method('getQtyMinAllowed')->willReturn(2); $this->stockItem->expects($this->any())->method('getQtyMaxAllowed')->willReturn(5); $this->stockItem->expects($this->any())->method('getQtyIncrements')->willReturn(3); $this->assertEquals($result, $this->block->afterGetQuantityValidators($productViewBlock, $validators)); }
/** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage The stock item for Product in option is not valid. */ public function testInitializeWithInvalidOptionQty() { $optionValue = 5; $qty = 10; $this->optionMock->expects($this->once())->method('getValue')->will($this->returnValue($optionValue)); $this->quoteItemMock->expects($this->once())->method('getQtyToAdd')->will($this->returnValue(false)); $this->productMock->expects($this->any())->method('getId')->will($this->returnValue($this->productId)); $this->optionMock->expects($this->any())->method('getProduct')->will($this->returnValue($this->productMock)); $this->stockItemMock->expects($this->once())->method('getItemId')->will($this->returnValue(false)); $this->stockRegistry->expects($this->once())->method('getStockItem')->will($this->returnValue($this->stockItemMock)); $this->validator->initialize($this->optionMock, $this->quoteItemMock, $qty); }
public function testExecuteThatProductIdsAreObtainedFromAttributeHelper() { $this->attributeHelper->expects($this->any())->method('getProductIds')->will($this->returnValue([5])); $this->attributeHelper->expects($this->any())->method('getSelectedStoreId')->will($this->returnValue([1])); $this->attributeHelper->expects($this->any())->method('getStoreWebsiteId')->will($this->returnValue(1)); $this->stockConfig->expects($this->any())->method('getConfigItemOptions')->will($this->returnValue([])); $this->dataObjectHelperMock->expects($this->any())->method('populateWithArray')->with($this->stockItem, $this->anything(), '\\Magento\\CatalogInventory\\Api\\Data\\StockItemInterface')->willReturnSelf(); $this->product->expects($this->any())->method('isProductsHasSku')->with([5])->will($this->returnValue(true)); $this->stockItemService->expects($this->any())->method('getStockItem')->with(5, 1)->will($this->returnValue($this->stockItem)); $this->stockIndexerProcessor->expects($this->any())->method('reindexList')->with([5]); $this->request->expects($this->any())->method('getParam')->will($this->returnValueMap([['inventory', [], [7]]])); $this->messageManager->expects($this->never())->method('addError'); $this->messageManager->expects($this->never())->method('addException'); $this->object->execute(); }
/** * Run test getMinimalQty method * * @param int $minSale * @param int|null $result * @return void * * @dataProvider dataProviderGetMinimalQty */ public function testGetMinimalQty($minSale, $result) { $id = 10; $websiteId = 99; $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getId', 'getStore'], [], '', false); $storeMock = $this->getMock('Magento\\Store\\Model\\Store', ['getWebsiteId'], [], '', false); $stockItemMock = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', [], '', false, true, true, ['getMinSaleQty']); $this->stockRegistryMock->expects($this->once())->method('getStockItem')->with($id, $websiteId)->will($this->returnValue($stockItemMock)); $productMock->expects($this->once())->method('getId')->will($this->returnValue($id)); $productMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock)); $storeMock->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId)); $stockItemMock->expects($this->once())->method('getMinSaleQty')->will($this->returnValue($minSale)); /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productMock */ $this->assertEquals($result, $this->block->getMinimalQty($productMock)); }
public function testExecuteThatProductIdsAreObtainedFromAttributeHelper() { $this->attributeHelper->expects($this->any())->method('getProductIds')->will($this->returnValue([5])); $this->attributeHelper->expects($this->any())->method('getSelectedStoreId')->will($this->returnValue([1])); $this->attributeHelper->expects($this->any())->method('getStoreWebsiteId')->will($this->returnValue(1)); $this->stockConfig->expects($this->any())->method('getConfigItemOptions')->will($this->returnValue([])); $itemToSave = $this->getMockBuilder('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface')->disableOriginalConstructor()->setMethods(['setItemId', 'save'])->getMockForAbstractClass(); $this->stockItemBuilder->expects($this->any())->method('mergeDataObjectWithArray')->withAnyParameters()->willReturnSelf(); $this->stockItemBuilder->expects($this->any())->method('create')->willReturn($itemToSave); $this->product->expects($this->any())->method('isProductsHasSku')->with([5])->will($this->returnValue(true)); $this->stockItemService->expects($this->any())->method('getStockItem')->with(5, 1)->will($this->returnValue($this->stockItem)); $this->stockIndexerProcessor->expects($this->any())->method('reindexList')->with([5]); $this->request->expects($this->any())->method('getParam')->will($this->returnValueMap([['inventory', [], [7]]])); $this->messageManager->expects($this->never())->method('addError'); $this->messageManager->expects($this->never())->method('addException'); $this->object->execute(); }
/** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Invalid stock item id: 35. Assigned stock item id is 40 */ public function testAroundSaveWithNotAssignedStockItemId() { $stockId = 80; $stockItemId = 35; $defaultScopeId = 100; $defaultStockId = 80; $storedStockitemId = 40; $this->stockItem->expects($this->once())->method('getStockId')->willReturn($stockId); $this->stockRegistry->expects($this->once())->method('getStock')->with($defaultScopeId)->willReturn($this->defaultStock); $this->stockConfiguration->expects($this->once())->method('getDefaultScopeId')->willReturn($defaultScopeId); $this->defaultStock->expects($this->once())->method('getStockId')->willReturn($defaultStockId); $this->product->expects($this->once())->method('getExtensionAttributes')->willReturn($this->productExtension); $this->productExtension->expects($this->once())->method('getStockItem')->willReturn($this->stockItem); $this->stockItem->expects($this->once())->method('getItemId')->willReturn($stockItemId); $storedStockItem = $this->getMockBuilder(StockItemInterface::class)->setMethods(['getItemId'])->getMockForAbstractClass(); $storedStockItem->expects($this->once())->method('getItemId')->willReturn($storedStockitemId); $this->stockRegistry->expects($this->once())->method('getStockItem')->willReturn($storedStockItem); $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product); }
public function testAroundSave() { $productId = 5494; $websiteId = 1; $storeId = 2; $sku = 'my product that needs saving'; $this->productMock->expects($this->once())->method('getExtensionAttributes')->willReturn($this->productExtensionMock); $this->productExtensionMock->expects($this->once())->method('getStockItem')->willReturn($this->stockItemMock); $storeMock = $this->getMockBuilder('\\Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId); $this->storeManager->expects($this->once())->method('getStore')->with($storeId)->willReturn($storeMock); $this->savedProductMock->expects($this->once())->method('getId')->willReturn($productId); $this->savedProductMock->expects($this->atLeastOnce())->method('getStoreId')->willReturn($storeId); $this->savedProductMock->expects($this->atLeastOnce())->method('getSku')->willReturn($sku); $this->stockItemMock->expects($this->once())->method('setProductId')->with($productId); $this->stockItemMock->expects($this->once())->method('setWebsiteId')->with($websiteId); $this->stockRegistry->expects($this->once())->method('updateStockItemBySku')->with($sku, $this->stockItemMock); $newProductMock = $this->getMockBuilder('Magento\\Catalog\\Api\\Data\\ProductInterface')->disableOriginalConstructor()->getMock(); $this->productRepositoryMock->expects($this->once())->method('get')->with($sku, false, $storeId, true)->willReturn($newProductMock); $this->assertEquals($newProductMock, $this->plugin->aroundSave($this->productRepositoryMock, $this->closureMock, $this->productMock)); }
/** * Run test getConfigFieldValue method * * @param int $stockId * @param array $methods * @param string $result * @return void * * @dataProvider dataProviderGetConfigFieldValue */ public function testGetConfigFieldValue($stockId, $methods, $result) { $productId = 10; $websiteId = 15; $fieldName = 'field'; $stockItemMock = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', [], '', false, false, false, $methods); $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', [], [], '', false); $storeMock = $this->getMock('Magento\\Store\\Model\\Store', [], [], '', false); $productMock->expects($this->once())->method('getId')->will($this->returnValue($productId)); $productMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock)); $storeMock->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId)); $this->coreRegistryMock->expects($this->any())->method('registry')->with('product')->will($this->returnValue($productMock)); $this->stockRegistryMock->expects($this->once())->method('getStockItem')->with($productId, $websiteId)->will($this->returnValue($stockItemMock)); $stockItemMock->expects($this->once())->method('getItemId')->will($this->returnValue($stockId)); if (!empty($methods)) { $stockItemMock->expects($this->once())->method(reset($methods))->will($this->returnValue('call-method')); } if (empty($methods) || empty($stockId)) { $this->stockConfigurationMock->expects($this->once())->method('getDefaultConfigValue')->will($this->returnValue('default-result')); } $resultField = $this->inventory->getConfigFieldValue($fieldName); $this->assertEquals($result, $resultField); }