5
5
*/
6
6
namespace Magento \Catalog \Test \Unit \Block \Product ;
7
7
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
+
8
13
class ImageBuilderTest extends \PHPUnit \Framework \TestCase
9
14
{
10
15
/**
11
- * @var \Magento\Catalog\Block\Product\ ImageBuilder
16
+ * @var ImageBuilder
12
17
*/
13
- protected $ model ;
18
+ private $ model ;
14
19
15
20
/**
16
21
* @var \Magento\Catalog\Helper\ImageFactory|\PHPUnit_Framework_MockObject_MockObject
17
22
*/
18
- protected $ helperFactory ;
23
+ private $ helperFactory ;
19
24
20
25
/**
21
- * @var \Magento\Catalog\Block\Product\ ImageFactory|\PHPUnit_Framework_MockObject_MockObject
26
+ * @var ImageFactory|\PHPUnit_Framework_MockObject_MockObject
22
27
*/
23
- protected $ imageFactory ;
28
+ private $ imageFactory ;
24
29
25
30
protected function setUp ()
26
31
{
@@ -29,25 +34,22 @@ protected function setUp()
29
34
->setMethods (['create ' ])
30
35
->getMock ();
31
36
32
- $ this ->imageFactory = $ this ->getMockBuilder (\ Magento \ Catalog \ Block \ Product \ ImageFactory::class)
37
+ $ this ->imageFactory = $ this ->getMockBuilder (ImageFactory::class)
33
38
->disableOriginalConstructor ()
34
39
->setMethods (['create ' ])
35
40
->getMock ();
36
41
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 );
41
43
}
42
44
43
45
public function testSetProduct ()
44
46
{
45
- $ productMock = $ this ->getMockBuilder (\ Magento \ Catalog \ Model \ Product::class)
47
+ $ productMock = $ this ->getMockBuilder (Product::class)
46
48
->disableOriginalConstructor ()
47
49
->getMock ();
48
50
49
51
$ this ->assertInstanceOf (
50
- \ Magento \ Catalog \ Block \ Product \ ImageBuilder::class,
52
+ ImageBuilder::class,
51
53
$ this ->model ->setProduct ($ productMock )
52
54
);
53
55
}
@@ -57,7 +59,7 @@ public function testSetImageId()
57
59
$ imageId = 'test_image_id ' ;
58
60
59
61
$ this ->assertInstanceOf (
60
- \ Magento \ Catalog \ Block \ Product \ ImageBuilder::class,
62
+ ImageBuilder::class,
61
63
$ this ->model ->setImageId ($ imageId )
62
64
);
63
65
}
@@ -68,7 +70,7 @@ public function testSetAttributes()
68
70
'name ' => 'value ' ,
69
71
];
70
72
$ this ->assertInstanceOf (
71
- \ Magento \ Catalog \ Block \ Product \ ImageBuilder::class,
73
+ ImageBuilder::class,
72
74
$ this ->model ->setAttributes ($ attributes )
73
75
);
74
76
}
@@ -81,11 +83,11 @@ public function testCreate($data, $expected)
81
83
{
82
84
$ imageId = 'test_image_id ' ;
83
85
84
- $ productMock = $ this ->getMockBuilder (\ Magento \ Catalog \ Model \ Product::class)
86
+ $ productMock = $ this ->getMockBuilder (Product::class)
85
87
->disableOriginalConstructor ()
86
88
->getMock ();
87
89
88
- $ helperMock = $ this ->getMockBuilder (\ Magento \ Catalog \ Helper \ Image::class)
90
+ $ helperMock = $ this ->getMockBuilder (Image::class)
89
91
->disableOriginalConstructor ()
90
92
->getMock ();
91
93
$ helperMock ->expects ($ this ->once ())
@@ -131,6 +133,77 @@ public function testCreate($data, $expected)
131
133
$ this ->assertInstanceOf (\Magento \Catalog \Block \Product \Image::class, $ this ->model ->create ());
132
134
}
133
135
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
+
134
207
/**
135
208
* @return array
136
209
*/
@@ -154,7 +227,7 @@ public function createDataProvider()
154
227
'width ' => 100 ,
155
228
'height ' => 100 ,
156
229
'label ' => 'test_label ' ,
157
- 'ratio ' => 1 ,
230
+ 'ratio ' => 1 ,
158
231
'custom_attributes ' => '' ,
159
232
'resized_image_width ' => 100 ,
160
233
'resized_image_height ' => 100 ,
@@ -181,7 +254,7 @@ public function createDataProvider()
181
254
'width ' => 100 ,
182
255
'height ' => 50 ,
183
256
'label ' => 'test_label_2 ' ,
184
- 'ratio ' => 0.5 ,
257
+ 'ratio ' => 0.5 ,
185
258
'custom_attributes ' => 'name_1="value_1" name_2="value_2" ' ,
186
259
'resized_image_width ' => 120 ,
187
260
'resized_image_height ' => 70 ,
@@ -190,4 +263,123 @@ public function createDataProvider()
190
263
],
191
264
];
192
265
}
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
+ }
193
385
}
0 commit comments