Skip to content

Commit 44e8747

Browse files
committed
Product image builder - Override attributes when builder used multiple times
1 parent 3572ee9 commit 44e8747

File tree

2 files changed

+215
-24
lines changed

2 files changed

+215
-24
lines changed

app/code/Magento/Catalog/Block/Product/ImageBuilder.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Catalog\Block\Product;
77

88
use Magento\Catalog\Helper\ImageFactory as HelperFactory;
9+
use Magento\Catalog\Model\Product;
910
use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;
1011

1112
class ImageBuilder
@@ -21,7 +22,7 @@ class ImageBuilder
2122
protected $helperFactory;
2223

2324
/**
24-
* @var \Magento\Catalog\Model\Product
25+
* @var Product
2526
*/
2627
protected $product;
2728

@@ -50,10 +51,10 @@ public function __construct(
5051
/**
5152
* Set product
5253
*
53-
* @param \Magento\Catalog\Model\Product $product
54+
* @param Product $product
5455
* @return $this
5556
*/
56-
public function setProduct(\Magento\Catalog\Model\Product $product)
57+
public function setProduct(Product $product)
5758
{
5859
$this->product = $product;
5960
return $this;
@@ -79,9 +80,7 @@ public function setImageId($imageId)
7980
*/
8081
public function setAttributes(array $attributes)
8182
{
82-
if ($attributes) {
83-
$this->attributes = $attributes;
84-
}
83+
$this->attributes = $attributes;
8584
return $this;
8685
}
8786

app/code/Magento/Catalog/Test/Unit/Block/Product/ImageBuilderTest.php

Lines changed: 210 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,27 @@
55
*/
66
namespace Magento\Catalog\Test\Unit\Block\Product;
77

8+
use Magento\Catalog\Block\Product\ImageBuilder;
9+
use Magento\Catalog\Block\Product\ImageFactory;
10+
use Magento\Catalog\Helper\Image;
11+
use Magento\Catalog\Model\Product;
12+
813
class ImageBuilderTest extends \PHPUnit\Framework\TestCase
914
{
1015
/**
11-
* @var \Magento\Catalog\Block\Product\ImageBuilder
16+
* @var ImageBuilder
1217
*/
13-
protected $model;
18+
private $model;
1419

1520
/**
1621
* @var \Magento\Catalog\Helper\ImageFactory|\PHPUnit_Framework_MockObject_MockObject
1722
*/
18-
protected $helperFactory;
23+
private $helperFactory;
1924

2025
/**
21-
* @var \Magento\Catalog\Block\Product\ImageFactory|\PHPUnit_Framework_MockObject_MockObject
26+
* @var ImageFactory|\PHPUnit_Framework_MockObject_MockObject
2227
*/
23-
protected $imageFactory;
28+
private $imageFactory;
2429

2530
protected function setUp()
2631
{
@@ -29,25 +34,22 @@ protected function setUp()
2934
->setMethods(['create'])
3035
->getMock();
3136

32-
$this->imageFactory = $this->getMockBuilder(\Magento\Catalog\Block\Product\ImageFactory::class)
37+
$this->imageFactory = $this->getMockBuilder(ImageFactory::class)
3338
->disableOriginalConstructor()
3439
->setMethods(['create'])
3540
->getMock();
3641

37-
$this->model = new \Magento\Catalog\Block\Product\ImageBuilder(
38-
$this->helperFactory,
39-
$this->imageFactory
40-
);
42+
$this->model = new ImageBuilder($this->helperFactory, $this->imageFactory);
4143
}
4244

4345
public function testSetProduct()
4446
{
45-
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
47+
$productMock = $this->getMockBuilder(Product::class)
4648
->disableOriginalConstructor()
4749
->getMock();
4850

4951
$this->assertInstanceOf(
50-
\Magento\Catalog\Block\Product\ImageBuilder::class,
52+
ImageBuilder::class,
5153
$this->model->setProduct($productMock)
5254
);
5355
}
@@ -57,7 +59,7 @@ public function testSetImageId()
5759
$imageId = 'test_image_id';
5860

5961
$this->assertInstanceOf(
60-
\Magento\Catalog\Block\Product\ImageBuilder::class,
62+
ImageBuilder::class,
6163
$this->model->setImageId($imageId)
6264
);
6365
}
@@ -68,7 +70,7 @@ public function testSetAttributes()
6870
'name' => 'value',
6971
];
7072
$this->assertInstanceOf(
71-
\Magento\Catalog\Block\Product\ImageBuilder::class,
73+
ImageBuilder::class,
7274
$this->model->setAttributes($attributes)
7375
);
7476
}
@@ -81,11 +83,11 @@ public function testCreate($data, $expected)
8183
{
8284
$imageId = 'test_image_id';
8385

84-
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
86+
$productMock = $this->getMockBuilder(Product::class)
8587
->disableOriginalConstructor()
8688
->getMock();
8789

88-
$helperMock = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class)
90+
$helperMock = $this->getMockBuilder(Image::class)
8991
->disableOriginalConstructor()
9092
->getMock();
9193
$helperMock->expects($this->once())
@@ -131,6 +133,77 @@ public function testCreate($data, $expected)
131133
$this->assertInstanceOf(\Magento\Catalog\Block\Product\Image::class, $this->model->create());
132134
}
133135

