protected function setUp()
 {
     $stockRegistryMock = $this->getMock('\\Magento\\CatalogInventory\\Api\\StockRegistryInterface');
     $this->productExtensionFactoryMock = $this->getMockBuilder('\\Magento\\Catalog\\Api\\Data\\ProductExtensionFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock();
     $this->plugin = new \Magento\CatalogInventory\Model\Plugin\AfterProductLoad($stockRegistryMock, $this->productExtensionFactoryMock);
     $productId = 5494;
     $stockItemMock = $this->getMock('\\Magento\\CatalogInventory\\Api\\Data\\StockItemInterface');
     $stockRegistryMock->expects($this->once())->method('getStockItem')->with($productId)->willReturn($stockItemMock);
     $this->productExtensionMock = $this->getMockBuilder('\\Magento\\Catalog\\Api\\Data\\ProductExtension')->setMethods(['setStockItem'])->getMock();
     $this->productExtensionMock->expects($this->once())->method('setStockItem')->with($stockItemMock)->willReturnSelf();
     $this->productMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock();
     $this->productMock->expects($this->once())->method('setExtensionAttributes')->with($this->productExtensionMock)->willReturnSelf();
     $this->productMock->expects($this->once())->method('getId')->will($this->returnValue($productId));
 }
 /**
  * @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));
 }