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
{
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 );
41
37
}
42
38
43
39
public function testSetProduct ()
44
40
{
45
- $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
46
- ->disableOriginalConstructor ()
47
- ->getMock ();
41
+ $ productMock = $ this ->createMock (Product::class);
48
42
49
43
$ this ->assertInstanceOf (
50
- \ Magento \ Catalog \ Block \ Product \ ImageBuilder::class,
44
+ ImageBuilder::class,
51
45
$ this ->model ->setProduct ($ productMock )
52
46
);
53
47
}
@@ -57,7 +51,7 @@ public function testSetImageId()
57
51
$ imageId = 'test_image_id ' ;
58
52
59
53
$ this ->assertInstanceOf (
60
- \ Magento \ Catalog \ Block \ Product \ ImageBuilder::class,
54
+ ImageBuilder::class,
61
55
$ this ->model ->setImageId ($ imageId )
62
56
);
63
57
}
@@ -68,7 +62,7 @@ public function testSetAttributes()
68
62
'name ' => 'value ' ,
69
63
];
70
64
$ this ->assertInstanceOf (
71
- \ Magento \ Catalog \ Block \ Product \ ImageBuilder::class,
65
+ ImageBuilder::class,
72
66
$ this ->model ->setAttributes ($ attributes )
73
67
);
74
68
}
@@ -81,13 +75,9 @@ public function testCreate($data, $expected)
81
75
{
82
76
$ imageId = 'test_image_id ' ;
83
77
84
- $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
85
- ->disableOriginalConstructor ()
86
- ->getMock ();
78
+ $ productMock = $ this ->createMock (Product::class);
87
79
88
- $ helperMock = $ this ->getMockBuilder (\Magento \Catalog \Helper \Image::class)
89
- ->disableOriginalConstructor ()
90
- ->getMock ();
80
+ $ helperMock = $ this ->createMock (Image::class);
91
81
$ helperMock ->expects ($ this ->once ())
92
82
->method ('init ' )
93
83
->with ($ productMock , $ imageId )
@@ -116,9 +106,7 @@ public function testCreate($data, $expected)
116
106
->method ('create ' )
117
107
->willReturn ($ helperMock );
118
108
119
- $ imageMock = $ this ->getMockBuilder (\Magento \Catalog \Block \Product \Image::class)
120
- ->disableOriginalConstructor ()
121
- ->getMock ();
109
+ $ imageMock = $ this ->createMock (\Magento \Catalog \Block \Product \Image::class);
122
110
123
111
$ this ->imageFactory ->expects ($ this ->once ())
124
112
->method ('create ' )
@@ -131,61 +119,173 @@ public function testCreate($data, $expected)
131
119
$ this ->assertInstanceOf (\Magento \Catalog \Block \Product \Image::class, $ this ->model ->create ());
132
120
}
133
121
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
+
134
208
/**
135
209
* @return array
136
210
*/
137
- public function createDataProvider ()
211
+ public function createMultipleCallsDataProvider (): array
138
212
{
139
213
return [
140
214
[
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 ' => [
141
245
'data ' => [
142
- 'frame ' => 0 ,
143
- 'url ' => 'test_url_1 ' ,
246
+ 'template ' => ' Magento_Catalog::product/image_with_borders.phtml ' ,
247
+ 'image_url ' => 'test_url_1 ' ,
144
248
'width ' => 100 ,
145
249
'height ' => 100 ,
146
250
'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 ,
149
255
],
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 ' ,
162
275
],
276
+ 'imagesize ' => [120 , 70 ],
163
277
],
164
- [
278
+ ' expected ' => [
165
279
'data ' => [
166
- 'frame ' => 1 ,
167
- 'url ' => 'test_url_2 ' ,
280
+ 'template ' => ' Magento_Catalog::product/image.phtml ' ,
281
+ 'image_url ' => 'test_url_2 ' ,
168
282
'width ' => 100 ,
169
283
'height ' => 50 ,
170
284
'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 ,
189
289
],
190
290
],
191
291
];
0 commit comments