Skip to content

Commit

Permalink
MAGETWO-8709: [GITHUB] Child product image should be shown in Wishist…
Browse files Browse the repository at this point in the history
… if options are selected for configurable product #8168

- fixing unit tests
  • Loading branch information
cpartica committed Jul 10, 2018
1 parent 1b82046 commit e6eef42
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 51 deletions.
12 changes: 4 additions & 8 deletions app/code/Magento/Checkout/CustomerData/DefaultItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ class DefaultItem extends AbstractItem
protected $checkoutHelper;

/**
* Escaper
*
* @var \Magento\Framework\Escaper
*/
private $escaper;

/** @var ItemResolverInterface */
/**
* @var ItemResolverInterface
*/
private $itemResolver;

/**
Expand Down Expand Up @@ -82,10 +82,7 @@ public function __construct(
*/
protected function doGetItemData()
{
$imageHelper = $this->imageHelper->init(
$this->itemResolver->getFinalProduct($this->item),
'mini_cart_product_thumbnail'
);
$imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
$productName = $this->escaper->escapeHtml($this->item->getProduct()->getName());

return [
Expand Down Expand Up @@ -125,7 +122,6 @@ protected function getOptionList()

/**
* @return \Magento\Catalog\Model\Product
* @deprecated
* @codeCoverageIgnore
*/
protected function getProductForThumbnail()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Catalog\Model\Product;
use Magento\Checkout\Block\Cart\Item\Renderer;
use Magento\Quote\Model\Quote\Item;
use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand All @@ -18,17 +19,22 @@ class RendererTest extends \PHPUnit\Framework\TestCase
/**
* @var Renderer
*/
protected $_renderer;
private $renderer;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $layout;
private $layout;

/**
* @var \Magento\Catalog\Block\Product\ImageBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
protected $imageBuilder;
private $imageBuilder;

/**
* @var ItemResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $itemResolver;

protected function setUp()
{
Expand All @@ -47,19 +53,24 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->_renderer = $objectManagerHelper->getObject(
$this->itemResolver = $this->createMock(
ItemResolverInterface::class
);

$this->renderer = $objectManagerHelper->getObject(
\Magento\Checkout\Block\Cart\Item\Renderer::class,
[
'context' => $context,
'imageBuilder' => $this->imageBuilder,
'itemResolver' => $this->itemResolver,
]
);
}

public function testGetProductForThumbnail()
{
$product = $this->_initProduct();
$productForThumbnail = $this->_renderer->getProductForThumbnail();
$productForThumbnail = $this->renderer->getProductForThumbnail();
$this->assertEquals($product->getName(), $productForThumbnail->getName(), 'Invalid product was returned.');
}

Expand All @@ -81,7 +92,12 @@ protected function _initProduct()
$item = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
$item->expects($this->any())->method('getProduct')->will($this->returnValue($product));

$this->_renderer->setItem($item);
$this->itemResolver->expects($this->any())
->method('getFinalProduct')
->with($item)
->will($this->returnValue($product));

$this->renderer->setItem($item);
return $product;
}

Expand All @@ -93,12 +109,12 @@ public function testGetIdentities()
->method('getIdentities')
->will($this->returnValue($identities));

$this->assertEquals($product->getIdentities(), $this->_renderer->getIdentities());
$this->assertEquals($product->getIdentities(), $this->renderer->getIdentities());
}

public function testGetIdentitiesFromEmptyItem()
{
$this->assertEmpty($this->_renderer->getIdentities());
$this->assertEmpty($this->renderer->getIdentities());
}

/**
Expand Down Expand Up @@ -133,7 +149,7 @@ public function testGetProductPriceHtml()
]
)->will($this->returnValue($priceHtml));

$this->assertEquals($priceHtml, $this->_renderer->getProductPriceHtml($product));
$this->assertEquals($priceHtml, $this->renderer->getProductPriceHtml($product));
}

public function testGetActions()
Expand All @@ -150,7 +166,7 @@ public function testGetActions()

$this->layout->expects($this->once())
->method('getChildName')
->with($this->_renderer->getNameInLayout(), 'actions')
->with($this->renderer->getNameInLayout(), 'actions')
->willReturn($blockNameInLayout);
$this->layout->expects($this->once())
->method('getBlock')
Expand All @@ -171,14 +187,14 @@ public function testGetActions()
->method('toHtml')
->willReturn($blockHtml);

$this->assertEquals($blockHtml, $this->_renderer->getActions($itemMock));
$this->assertEquals($blockHtml, $this->renderer->getActions($itemMock));
}

public function testGetActionsWithNoBlock()
{
$this->layout->expects($this->once())
->method('getChildName')
->with($this->_renderer->getNameInLayout(), 'actions')
->with($this->renderer->getNameInLayout(), 'actions')
->willReturn(false);

/**
Expand All @@ -188,7 +204,7 @@ public function testGetActionsWithNoBlock()
->disableOriginalConstructor()
->getMock();

$this->assertEquals('', $this->_renderer->getActions($itemMock));
$this->assertEquals('', $this->renderer->getActions($itemMock));
}

public function testGetImage()
Expand All @@ -198,14 +214,14 @@ public function testGetImage()
$product = $this->createMock(Product::class);
$imageMock = $this->createMock(Image::class);

$this->imageBuilder->expects(self::once())
$this->imageBuilder->expects($this->once())
->method('create')
->with($product, $imageId, $attributes)
->willReturn($imageMock);

static::assertInstanceOf(
$this->assertInstanceOf(
Image::class,
$this->_renderer->getImage($product, $imageId, $attributes)
$this->renderer->getImage($product, $imageId, $attributes)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
*/
namespace Magento\Checkout\Test\Unit\CustomerData;

use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;

class DefaultItemTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Checkout\CustomerData\DefaultItem
*/
protected $model;
private $model;

/**
* @var \Magento\Catalog\Helper\Image
Expand All @@ -22,6 +24,11 @@ class DefaultItemTest extends \PHPUnit\Framework\TestCase
*/
private $configurationPool;

/**
* @var ItemResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $itemResolver;

protected function setUp()
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand All @@ -35,12 +42,14 @@ protected function setUp()
$checkoutHelper = $this->getMockBuilder(\Magento\Checkout\Helper\Data::class)
->setMethods(['formatPrice'])->disableOriginalConstructor()->getMock();
$checkoutHelper->expects($this->any())->method('formatPrice')->willReturn(5);
$this->itemResolver = $this->createMock(ItemResolverInterface::class);
$this->model = $objectManager->getObject(
\Magento\Checkout\CustomerData\DefaultItem::class,
[
'imageHelper' => $this->imageHelper,
'configurationPool' => $this->configurationPool,
'checkoutHelper' => $checkoutHelper
'checkoutHelper' => $checkoutHelper,
'itemResolver' => $this->itemResolver,
]
);
}
Expand Down Expand Up @@ -73,6 +82,11 @@ public function testGetItemData()
$this->imageHelper->expects($this->any())->method('getHeight')->willReturn(100);
$this->configurationPool->expects($this->any())->method('getByProductType')->willReturn($product);

$this->itemResolver->expects($this->any())
->method('getFinalProduct')
->with($item)
->will($this->returnValue($product));

$itemData = $this->model->getItemData($item);
$this->assertArrayHasKey('options', $itemData);
$this->assertArrayHasKey('qty', $itemData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,34 @@ class ImageProviderTest extends \PHPUnit\Framework\TestCase
/**
* @var \Magento\Checkout\Model\Cart\ImageProvider
*/
public $model;
private $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Quote\Api\CartItemRepositoryInterface
*/
protected $itemRepositoryMock;
private $itemRepositoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Checkout\CustomerData\ItemPoolInterface
*/
protected $itemPoolMock;
private $itemPoolMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Checkout\CustomerData\DefaultItem
*/
private $customerItem;

protected function setUp()
{
$this->itemRepositoryMock = $this->createMock(\Magento\Quote\Api\CartItemRepositoryInterface::class);
$this->itemPoolMock = $this->createMock(\Magento\Checkout\CustomerData\ItemPoolInterface::class);
$this->customerItem = $this->getMockBuilder(\Magento\Checkout\CustomerData\DefaultItem::class)
->disableOriginalConstructor()
->getMock();
$this->model = new \Magento\Checkout\Model\Cart\ImageProvider(
$this->itemRepositoryMock,
$this->itemPoolMock
$this->itemPoolMock,
$this->customerItem
);
}

Expand All @@ -44,7 +53,7 @@ public function testGetImages()
$expectedResult = [$itemId => $itemData['product_image']];

$this->itemRepositoryMock->expects($this->once())->method('getList')->with($cartId)->willReturn([$itemMock]);
$this->itemPoolMock->expects($this->once())->method('getItemData')->with($itemMock)->willReturn($itemData);
$this->customerItem->expects($this->once())->method('getItemData')->with($itemMock)->willReturn($itemData);

$this->assertEquals($expectedResult, $this->model->getImages($cartId));
}
Expand Down
21 changes: 8 additions & 13 deletions app/code/Magento/Wishlist/CustomerData/Wishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;
use Magento\Customer\CustomerData\SectionSourceInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Image;

/**
* Wishlist section
Expand Down Expand Up @@ -41,29 +40,31 @@ class Wishlist implements SectionSourceInterface
protected $block;

/**
* @var \Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Image
* @var \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface
*/
private $image;
private $itemResolver;

/**
* @param \Magento\Wishlist\Helper\Data $wishlistHelper
* @param \Magento\Wishlist\Block\Customer\Sidebar $block
* @param \Magento\Catalog\Helper\ImageFactory $imageHelperFactory
* @param \Magento\Framework\App\ViewInterface $view
* @param Image|null $image
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface|null $itemResolver
*/
public function __construct(
\Magento\Wishlist\Helper\Data $wishlistHelper,
\Magento\Wishlist\Block\Customer\Sidebar $block,
\Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
\Magento\Framework\App\ViewInterface $view,
Image $image = null
\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemResolver = null
) {
$this->wishlistHelper = $wishlistHelper;
$this->imageHelperFactory = $imageHelperFactory;
$this->block = $block;
$this->view = $view;
$this->image = $image ?? ObjectManager::getInstance()->get(Image::class);
$this->itemResolver = $itemResolver ?? ObjectManager::getInstance()->get(
\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface::class
);
}

/**
Expand Down Expand Up @@ -131,14 +132,8 @@ protected function getItems()
protected function getItemData(\Magento\Wishlist\Model\Item $wishlistItem)
{
$product = $wishlistItem->getProduct();

/** @var \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemProductResolver */
$itemProductResolver = ObjectManager::getInstance()->get(
\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface::class
);

return [
'image' => $this->getImageData($itemProductResolver->getFinalProduct($wishlistItem)),
'image' => $this->getImageData($this->itemResolver->getFinalProduct($wishlistItem)),
'product_sku' => $product->getSku(),
'product_id' => $product->getId(),
'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem),
Expand Down
Loading

0 comments on commit e6eef42

Please sign in to comment.