55 */
66namespace 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+
813class 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 {
27- $ this ->helperFactory = $ this ->getMockBuilder (\Magento \Catalog \Helper \ImageFactory::class)
28- ->disableOriginalConstructor ()
29- ->setMethods (['create ' ])
30- ->getMock ();
31-
32- $ this ->imageFactory = $ this ->getMockBuilder (\Magento \Catalog \Block \Product \ImageFactory::class)
33- ->disableOriginalConstructor ()
34- ->setMethods (['create ' ])
35- ->getMock ();
36-
37- $ this ->model = new \Magento \Catalog \Block \Product \ImageBuilder (
38- $ this ->helperFactory ,
39- $ this ->imageFactory
40- );
32+ $ this ->helperFactory = $ this ->createPartialMock (\Magento \Catalog \Helper \ImageFactory::class, ['create ' ]);
33+
34+ $ this ->imageFactory = $ this ->createPartialMock (ImageFactory::class, ['create ' ]);
35+
36+ $ this ->model = new ImageBuilder ($ this ->helperFactory , $ this ->imageFactory );
4137 }
4238
4339 public function testSetProduct ()
4440 {
45- $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
46- ->disableOriginalConstructor ()
47- ->getMock ();
41+ $ productMock = $ this ->createMock (Product::class);
4842
4943 $ this ->assertInstanceOf (
50- \ Magento \ Catalog \ Block \ Product \ ImageBuilder::class,
44+ ImageBuilder::class,
5145 $ this ->model ->setProduct ($ productMock )
5246 );
5347 }
@@ -57,7 +51,7 @@ public function testSetImageId()
5751 $ imageId = 'test_image_id ' ;
5852
5953 $ this ->assertInstanceOf (
60- \ Magento \ Catalog \ Block \ Product \ ImageBuilder::class,
54+ ImageBuilder::class,
6155 $ this ->model ->setImageId ($ imageId )
6256 );
6357 }
@@ -68,7 +62,7 @@ public function testSetAttributes()
6862 'name ' => 'value ' ,
6963 ];
7064 $ this ->assertInstanceOf (
71- \ Magento \ Catalog \ Block \ Product \ ImageBuilder::class,
65+ ImageBuilder::class,
7266 $ this ->model ->setAttributes ($ attributes )
7367 );
7468 }
@@ -81,13 +75,9 @@ public function testCreate($data, $expected)
8175 {
8276 $ imageId = 'test_image_id ' ;
8377
84- $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
85- ->disableOriginalConstructor ()
86- ->getMock ();
78+ $ productMock = $ this ->createMock (Product::class);
8779
88- $ helperMock = $ this ->getMockBuilder (\Magento \Catalog \Helper \Image::class)
89- ->disableOriginalConstructor ()
90- ->getMock ();
80+ $ helperMock = $ this ->createMock (Image::class);
9181 $ helperMock ->expects ($ this ->once ())
9282 ->method ('init ' )
9383 ->with ($ productMock , $ imageId )
@@ -116,9 +106,7 @@ public function testCreate($data, $expected)
116106 ->method ('create ' )
117107 ->willReturn ($ helperMock );
118108
119- $ imageMock = $ this ->getMockBuilder (\Magento \Catalog \Block \Product \Image::class)
120- ->disableOriginalConstructor ()
121- ->getMock ();
109+ $ imageMock = $ this ->createMock (\Magento \Catalog \Block \Product \Image::class);
122110
123111 $ this ->imageFactory ->expects ($ this ->once ())
124112 ->method ('create ' )
@@ -131,61 +119,173 @@ public function testCreate($data, $expected)
131119 $ this ->assertInstanceOf (\Magento \Catalog \Block \Product \Image::class, $ this ->model ->create ());
132120 }
133121
122+ /**
123+ * Check if custom attributes will be overridden when builder used few times
124+ * @param array $data
125+ * @dataProvider createMultipleCallsDataProvider
126+ */
127+ public function testCreateMultipleCalls ($ data )
128+ {
129+ list ($ firstCall , $ secondCall ) = array_values ($ data );
130+
131+ $ imageId = 'test_image_id ' ;
132+
133+ $ productMock = $ this ->createMock (Product::class);
134+
135+ $ helperMock = $ this ->createMock (Image::class);
136+ $ helperMock ->expects ($ this ->exactly (2 ))
137+ ->method ('init ' )
138+ ->with ($ productMock , $ imageId )
139+ ->willReturnSelf ();
140+
141+ $ helperMock ->expects ($ this ->exactly (2 ))
142+ ->method ('getFrame ' )
143+ ->willReturnOnConsecutiveCalls ($ firstCall ['data ' ]['frame ' ], $ secondCall ['data ' ]['frame ' ]);
144+ $ helperMock ->expects ($ this ->exactly (2 ))
145+ ->method ('getUrl ' )
146+ ->willReturnOnConsecutiveCalls ($ firstCall ['data ' ]['url ' ], $ secondCall ['data ' ]['url ' ]);
147+ $ helperMock ->expects ($ this ->exactly (4 ))
148+ ->method ('getWidth ' )
149+ ->willReturnOnConsecutiveCalls (
150+ $ firstCall ['data ' ]['width ' ],
151+ $ firstCall ['data ' ]['width ' ],
152+ $ secondCall ['data ' ]['width ' ],
153+ $ secondCall ['data ' ]['width ' ]
154+ );
155+ $ helperMock ->expects ($ this ->exactly (4 ))
156+ ->method ('getHeight ' )
157+ ->willReturnOnConsecutiveCalls (
158+ $ firstCall ['data ' ]['height ' ],
159+ $ firstCall ['data ' ]['height ' ],
160+ $ secondCall ['data ' ]['height ' ],
161+ $ secondCall ['data ' ]['height ' ]
162+ );
163+ $ helperMock ->expects ($ this ->exactly (2 ))
164+ ->method ('getLabel ' )
165+ ->willReturnOnConsecutiveCalls ($ firstCall ['data ' ]['label ' ], $ secondCall ['data ' ]['label ' ]);
166+ $ helperMock ->expects ($ this ->exactly (2 ))
167+ ->method ('getResizedImageInfo ' )
168+ ->willReturnOnConsecutiveCalls ($ firstCall ['data ' ]['imagesize ' ], $ secondCall ['data ' ]['imagesize ' ]);
169+ $ this ->helperFactory ->expects ($ this ->exactly (2 ))
170+ ->method ('create ' )
171+ ->willReturn ($ helperMock );
172+
173+ $ imageMock = $ this ->createMock (\Magento \Catalog \Block \Product \Image::class);
174+
175+ $ this ->imageFactory ->expects ($ this ->at (0 ))
176+ ->method ('create ' )
177+ ->with ($ firstCall ['expected ' ])
178+ ->willReturn ($ imageMock );
179+
180+ $ this ->imageFactory ->expects ($ this ->at (1 ))
181+ ->method ('create ' )
182+ ->with ($ secondCall ['expected ' ])
183+ ->willReturn ($ imageMock );
184+
185+ $ this ->model ->setProduct ($ productMock );
186+ $ this ->model ->setImageId ($ imageId );
187+ $ this ->model ->setAttributes ($ firstCall ['data ' ]['custom_attributes ' ]);
188+
189+ $ this ->assertInstanceOf (\Magento \Catalog \Block \Product \Image::class, $ this ->model ->create ());
190+
191+ $ this ->model ->setProduct ($ productMock );
192+ $ this ->model ->setImageId ($ imageId );
193+ $ this ->model ->setAttributes ($ secondCall ['data ' ]['custom_attributes ' ]);
194+ $ this ->assertInstanceOf (\Magento \Catalog \Block \Product \Image::class, $ this ->model ->create ());
195+ }
196+
197+ /**
198+ * @return array
199+ */
200+ public function createDataProvider (): array
201+ {
202+ return [
203+ $ this ->getTestDataWithoutAttributes (),
204+ $ this ->getTestDataWithAttributes (),
205+ ];
206+ }
207+
134208 /**
135209 * @return array
136210 */
137- public function createDataProvider ()
211+ public function createMultipleCallsDataProvider (): array
138212 {
139213 return [
140214 [
215+ [
216+ 'without_attributes ' => $ this ->getTestDataWithoutAttributes (),
217+ 'with_attributes ' => $ this ->getTestDataWithAttributes (),
218+ ],
219+ ],
220+ [
221+ [
222+ 'with_attributes ' => $ this ->getTestDataWithAttributes (),
223+ 'without_attributes ' => $ this ->getTestDataWithoutAttributes (),
224+ ],
225+ ],
226+ ];
227+ }
228+
229+ /**
230+ * @return array
231+ */
232+ private function getTestDataWithoutAttributes (): array
233+ {
234+ return [
235+ 'data ' => [
236+ 'frame ' => 0 ,
237+ 'url ' => 'test_url_1 ' ,
238+ 'width ' => 100 ,
239+ 'height ' => 100 ,
240+ 'label ' => 'test_label ' ,
241+ 'custom_attributes ' => [],
242+ 'imagesize ' => [100 , 100 ],
243+ ],
244+ 'expected ' => [
141245 'data ' => [
142- 'frame ' => 0 ,
143- 'url ' => 'test_url_1 ' ,
246+ 'template ' => ' Magento_Catalog::product/image_with_borders.phtml ' ,
247+ 'image_url ' => 'test_url_1 ' ,
144248 'width ' => 100 ,
145249 'height ' => 100 ,
146250 'label ' => 'test_label ' ,
147- 'custom_attributes ' => [],
148- 'imagesize ' => [100 , 100 ],
251+ 'ratio ' => 1 ,
252+ 'custom_attributes ' => '' ,
253+ 'resized_image_width ' => 100 ,
254+ 'resized_image_height ' => 100 ,
149255 ],
150- 'expected ' => [
151- 'data ' => [
152- 'template ' => 'Magento_Catalog::product/image_with_borders.phtml ' ,
153- 'image_url ' => 'test_url_1 ' ,
154- 'width ' => 100 ,
155- 'height ' => 100 ,
156- 'label ' => 'test_label ' ,
157- 'ratio ' => 1 ,
158- 'custom_attributes ' => '' ,
159- 'resized_image_width ' => 100 ,
160- 'resized_image_height ' => 100 ,
161- ],
256+ ],
257+ ];
258+ }
259+
260+ /**
261+ * @return array
262+ */
263+ private function getTestDataWithAttributes (): array
264+ {
265+ return [
266+ 'data ' => [
267+ 'frame ' => 1 ,
268+ 'url ' => 'test_url_2 ' ,
269+ 'width ' => 100 ,
270+ 'height ' => 50 ,
271+ 'label ' => 'test_label_2 ' ,
272+ 'custom_attributes ' => [
273+ 'name_1 ' => 'value_1 ' ,
274+ 'name_2 ' => 'value_2 ' ,
162275 ],
276+ 'imagesize ' => [120 , 70 ],
163277 ],
164- [
278+ ' expected ' => [
165279 'data ' => [
166- 'frame ' => 1 ,
167- 'url ' => 'test_url_2 ' ,
280+ 'template ' => ' Magento_Catalog::product/image.phtml ' ,
281+ 'image_url ' => 'test_url_2 ' ,
168282 'width ' => 100 ,
169283 'height ' => 50 ,
170284 'label ' => 'test_label_2 ' ,
171- 'custom_attributes ' => [
172- 'name_1 ' => 'value_1 ' ,
173- 'name_2 ' => 'value_2 ' ,
174- ],
175- 'imagesize ' => [120 , 70 ],
176- ],
177- 'expected ' => [
178- 'data ' => [
179- 'template ' => 'Magento_Catalog::product/image.phtml ' ,
180- 'image_url ' => 'test_url_2 ' ,
181- 'width ' => 100 ,
182- 'height ' => 50 ,
183- 'label ' => 'test_label_2 ' ,
184- 'ratio ' => 0.5 ,
185- 'custom_attributes ' => 'name_1="value_1" name_2="value_2" ' ,
186- 'resized_image_width ' => 120 ,
187- 'resized_image_height ' => 70 ,
188- ],
285+ 'ratio ' => 0.5 ,
286+ 'custom_attributes ' => 'name_1="value_1" name_2="value_2" ' ,
287+ 'resized_image_width ' => 120 ,
288+ 'resized_image_height ' => 70 ,
189289 ],
190290 ],
191291 ];
0 commit comments