5
5
*/
6
6
namespace Magento \Swatches \Test \Unit \Block \Product \Renderer \Listing ;
7
7
8
- use Magento \Swatches \Block \Product \Renderer \Configurable ;
8
+ use \Magento \Swatches \Block \Product \Renderer \Listing \Configurable ;
9
+ use Magento \Swatches \Model \SwatchAttributesProvider ;
9
10
10
11
/**
11
12
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13
+ * @SuppressWarnings(PHPMD.TooManyFields)
12
14
*/
13
15
class ConfigurableTest extends \PHPUnit_Framework_TestCase
14
16
{
@@ -57,33 +59,129 @@ class ConfigurableTest extends \PHPUnit_Framework_TestCase
57
59
/** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
58
60
private $ urlBuilder ;
59
61
62
+ /** @var SwatchAttributesProvider|\PHPUnit_Framework_MockObject_MockObject */
63
+ private $ swatchAttributesProvider ;
64
+
65
+ /** @var \Magento\Catalog\Block\Product\Context|\PHPUnit_Framework_MockObject_MockObject */
66
+ private $ contextMock ;
67
+
68
+ /** @var \Magento\Framework\View\Element\Template\File\Resolver|\PHPUnit_Framework_MockObject_MockObject */
69
+ private $ resolver ;
70
+
71
+ /** @var \Magento\Framework\Event\Manager|\PHPUnit_Framework_MockObject_MockObject */
72
+ private $ eventManager ;
73
+
74
+ /** @var \Magento\Framework\App\Cache\StateInterface|\PHPUnit_Framework_MockObject_MockObject */
75
+ private $ cacheState ;
76
+
77
+ /** @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject */
78
+ private $ directory ;
79
+
80
+ /** @var \Magento\Framework\View\Element\Template\File\Validator|\PHPUnit_Framework_MockObject_MockObject */
81
+ private $ validator ;
82
+
83
+ /** @var \Magento\Framework\View\TemplateEnginePool|\PHPUnit_Framework_MockObject_MockObject */
84
+ private $ templateEnginePool ;
85
+
86
+ /**
87
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
88
+ */
60
89
public function setUp ()
61
90
{
62
- $ this ->arrayUtils = $ this ->getMock ('\Magento\Framework\Stdlib\ArrayUtils ' , [], [], '' , false );
63
- $ this ->jsonEncoder = $ this ->getMock ('\Magento\Framework\Json\EncoderInterface ' , [], [], '' , false );
64
- $ this ->helper = $ this ->getMock ('\Magento\ConfigurableProduct\Helper\Data ' , [], [], '' , false );
65
- $ this ->swatchHelper = $ this ->getMock ('\Magento\Swatches\Helper\Data ' , [], [], '' , false );
66
- $ this ->swatchMediaHelper = $ this ->getMock ('\Magento\Swatches\Helper\Media ' , [], [], '' , false );
67
- $ this ->catalogProduct = $ this ->getMock ('\Magento\Catalog\Helper\Product ' , [], [], '' , false );
68
- $ this ->currentCustomer = $ this ->getMock ('\Magento\Customer\Helper\Session\CurrentCustomer ' , [], [], '' , false );
69
- $ this ->priceCurrency = $ this ->getMock ('\Magento\Framework\Pricing\PriceCurrencyInterface ' , [], [], '' , false );
91
+ $ objectManagerHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
92
+
93
+ $ this ->arrayUtils = $ this ->getMock (\Magento \Framework \Stdlib \ArrayUtils::class, [], [], '' , false );
94
+ $ this ->jsonEncoder = $ this ->getMock (\Magento \Framework \Json \EncoderInterface::class, [], [], '' , false );
95
+ $ this ->helper = $ this ->getMock (\Magento \ConfigurableProduct \Helper \Data::class, [], [], '' , false );
96
+ $ this ->swatchHelper = $ this ->getMock (\Magento \Swatches \Helper \Data::class, [], [], '' , false );
97
+ $ this ->swatchMediaHelper = $ this ->getMock (\Magento \Swatches \Helper \Media::class, [], [], '' , false );
98
+ $ this ->catalogProduct = $ this ->getMock (\Magento \Catalog \Helper \Product::class, [], [], '' , false );
99
+ $ this ->currentCustomer = $ this ->getMock (
100
+ \Magento \Customer \Helper \Session \CurrentCustomer::class,
101
+ [],
102
+ [],
103
+ '' ,
104
+ false
105
+ );
106
+ $ this ->priceCurrency = $ this ->getMock (
107
+ \Magento \Framework \Pricing \PriceCurrencyInterface::class,
108
+ [],
109
+ [],
110
+ '' ,
111
+ false
112
+ );
70
113
$ this ->configurableAttributeData = $ this ->getMock (
71
- 'Magento\ConfigurableProduct\Model\ConfigurableAttributeData ' ,
114
+ \Magento \ConfigurableProduct \Model \ConfigurableAttributeData::class,
115
+ [],
116
+ [],
117
+ '' ,
118
+ false
119
+ );
120
+ $ this ->product = $ this ->getMock (\Magento \Catalog \Model \Product::class, [], [], '' , false );
121
+ $ this ->typeInstance = $ this ->getMock (
122
+ \Magento \Catalog \Model \Product \Type \AbstractType::class,
72
123
[],
73
124
[],
74
125
'' ,
75
126
false
76
127
);
77
- $ this ->product = $ this ->getMock ('\Magento\Catalog\Model\Product ' , [], [], '' , false );
78
- $ this ->typeInstance = $ this ->getMock ('\Magento\Catalog\Model\Product\Type\AbstractType ' , [], [], '' , false );
79
- $ this ->scopeConfig = $ this ->getMock ('\Magento\Framework\App\Config\ScopeConfigInterface ' , [], [], '' , false );
80
- $ this ->imageHelper = $ this ->getMock ('\Magento\Catalog\Helper\Image ' , [], [], '' , false );
81
- $ this ->urlBuilder = $ this ->getMock ('\Magento\Framework\UrlInterface ' );
128
+ $ this ->scopeConfig = $ this ->getMock (
129
+ \Magento \Framework \App \Config \ScopeConfigInterface::class,
130
+ [],
131
+ [],
132
+ '' ,
133
+ false
134
+ );
135
+ $ this ->imageHelper = $ this ->getMock (\Magento \Catalog \Helper \Image::class, [], [], '' , false );
136
+ $ this ->urlBuilder = $ this ->getMock (\Magento \Framework \UrlInterface::class);
137
+
138
+ $ this ->swatchAttributesProvider = self ::getMockBuilder (SwatchAttributesProvider::class)
139
+ ->setMethods (['provide ' ])
140
+ ->disableOriginalConstructor ()
141
+ ->getMock ();
142
+
143
+ $ this ->contextMock = self ::getMockBuilder (\Magento \Catalog \Block \Product \Context::class)
144
+ ->disableOriginalConstructor ()
145
+ ->getMock ();
146
+
147
+ $ this ->eventManager = self ::getMockBuilder (\Magento \Framework \Event \Manager::class)
148
+ ->disableOriginalConstructor ()
149
+ ->getMock ();
150
+
151
+ $ this ->resolver = self ::getMockBuilder (\Magento \Framework \View \Element \Template \File \Resolver::class)
152
+ ->setMethods (['getTemplateFileName ' ])
153
+ ->disableOriginalConstructor ()
154
+ ->getMock ();
155
+
156
+ $ this ->cacheState = self ::getMockBuilder (\Magento \Framework \App \Cache \StateInterface::class)
157
+ ->disableOriginalConstructor ()
158
+ ->getMockForAbstractClass ();
159
+
160
+ $ this ->directory = self ::getMockBuilder (\Magento \Framework \Filesystem \Directory \ReadInterface::class)
161
+ ->disableOriginalConstructor ()
162
+ ->getMockForAbstractClass ();
163
+
164
+ $ this ->validator = self ::getMockBuilder (\Magento \Framework \View \Element \Template \File \Validator::class)
165
+ ->disableOriginalConstructor ()
166
+ ->getMock ();
167
+
168
+ $ this ->templateEnginePool = self ::getMockBuilder (
169
+ \Magento \Framework \View \TemplateEnginePool::class
170
+ )
171
+ ->disableOriginalConstructor ()
172
+ ->getMock ();
173
+
174
+ $ this ->contextMock ->expects ($ this ->once ())->method ('getResolver ' )->willReturn ($ this ->resolver );
175
+ $ this ->contextMock ->expects ($ this ->once ())->method ('getEventManager ' )->willReturn ($ this ->eventManager );
176
+ $ this ->contextMock ->expects ($ this ->once ())->method ('getScopeConfig ' )->willReturn ($ this ->scopeConfig );
177
+ $ this ->contextMock ->expects ($ this ->once ())->method ('getCacheState ' )->willReturn ($ this ->cacheState );
178
+ $ this ->contextMock ->expects ($ this ->once ())->method ('getValidator ' )->willReturn ($ this ->validator );
179
+ $ this ->contextMock ->expects ($ this ->once ())->method ('getEnginePool ' )->willReturn ($ this ->templateEnginePool );
82
180
83
- $ objectManagerHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
84
181
$ this ->configurable = $ objectManagerHelper ->getObject (
85
- ' \Magento\Swatches\Block\Product\Renderer\Listing\Configurable ' ,
182
+ \Magento \Swatches \Block \Product \Renderer \Listing \Configurable::class ,
86
183
[
184
+ 'context ' => $ this ->contextMock ,
87
185
'scopeConfig ' => $ this ->scopeConfig ,
88
186
'imageHelper ' => $ this ->imageHelper ,
89
187
'urlBuilder ' => $ this ->urlBuilder ,
@@ -96,9 +194,12 @@ public function setUp()
96
194
'currentCustomer ' => $ this ->currentCustomer ,
97
195
'priceCurrency ' => $ this ->priceCurrency ,
98
196
'configurableAttributeData ' => $ this ->configurableAttributeData ,
197
+ 'swatchAttributesProvider ' => $ this ->swatchAttributesProvider ,
99
198
'data ' => [],
100
199
]
101
200
);
201
+
202
+ $ objectManagerHelper ->setBackwardCompatibleProperty ($ this ->configurable , 'directory ' , $ this ->directory );
102
203
}
103
204
104
205
/**
@@ -176,32 +277,42 @@ public function testGetJsonSwatchUsedInProductListing()
176
277
$ this ->configurable ->getJsonSwatchConfig ();
177
278
}
178
279
280
+ /**
281
+ * @return void
282
+ */
179
283
private function prepareGetJsonSwatchConfig ()
180
284
{
181
- $ product1 = $ this ->getMock (' \Magento\Catalog\Model\Product ' , [], [], '' , false );
285
+ $ product1 = $ this ->getMock (\Magento \Catalog \Model \Product::class , [], [], '' , false );
182
286
$ product1 ->expects ($ this ->any ())->method ('getData ' )->with ('code ' )->willReturn (1 );
183
287
184
- $ product2 = $ this ->getMock (' \Magento\Catalog\Model\Product ' , [], [], '' , false );
288
+ $ product2 = $ this ->getMock (\Magento \Catalog \Model \Product::class , [], [], '' , false );
185
289
$ product2 ->expects ($ this ->any ())->method ('getData ' )->with ('code ' )->willReturn (3 );
186
290
187
291
$ simpleProducts = [$ product1 , $ product2 ];
188
292
$ configurableType = $ this ->getMock (
189
- ' \Magento\ConfigurableProduct\Model\Product\Type\Configurable ' ,
293
+ \Magento \ConfigurableProduct \Model \Product \Type \Configurable::class ,
190
294
[],
191
295
[],
192
296
'' ,
193
297
false
194
298
);
195
- $ configurableType ->expects ($ this ->atLeastOnce ())->method ('getSalableUsedProducts ' )->with ($ this ->product , null )
299
+ $ configurableType ->expects ($ this ->atLeastOnce ())->method ('getSalableUsedProducts ' )
300
+ ->with ($ this ->product , null )
196
301
->willReturn ($ simpleProducts );
197
302
$ this ->product ->expects ($ this ->any ())->method ('getTypeInstance ' )->willReturn ($ configurableType );
198
303
199
- $ productAttribute1 = $ this ->getMock ('\Magento\Eav\Model\Entity\Attribute\AbstractAttribute ' , [], [], '' , false );
304
+ $ productAttribute1 = $ this ->getMock (
305
+ \Magento \Eav \Model \Entity \Attribute \AbstractAttribute::class,
306
+ [],
307
+ [],
308
+ '' ,
309
+ false
310
+ );
200
311
$ productAttribute1 ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
201
312
$ productAttribute1 ->expects ($ this ->any ())->method ('getAttributeCode ' )->willReturn ('code ' );
202
313
203
314
$ attribute1 = $ this ->getMock (
204
- ' \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute ' ,
315
+ \Magento \ConfigurableProduct \Model \Product \Type \Configurable \Attribute::class ,
205
316
['getProductAttribute ' ],
206
317
[],
207
318
'' ,
@@ -212,4 +323,72 @@ private function prepareGetJsonSwatchConfig()
212
323
$ this ->helper ->expects ($ this ->any ())->method ('getAllowAttributes ' )->with ($ this ->product )
213
324
->willReturn ([$ attribute1 ]);
214
325
}
326
+
327
+ /**
328
+ * @return void
329
+ */
330
+ public function testToHtmlNoSwatches ()
331
+ {
332
+ $ this ->swatchAttributesProvider ->expects (self ::atLeastOnce ())
333
+ ->method ('provide ' )
334
+ ->with ($ this ->product )
335
+ ->willReturn ([]);
336
+
337
+ $ this ->configurable ->setProduct ($ this ->product );
338
+
339
+ self ::assertEmpty ($ this ->configurable ->toHtml ());
340
+ }
341
+
342
+ /**
343
+ * @return void
344
+ */
345
+ public function testToHtmlSwatches ()
346
+ {
347
+ $ attribute = self ::getMockBuilder (
348
+ \Magento \ConfigurableProduct \Model \Product \Type \Configurable \Attribute::class
349
+ )
350
+ ->disableOriginalConstructor ()
351
+ ->getMock ();
352
+
353
+ $ this ->swatchAttributesProvider ->expects (self ::atLeastOnce ())
354
+ ->method ('provide ' )
355
+ ->with ($ this ->product )
356
+ ->willReturn ([$ attribute ]);
357
+
358
+ $ engine = self ::getMockBuilder (\Magento \Framework \View \TemplateEngineInterface::class)
359
+ ->getMockForAbstractClass ();
360
+
361
+ $ engine ->expects (self ::atLeastOnce ())
362
+ ->method ('render ' )
363
+ ->with ($ this ->configurable , 'product/listing/renderer.phtml ' )
364
+ ->willReturn ('<li>Swatches listing</li> ' );
365
+
366
+ $ this ->templateEnginePool ->expects (self ::atLeastOnce ())
367
+ ->method ('get ' )
368
+ ->withAnyParameters ()
369
+ ->willReturn ($ engine );
370
+
371
+ $ this ->configurable ->setProduct ($ this ->product );
372
+ $ this ->configurable ->setTemplate ('product/listing/renderer.phtml ' );
373
+ $ this ->configurable ->setArea ('frontend ' );
374
+
375
+ $ this ->resolver ->expects (self ::atLeastOnce ())
376
+ ->method ('getTemplateFileName ' )
377
+ ->willReturn ('product/listing/renderer.phtml ' );
378
+
379
+ $ this ->directory ->expects (self ::atLeastOnce ())
380
+ ->method ('getRelativePath ' )
381
+ ->with ('product/listing/renderer.phtml ' )
382
+ ->willReturn ('product/listing/renderer.phtml ' );
383
+
384
+ $ this ->validator ->expects (self ::atLeastOnce ())
385
+ ->method ('isValid ' )
386
+ ->with ('product/listing/renderer.phtml ' )
387
+ ->willReturn (true );
388
+
389
+ $ html = $ this ->configurable ->toHtml ();
390
+
391
+ self ::assertNotEmpty ($ html );
392
+ self ::assertEquals ('<li>Swatches listing</li> ' , $ html );
393
+ }
215
394
}
0 commit comments