Skip to content

Commit e6eef42

Browse files
committed
MAGETWO-8709: [GITHUB] Child product image should be shown in Wishist if options are selected for configurable product #8168
- fixing unit tests
1 parent 1b82046 commit e6eef42

File tree

6 files changed

+97
-51
lines changed

6 files changed

+97
-51
lines changed

app/code/Magento/Checkout/CustomerData/DefaultItem.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ class DefaultItem extends AbstractItem
4040
protected $checkoutHelper;
4141

4242
/**
43-
* Escaper
44-
*
4543
* @var \Magento\Framework\Escaper
4644
*/
4745
private $escaper;
4846

49-
/** @var ItemResolverInterface */
47+
/**
48+
* @var ItemResolverInterface
49+
*/
5050
private $itemResolver;
5151

5252
/**
@@ -82,10 +82,7 @@ public function __construct(
8282
*/
8383
protected function doGetItemData()
8484
{
85-
$imageHelper = $this->imageHelper->init(
86-
$this->itemResolver->getFinalProduct($this->item),
87-
'mini_cart_product_thumbnail'
88-
);
85+
$imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
8986
$productName = $this->escaper->escapeHtml($this->item->getProduct()->getName());
9087

9188
return [
@@ -125,7 +122,6 @@ protected function getOptionList()
125122

126123
/**
127124
* @return \Magento\Catalog\Model\Product
128-
* @deprecated
129125
* @codeCoverageIgnore
130126
*/
131127
protected function getProductForThumbnail()

app/code/Magento/Checkout/Test/Unit/Block/Cart/Item/RendererTest.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Catalog\Model\Product;
1010
use Magento\Checkout\Block\Cart\Item\Renderer;
1111
use Magento\Quote\Model\Quote\Item;
12+
use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;
1213

1314
/**
1415
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -18,17 +19,22 @@ class RendererTest extends \PHPUnit\Framework\TestCase
1819
/**
1920
* @var Renderer
2021
*/
21-
protected $_renderer;
22+
private $renderer;
2223

2324
/**
2425
* @var \PHPUnit_Framework_MockObject_MockObject
2526
*/
26-
protected $layout;
27+
private $layout;
2728

2829
/**
2930
* @var \Magento\Catalog\Block\Product\ImageBuilder|\PHPUnit_Framework_MockObject_MockObject
3031
*/
31-
protected $imageBuilder;
32+
private $imageBuilder;
33+
34+
/**
35+
* @var ItemResolverInterface|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $itemResolver;
3238

3339
protected function setUp()
3440
{
@@ -47,19 +53,24 @@ protected function setUp()
4753
->disableOriginalConstructor()
4854
->getMock();
4955

50-
$this->_renderer = $objectManagerHelper->getObject(
56+
$this->itemResolver = $this->createMock(
57+
ItemResolverInterface::class
58+
);
59+
60+
$this->renderer = $objectManagerHelper->getObject(
5161
\Magento\Checkout\Block\Cart\Item\Renderer::class,
5262
[
5363
'context' => $context,
5464
'imageBuilder' => $this->imageBuilder,
65+
'itemResolver' => $this->itemResolver,
5566
]
5667
);
5768
}
5869

5970
public function testGetProductForThumbnail()
6071
{
6172
$product = $this->_initProduct();
62-
$productForThumbnail = $this->_renderer->getProductForThumbnail();
73+
$productForThumbnail = $this->renderer->getProductForThumbnail();
6374
$this->assertEquals($product->getName(), $productForThumbnail->getName(), 'Invalid product was returned.');
6475
}
6576

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

84-
$this->_renderer->setItem($item);
95+
$this->itemResolver->expects($this->any())
96+
->method('getFinalProduct')
97+
->with($item)
98+
->will($this->returnValue($product));
99+
100+
$this->renderer->setItem($item);
85101
return $product;
86102
}
87103

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

96-
$this->assertEquals($product->getIdentities(), $this->_renderer->getIdentities());
112+
$this->assertEquals($product->getIdentities(), $this->renderer->getIdentities());
97113
}
98114

99115
public function testGetIdentitiesFromEmptyItem()
100116
{
101-
$this->assertEmpty($this->_renderer->getIdentities());
117+
$this->assertEmpty($this->renderer->getIdentities());
102118
}
103119

104120
/**
@@ -133,7 +149,7 @@ public function testGetProductPriceHtml()
133149
]
134150
)->will($this->returnValue($priceHtml));
135151

136-
$this->assertEquals($priceHtml, $this->_renderer->getProductPriceHtml($product));
152+
$this->assertEquals($priceHtml, $this->renderer->getProductPriceHtml($product));
137153
}
138154

139155
public function testGetActions()
@@ -150,7 +166,7 @@ public function testGetActions()
150166

151167
$this->layout->expects($this->once())
152168
->method('getChildName')
153-
->with($this->_renderer->getNameInLayout(), 'actions')
169+
->with($this->renderer->getNameInLayout(), 'actions')
154170
->willReturn($blockNameInLayout);
155171
$this->layout->expects($this->once())
156172
->method('getBlock')
@@ -171,14 +187,14 @@ public function testGetActions()
171187
->method('toHtml')
172188
->willReturn($blockHtml);
173189

174-
$this->assertEquals($blockHtml, $this->_renderer->getActions($itemMock));
190+
$this->assertEquals($blockHtml, $this->renderer->getActions($itemMock));
175191
}
176192

177193
public function testGetActionsWithNoBlock()
178194
{
179195
$this->layout->expects($this->once())
180196
->method('getChildName')
181-
->with($this->_renderer->getNameInLayout(), 'actions')
197+
->with($this->renderer->getNameInLayout(), 'actions')
182198
->willReturn(false);
183199

184200
/**
@@ -188,7 +204,7 @@ public function testGetActionsWithNoBlock()
188204
->disableOriginalConstructor()
189205
->getMock();
190206

191-
$this->assertEquals('', $this->_renderer->getActions($itemMock));
207+
$this->assertEquals('', $this->renderer->getActions($itemMock));
192208
}
193209

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

201-
$this->imageBuilder->expects(self::once())
217+
$this->imageBuilder->expects($this->once())
202218
->method('create')
203219
->with($product, $imageId, $attributes)
204220
->willReturn($imageMock);
205221

206-
static::assertInstanceOf(
222+
$this->assertInstanceOf(
207223
Image::class,
208-
$this->_renderer->getImage($product, $imageId, $attributes)
224+
$this->renderer->getImage($product, $imageId, $attributes)
209225
);
210226
}
211227
}

app/code/Magento/Checkout/Test/Unit/CustomerData/DefaultItemTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
*/
66
namespace Magento\Checkout\Test\Unit\CustomerData;
77

8+
use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;
9+
810
class DefaultItemTest extends \PHPUnit\Framework\TestCase
911
{
1012
/**
1113
* @var \Magento\Checkout\CustomerData\DefaultItem
1214
*/
13-
protected $model;
15+
private $model;
1416

1517
/**
1618
* @var \Magento\Catalog\Helper\Image
@@ -22,6 +24,11 @@ class DefaultItemTest extends \PHPUnit\Framework\TestCase
2224
*/
2325
private $configurationPool;
2426

27+
/**
28+
* @var ItemResolverInterface|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $itemResolver;
31+
2532
protected function setUp()
2633
{
2734
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -35,12 +42,14 @@ protected function setUp()
3542
$checkoutHelper = $this->getMockBuilder(\Magento\Checkout\Helper\Data::class)
3643
->setMethods(['formatPrice'])->disableOriginalConstructor()->getMock();
3744
$checkoutHelper->expects($this->any())->method('formatPrice')->willReturn(5);
45+
$this->itemResolver = $this->createMock(ItemResolverInterface::class);
3846
$this->model = $objectManager->getObject(
3947
\Magento\Checkout\CustomerData\DefaultItem::class,
4048
[
4149
'imageHelper' => $this->imageHelper,
4250
'configurationPool' => $this->configurationPool,
43-
'checkoutHelper' => $checkoutHelper
51+
'checkoutHelper' => $checkoutHelper,
52+
'itemResolver' => $this->itemResolver,
4453
]
4554
);
4655
}
@@ -73,6 +82,11 @@ public function testGetItemData()
7382
$this->imageHelper->expects($this->any())->method('getHeight')->willReturn(100);
7483
$this->configurationPool->expects($this->any())->method('getByProductType')->willReturn($product);
7584

85+
$this->itemResolver->expects($this->any())
86+
->method('getFinalProduct')
87+
->with($item)
88+
->will($this->returnValue($product));
89+
7690
$itemData = $this->model->getItemData($item);
7791
$this->assertArrayHasKey('options', $itemData);
7892
$this->assertArrayHasKey('qty', $itemData);

app/code/Magento/Checkout/Test/Unit/Model/Cart/ImageProviderTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,34 @@ class ImageProviderTest extends \PHPUnit\Framework\TestCase
1111
/**
1212
* @var \Magento\Checkout\Model\Cart\ImageProvider
1313
*/
14-
public $model;
14+
private $model;
1515

1616
/**
1717
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Quote\Api\CartItemRepositoryInterface
1818
*/
19-
protected $itemRepositoryMock;
19+
private $itemRepositoryMock;
2020

2121
/**
2222
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Checkout\CustomerData\ItemPoolInterface
2323
*/
24-
protected $itemPoolMock;
24+
private $itemPoolMock;
25+
26+
/**
27+
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Checkout\CustomerData\DefaultItem
28+
*/
29+
private $customerItem;
2530

2631
protected function setUp()
2732
{
2833
$this->itemRepositoryMock = $this->createMock(\Magento\Quote\Api\CartItemRepositoryInterface::class);
2934
$this->itemPoolMock = $this->createMock(\Magento\Checkout\CustomerData\ItemPoolInterface::class);
35+
$this->customerItem = $this->getMockBuilder(\Magento\Checkout\CustomerData\DefaultItem::class)
36+
->disableOriginalConstructor()
37+
->getMock();
3038
$this->model = new \Magento\Checkout\Model\Cart\ImageProvider(
3139
$this->itemRepositoryMock,
32-
$this->itemPoolMock
40+
$this->itemPoolMock,
41+
$this->customerItem
3342
);
3443
}
3544

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

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

4958
$this->assertEquals($expectedResult, $this->model->getImages($cartId));
5059
}

app/code/Magento/Wishlist/CustomerData/Wishlist.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;
99
use Magento\Customer\CustomerData\SectionSourceInterface;
1010
use Magento\Framework\App\ObjectManager;
11-
use Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Image;
1211

1312
/**
1413
* Wishlist section
@@ -41,29 +40,31 @@ class Wishlist implements SectionSourceInterface
4140
protected $block;
4241

4342
/**
44-
* @var \Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Image
43+
* @var \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface
4544
*/
46-
private $image;
45+
private $itemResolver;
4746

4847
/**
4948
* @param \Magento\Wishlist\Helper\Data $wishlistHelper
5049
* @param \Magento\Wishlist\Block\Customer\Sidebar $block
5150
* @param \Magento\Catalog\Helper\ImageFactory $imageHelperFactory
5251
* @param \Magento\Framework\App\ViewInterface $view
53-
* @param Image|null $image
52+
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface|null $itemResolver
5453
*/
5554
public function __construct(
5655
\Magento\Wishlist\Helper\Data $wishlistHelper,
5756
\Magento\Wishlist\Block\Customer\Sidebar $block,
5857
\Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
5958
\Magento\Framework\App\ViewInterface $view,
60-
Image $image = null
59+
\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemResolver = null
6160
) {
6261
$this->wishlistHelper = $wishlistHelper;
6362
$this->imageHelperFactory = $imageHelperFactory;
6463
$this->block = $block;
6564
$this->view = $view;
66-
$this->image = $image ?? ObjectManager::getInstance()->get(Image::class);
65+
$this->itemResolver = $itemResolver ?? ObjectManager::getInstance()->get(
66+
\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface::class
67+
);
6768
}
6869

6970
/**
@@ -131,14 +132,8 @@ protected function getItems()
131132
protected function getItemData(\Magento\Wishlist\Model\Item $wishlistItem)
132133
{
133134
$product = $wishlistItem->getProduct();
134-
135-
/** @var \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemProductResolver */
136-
$itemProductResolver = ObjectManager::getInstance()->get(
137-
\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface::class
138-
);
139-
140135
return [
141-
'image' => $this->getImageData($itemProductResolver->getFinalProduct($wishlistItem)),
136+
'image' => $this->getImageData($this->itemResolver->getFinalProduct($wishlistItem)),
142137
'product_sku' => $product->getSku(),
143138
'product_id' => $product->getId(),
144139
'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem),

0 commit comments

Comments
 (0)