/** * Test for method _toHtml * * @return void */ public function testToHtml() { $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getProductUrl', 'getDescription', 'getAllowedPriceInRss', 'getName', '__wakeup', 'getResourceCollection'], [], '', false); $productCollectionMock = $this->getMock('Magento\\Catalog\\Model\\Resource\\Product\\CollectionFactory', ['addPriceDataFieldFilter', 'addPriceData', 'addAttributeToSelect', 'addAttributeToSort', 'getSelect'], [], '', false); $rssObjMock = $this->getMock('Magento\\Rss\\Model\\Rss', [], [], '', false); $productUrl = '<a href="http://product.url">Product Url</a>'; $imgThumbSrc = 'http://source-for-thumbnail'; $productTitle = 'Product title'; $basePriceFormatted = '<span class="price">$10.00</span>'; $finalPriceFormatted = '<span class="price">$20.00</span>'; $productDescription = '<table><tr>' . '<td><a href="' . $productUrl . '"><img src="' . $imgThumbSrc . '" alt="" border="0" align="left" height="75" width="75" /></a></td>' . '<td style="text-decoration:none;"><p>Price: ' . $basePriceFormatted . ' Special Price: ' . $finalPriceFormatted . '</p></td></tr></table>'; $expectedData = ['title' => $productTitle, 'link' => $productUrl, 'description' => $productDescription]; $expectedResult = new \Magento\Framework\Object(['rss_feed' => '<xml>Feed of the rss</xml>']); $this->addMocks(); $this->productFactoryMock->expects($this->once())->method('create')->will($this->returnValue($productMock)); $productMock->expects($this->once())->method('getResourceCollection')->will($this->returnValue($productCollectionMock)); $productCollectionMock->expects($this->once())->method('addPriceDataFieldFilter')->will($this->returnSelf()); $productCollectionMock->expects($this->once())->method('addPriceData')->will($this->returnSelf()); $productCollectionMock->expects($this->once())->method('addAttributeToSelect')->will($this->returnSelf()); $productCollectionMock->expects($this->once())->method('addAttributeToSort')->will($this->returnSelf()); $this->rssFactoryMock->expects($this->once())->method('create')->will($this->returnValue($rssObjMock)); $productMock->expects($this->exactly(2))->method('getProductUrl')->will($this->returnValue($productUrl)); $this->imageHelperMock->expects($this->once())->method('resize')->will($this->returnValue($imgThumbSrc)); $productMock->expects($this->any())->method('getAllowedPriceInRss')->will($this->returnValue(true)); $productMock->expects($this->once())->method('getName')->will($this->returnValue($productTitle)); $this->priceCurrencyMock->expects($this->exactly(2))->method('convertAndFormat')->will($this->returnValueMap([[10, true, PriceCurrencyInterface::DEFAULT_PRECISION, null, null, $basePriceFormatted], [20, true, PriceCurrencyInterface::DEFAULT_PRECISION, null, null, $finalPriceFormatted]])); $rssObjMock->expects($this->once())->method('_addEntry')->with($expectedData)->will($this->returnSelf()); $rssObjMock->expects($this->once())->method('createRssXml')->will($this->returnValue($expectedResult)); $this->assertEquals($expectedResult, $this->block->toHtml()); }
/** * Test for method _toHtml for the case, when wishlist is absent */ public function testToHtmlWithoutWishlist() { $url = 'http://base.url/index'; $rssString = '<xml>Some empty xml</xml>'; $rssObjMock = $this->getMock('Magento\\Rss\\Model\\Rss', [], [], '', false); $customerServiceMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Customer', [], [], '', false); $wishlistModelMock = $this->getMock('Magento\\Wishlist\\Model\\Wishlist', ['getId', '__wakeup', 'getCustomerId'], [], '', false); $expectedHeaders = ['title' => __('We cannot retrieve the wish list.'), 'description' => __('We cannot retrieve the wish list.'), 'link' => $url, 'charset' => 'UTF-8']; $this->rssFactoryMock->expects($this->once())->method('create')->will($this->returnValue($rssObjMock)); $this->wishlistHelperMock->expects($this->once())->method('getWishlist')->will($this->returnValue($wishlistModelMock)); $wishlistModelMock->expects($this->once())->method('getId')->will($this->returnValue(false)); $this->urlBuilderMock->expects($this->once())->method('getUrl')->will($this->returnValue($url)); $this->wishlistHelperMock->expects($this->once())->method('getCustomer')->will($this->returnValue($customerServiceMock)); $rssObjMock->expects($this->once())->method('_addHeader')->with($expectedHeaders)->will($this->returnSelf()); $rssObjMock->expects($this->once())->method('createRssXml')->will($this->returnValue($rssString)); $this->assertEquals($rssString, $this->block->toHtml()); }