/** * {@inheritdoc} */ public function assignedProducts($categoryId) { $category = $this->getCategory($categoryId); $productsPosition = $category->getProductsPosition(); /** @var \Magento\Framework\Data\Collection\Db $products */ $products = $category->getProductCollection(); /** @var \Magento\Catalog\Service\V1\Data\Eav\Category\Product[] $dtoProductList */ $dtoProductList = []; /** @var \Magento\Catalog\Model\Product $product */ foreach ($products->getItems() as $productId => $product) { $dtoProductList[] = $this->productLinkBuilder->populateWithArray([ProductLink::SKU => $product->getSku(), ProductLink::POSITION => $productsPosition[$productId]])->create(); } return $dtoProductList; }
public function testAssignedProducts() { $categoryId = 3; $productPosition = 1; $productId = $categoryId + 6; $productSku = "sku{$productId}"; $productDto = $this->getMockBuilder('Magento\\Catalog\\Service\\V1\\Data\\Category\\ProductLink')->disableOriginalConstructor()->getMock(); /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productObject */ $productObject = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $productObject->expects($this->once())->method('getSku')->will($this->returnValue($productSku)); /** @var \Magento\Framework\Data\Collection\Db|\PHPUnit_Framework_MockObject_MockObject $productCollection */ $productCollection = $this->getMockBuilder('Magento\\Framework\\Data\\Collection\\Db')->disableOriginalConstructor()->getMock(); $productCollection->expects($this->any())->method('getItems')->will($this->returnValue([$productId => $productObject])); $this->category->expects($this->once())->method('load')->with($this->equalTo($categoryId)); $this->category->expects($this->once())->method('getId')->will($this->returnValue(333)); $this->category->expects($this->once())->method('getProductsPosition')->will($this->returnValue([$productId => $productPosition])); $this->category->expects($this->once())->method('getProductCollection')->will($this->returnValue($productCollection)); $this->productLinkBuilder->expects($this->any())->method('populateWithArray')->with($this->equalTo([ProductLink::SKU => $productSku, ProductLink::POSITION => $productPosition]))->will($this->returnValue($this->productLinkBuilder)); $this->assertEquals([$productDto], $this->model->assignedProducts($categoryId)); }