136+
/**
137+
* Check if custom attributes will be overridden when builder used few times
138+
* @param array $data
139+
* @dataProvider createMultipleCallsDataProvider
140+
*/
141+
public function testCreateMultipleCalls($data)
142+
{
143+
list ($firstCall, $secondCall) = array_values($data);
144+
145+
$imageId = 'test_image_id';
146+
147+
$productMock = $this->getMockBuilder(Product::class)
148+
->disableOriginalConstructor()
149+
->getMock();
150+
151+
$helperMock = $this->getMockBuilder(Image::class)
152+
->disableOriginalConstructor()
153+
->getMock();
154+
$helperMock->expects($this->exactly(2))
155+
->method('init')
156+
->with($productMock, $imageId)
157+
->willReturnSelf();
158+
159+
$helperMock->expects($this->exactly(2))
160+
->method('getFrame')
161+
->willReturnOnConsecutiveCalls($firstCall['data']['frame'], $secondCall['data']['frame']);
162+
$helperMock->expects($this->exactly(2))
163+
->method('getUrl')
164+
->willReturnOnConsecutiveCalls($firstCall['data']['url'], $secondCall['data']['url']);
165+
$helperMock->expects($this->exactly(4))
166+
->method('getWidth')
167+
->willReturnOnConsecutiveCalls($firstCall['data']['width'], $firstCall['data']['width'], $secondCall['data']['width'], $secondCall['data']['width']);
168+
$helperMock->expects($this->exactly(4))
169+
->method('getHeight')
170+
->willReturnOnConsecutiveCalls($firstCall['data']['height'], $firstCall['data']['height'], $secondCall['data']['height'], $secondCall['data']['height']);
171+
$helperMock->expects($this->exactly(2))
172+
->method('getLabel')
173+
->willReturnOnConsecutiveCalls($firstCall['data']['label'], $secondCall['data']['label']);
174+
$helperMock->expects($this->exactly(2))
175+
->method('getResizedImageInfo')
176+
->willReturnOnConsecutiveCalls($firstCall['data']['imagesize'], $secondCall['data']['imagesize']);
177+
$this->helperFactory->expects($this->exactly(2))
178+
->method('create')
179+
->willReturn($helperMock);
180+
181+
$imageMock = $this->getMockBuilder(\Magento\Catalog\Block\Product\Image::class)
182+
->disableOriginalConstructor()
183+
->getMock();
184+
185+
$this->imageFactory->expects($this->at(0))
186+
->method('create')
187+
->with($firstCall['expected'])
188+
->willReturn($imageMock);
189+
190+
$this->imageFactory->expects($this->at(1))
191+
->method('create')
192+
->with($secondCall['expected'])
193+
->willReturn($imageMock);
194+
195+
$this->model->setProduct($productMock);
196+
$this->model->setImageId($imageId);
197+
$this->model->setAttributes($firstCall['data']['custom_attributes']);
198+
199+
$this->assertInstanceOf(\Magento\Catalog\Block\Product\Image::class, $this->model->create());
200+
201+
$this->model->setProduct($productMock);
202+
$this->model->setImageId($imageId);
203+
$this->model->setAttributes($secondCall['data']['custom_attributes']);
204+
$this->assertInstanceOf(\Magento\Catalog\Block\Product\Image::class, $this->model->create());
205+
}
206+
134207
/**
135208
* @return array
136209
*/
@@ -154,7 +227,7 @@ public function createDataProvider()
154227
'width' => 100,
155228
'height' => 100,
156229
'label' => 'test_label',
157-
'ratio' => 1,
230+
'ratio' => 1,
158231
'custom_attributes' => '',
159232
'resized_image_width' => 100,
160233
'resized_image_height' => 100,
@@ -181,7 +254,7 @@ public function createDataProvider()
181254
'width' => 100,
182255
'height' => 50,
183256
'label' => 'test_label_2',
184-
'ratio' => 0.5,
257+
'ratio' => 0.5,
185258
'custom_attributes' => 'name_1="value_1" name_2="value_2"',
186259
'resized_image_width' => 120,
187260
'resized_image_height' => 70,
@@ -190,4 +263,123 @@ public function createDataProvider()
190263
],
191264
];
192265
}
266+
267+
/**
268+
* @return array
269+
*/
270+
public function createMultipleCallsDataProvider()
271+
{
272+
return [
273+
[
274+
[
275+
'without_attributes' => [
276+
'data' => [
277+
'frame' => 0,
278+
'url' => 'test_url_1',
279+
'width' => 100,
280+
'height' => 100,
281+
'label' => 'test_label',
282+
'custom_attributes' => [],
283+
'imagesize' => [100, 100],
284+
],
285+
'expected' => [
286+
'data' => [
287+
'template' => 'Magento_Catalog::product/image_with_borders.phtml',
288+
'image_url' => 'test_url_1',
289+
'width' => 100,
290+
'height' => 100,
291+
'label' => 'test_label',
292+
'ratio' => 1,
293+
'custom_attributes' => '',
294+
'resized_image_width' => 100,
295+
'resized_image_height' => 100,
296+
],
297+
],
298+
],
299+
'with_attributes' => [
300+
'data' => [
301+
'frame' => 1,
302+
'url' => 'test_url_2',
303+
'width' => 100,
304+
'height' => 50,
305+
'label' => 'test_label_2',
306+
'custom_attributes' => [
307+
'name_1' => 'value_1',
308+
'name_2' => 'value_2',
309+
],
310+
'imagesize' => [120, 70],
311+
],
312+
'expected' => [
313+
'data' => [
314+
'template' => 'Magento_Catalog::product/image.phtml',
315+
'image_url' => 'test_url_2',
316+
'width' => 100,
317+
'height' => 50,
318+
'label' => 'test_label_2',
319+
'ratio' => 0.5,
320+
'custom_attributes' => 'name_1="value_1" name_2="value_2"',
321+
'resized_image_width' => 120,
322+
'resized_image_height' => 70,
323+
],
324+
],
325+
],
326+
],
327+
],
328+
[
329+
[
330+
'with_attributes' => [
331+
'data' => [
332+
'frame' => 1,
333+
'url' => 'test_url_2',
334+
'width' => 100,
335+
'height' => 50,
336+
'label' => 'test_label_2',
337+
'custom_attributes' => [
338+
'name_1' => 'value_1',
339+
'name_2' => 'value_2',
340+
],
341+
'imagesize' => [120, 70],
342+
],
343+
'expected' => [
344+
'data' => [
345+
'template' => 'Magento_Catalog::product/image.phtml',
346+
'image_url' => 'test_url_2',
347+
'width' => 100,
348+
'height' => 50,
349+
'label' => 'test_label_2',
350+
'ratio' => 0.5,
351+
'custom_attributes' => 'name_1="value_1" name_2="value_2"',
352+
'resized_image_width' => 120,
353+
'resized_image_height' => 70,
354+
],
355+
],
356+
],
357+
'without_attributes' => [
358+
'data' => [
359+
'frame' => 0,
360+
'url' => 'test_url_1',
361+
'width' => 100,
362+
'height' => 100,
363+
'label' => 'test_label',
364+
'custom_attributes' => [],
365+
'imagesize' => [100, 100],
366+
],
367+
'expected' => [
368+
'data' => [
369+
'template' => 'Magento_Catalog::product/image_with_borders.phtml',
370+
'image_url' => 'test_url_1',
371+
'width' => 100,
372+
'height' => 100,
373+
'label' => 'test_label',
374+
'ratio' => 1,
375+
'custom_attributes' => '',
376+
'resized_image_width' => 100,
377+
'resized_image_height' => 100,
378+
],
379+
],
380+
],
381+
],
382+
],
383+
];
384+
}
193385
}

0 commit comments

Comments
 (0)