public function testGetAssignedProducts() { $categoryId = 42; $productId = 55; $productsPosition = [$productId => 25]; $productSku = 'testSku'; $expectedValue = 'testComplete'; $categoryMock = $this->getMock('\\Magento\\Catalog\\Model\\Category', [], [], '', false); $productMock = $this->getMock('\\Magento\\Catalog\\Model\\Product', [], [], '', false); $productMock->expects($this->once())->method('getSku')->willReturn($productSku); $items = [$productId => $productMock]; $productLinkArray = ['sku' => $productSku, 'position' => 25, 'category_id' => $categoryId]; $productsMock = $this->getMock('\\Magento\\Framework\\Data\\Collection\\Db', [], [], '', false); $this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)->willReturn($categoryMock); $categoryMock->expects($this->once())->method('getProductsPosition')->willReturn($productsPosition); $categoryMock->expects($this->once())->method('getProductCollection')->willReturn($productsMock); $categoryMock->expects($this->once())->method('getId')->willReturn($categoryId); $productsMock->expects($this->once())->method('getItems')->willReturn($items); $this->productLinkBuilderMock->expects($this->once())->method('populateWithArray')->with($productLinkArray)->willReturnSelf(); $this->productLinkBuilderMock->expects($this->once())->method('create')->willReturn($expectedValue); $this->assertEquals([$expectedValue], $this->model->getAssignedProducts($categoryId)); }
public function testGetAssignedProducts() { $categoryId = 42; $productId = 55; $productsPosition = [$productId => 25]; $productSku = 'testSku'; $categoryProductLinkMock = $this->getMock('\\Magento\\Catalog\\Api\\Data\\CategoryProductLinkInterface'); $categoryMock = $this->getMock('\\Magento\\Catalog\\Model\\Category', [], [], '', false); $productMock = $this->getMock('\\Magento\\Catalog\\Model\\Product', [], [], '', false); $productMock->expects($this->once())->method('getSku')->willReturn($productSku); $items = [$productId => $productMock]; $productsMock = $this->getMock('\\Magento\\Framework\\Data\\Collection\\AbstractDb', [], [], '', false); $this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)->willReturn($categoryMock); $categoryMock->expects($this->once())->method('getProductsPosition')->willReturn($productsPosition); $categoryMock->expects($this->once())->method('getProductCollection')->willReturn($productsMock); $categoryMock->expects($this->once())->method('getId')->willReturn($categoryId); $productsMock->expects($this->once())->method('getItems')->willReturn($items); $this->productLinkFactoryMock->expects($this->once())->method('create')->willReturn($categoryProductLinkMock); $categoryProductLinkMock->expects($this->once())->method('setSku')->with($productSku)->willReturnSelf(); $categoryProductLinkMock->expects($this->once())->method('setPosition')->with(25)->willReturnSelf(); $categoryProductLinkMock->expects($this->once())->method('setCategoryId')->with($categoryId)->willReturnSelf(); $this->assertEquals([$categoryProductLinkMock], $this->model->getAssignedProducts($categoryId)); }