55 */
66namespace Magento \Swatches \Test \Unit \Block \Product \Renderer \Listing ;
77
8- use Magento \Swatches \Block \Product \Renderer \Configurable ;
8+ use \Magento \Swatches \Block \Product \Renderer \Listing \Configurable ;
9+ use Magento \Swatches \Model \SwatchAttributesProvider ;
910
1011/**
1112 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13+ * @SuppressWarnings(PHPMD.TooManyFields)
1214 */
1315class ConfigurableTest extends \PHPUnit_Framework_TestCase
1416{
@@ -57,33 +59,129 @@ class ConfigurableTest extends \PHPUnit_Framework_TestCase
5759 /** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
5860 private $ urlBuilder ;
5961
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+ */
6089 public function setUp ()
6190 {
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+ );
70113 $ 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,
72123 [],
73124 [],
74125 '' ,
75126 false
76127 );
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 );
82180
83- $ objectManagerHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
84181 $ this ->configurable = $ objectManagerHelper ->getObject (
85- ' \Magento\Swatches\Block\Product\Renderer\Listing\Configurable ' ,
182+ \Magento \Swatches \Block \Product \Renderer \Listing \Configurable::class ,
86183 [
184+ 'context ' => $ this ->contextMock ,
87185 'scopeConfig ' => $ this ->scopeConfig ,
88186 'imageHelper ' => $ this ->imageHelper ,
89187 'urlBuilder ' => $ this ->urlBuilder ,
@@ -96,9 +194,12 @@ public function setUp()
96194 'currentCustomer ' => $ this ->currentCustomer ,
97195 'priceCurrency ' => $ this ->priceCurrency ,
98196 'configurableAttributeData ' => $ this ->configurableAttributeData ,
197+ 'swatchAttributesProvider ' => $ this ->swatchAttributesProvider ,
99198 'data ' => [],
100199 ]
101200 );
201+
202+ $ objectManagerHelper ->setBackwardCompatibleProperty ($ this ->configurable , 'directory ' , $ this ->directory );
102203 }
103204
104205 /**
@@ -176,32 +277,42 @@ public function testGetJsonSwatchUsedInProductListing()
176277 $ this ->configurable ->getJsonSwatchConfig ();
177278 }
178279
280+ /**
281+ * @return void
282+ */
179283 private function prepareGetJsonSwatchConfig ()
180284 {
181- $ product1 = $ this ->getMock (' \Magento\Catalog\Model\Product ' , [], [], '' , false );
285+ $ product1 = $ this ->getMock (\Magento \Catalog \Model \Product::class , [], [], '' , false );
182286 $ product1 ->expects ($ this ->any ())->method ('getData ' )->with ('code ' )->willReturn (1 );
183287
184- $ product2 = $ this ->getMock (' \Magento\Catalog\Model\Product ' , [], [], '' , false );
288+ $ product2 = $ this ->getMock (\Magento \Catalog \Model \Product::class , [], [], '' , false );
185289 $ product2 ->expects ($ this ->any ())->method ('getData ' )->with ('code ' )->willReturn (3 );
186290
187291 $ simpleProducts = [$ product1 , $ product2 ];
188292 $ configurableType = $ this ->getMock (
189- ' \Magento\ConfigurableProduct\Model\Product\Type\Configurable ' ,
293+ \Magento \ConfigurableProduct \Model \Product \Type \Configurable::class ,
190294 [],
191295 [],
192296 '' ,
193297 false
194298 );
195- $ configurableType ->expects ($ this ->atLeastOnce ())->method ('getSalableUsedProducts ' )->with ($ this ->product , null )
299+ $ configurableType ->expects ($ this ->atLeastOnce ())->method ('getSalableUsedProducts ' )
300+ ->with ($ this ->product , null )
196301 ->willReturn ($ simpleProducts );
197302 $ this ->product ->expects ($ this ->any ())->method ('getTypeInstance ' )->willReturn ($ configurableType );
198303
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+ );
200311 $ productAttribute1 ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
201312 $ productAttribute1 ->expects ($ this ->any ())->method ('getAttributeCode ' )->willReturn ('code ' );
202313
203314 $ attribute1 = $ this ->getMock (
204- ' \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute ' ,
315+ \Magento \ConfigurableProduct \Model \Product \Type \Configurable \Attribute::class ,
205316 ['getProductAttribute ' ],
206317 [],
207318 '' ,
@@ -212,4 +323,72 @@ private function prepareGetJsonSwatchConfig()
212323 $ this ->helper ->expects ($ this ->any ())->method ('getAllowAttributes ' )->with ($ this ->product )
213324 ->willReturn ([$ attribute1 ]);
214325 }
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+ }
215394}
0 commit comments