77
88namespace Magento \Swatches \Test \Unit \Block \Product \Renderer \Listing ;
99
10+ use Magento \Catalog \Block \Product \Context ;
1011use Magento \Catalog \Helper \Image ;
1112use Magento \Catalog \Helper \Product ;
1213use Magento \Catalog \Model \Product \Attribute \Source \Status ;
1718use Magento \ConfigurableProduct \Model \Product \Type \Configurable \Attribute ;
1819use Magento \ConfigurableProduct \Model \Product \Type \Configurable \Variations \Prices ;
1920use Magento \Customer \Helper \Session \CurrentCustomer ;
21+ use Magento \Eav \Api \Data \AttributeInterface ;
2022use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
2123use Magento \Framework \App \Config \ScopeConfigInterface ;
24+ use Magento \Framework \App \Request \Http ;
25+ use Magento \Framework \App \RequestInterface ;
2226use Magento \Framework \Json \EncoderInterface ;
27+ use Magento \Framework \Model \AbstractModel ;
2328use Magento \Framework \Pricing \PriceCurrencyInterface ;
2429use Magento \Framework \Pricing \PriceInfo \Base ;
2530use Magento \Framework \Stdlib \ArrayUtils ;
2631use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
32+ use Magento \Store \Model \StoreManagerInterface ;
2733use Magento \Swatches \Block \Product \Renderer \Configurable ;
2834use Magento \Swatches \Block \Product \Renderer \Listing \Configurable as ConfigurableRenderer ;
2935use Magento \Swatches \Helper \Media ;
@@ -84,6 +90,11 @@ class ConfigurableTest extends TestCase
8490 /** @var MockObject */
8591 private $ variationPricesMock ;
8692
93+ /**
94+ * @var RequestInterface|MockObject
95+ */
96+ private $ request ;
97+
8798 protected function setUp (): void
8899 {
89100 $ this ->arrayUtils = $ this ->createMock (ArrayUtils::class);
@@ -105,11 +116,22 @@ protected function setUp(): void
105116 $ this ->variationPricesMock = $ this ->createMock (
106117 Prices::class
107118 );
119+ $ customerSession = $ this ->createMock (\Magento \Customer \Model \Session::class);
120+ $ this ->request = $ this ->getMockBuilder (Http::class)
121+ ->addMethods (['toArray ' ])
122+ ->onlyMethods (['getQuery ' ])
123+ ->disableOriginalConstructor ()
124+ ->getMock ();
125+
126+ $ this ->request ->method ('getQuery ' )->willReturnSelf ();
127+ $ context = $ this ->getContextMock ();
128+ $ context ->method ('getRequest ' )->willReturn ($ this ->request );
108129
109130 $ objectManagerHelper = new ObjectManager ($ this );
110131 $ this ->configurable = $ objectManagerHelper ->getObject (
111132 ConfigurableRenderer::class,
112133 [
134+ 'context ' => $ context ,
113135 'scopeConfig ' => $ this ->scopeConfig ,
114136 'imageHelper ' => $ this ->imageHelper ,
115137 'imageUrlBuilder ' => $ this ->imageUrlBuilder ,
@@ -123,7 +145,8 @@ protected function setUp(): void
123145 'priceCurrency ' => $ this ->priceCurrency ,
124146 'configurableAttributeData ' => $ this ->configurableAttributeData ,
125147 'data ' => [],
126- 'variationPrices ' => $ this ->variationPricesMock
148+ 'variationPrices ' => $ this ->variationPricesMock ,
149+ 'customerSession ' => $ customerSession ,
127150 ]
128151 );
129152 }
@@ -262,4 +285,61 @@ public function testGetPricesJson()
262285 $ this ->jsonEncoder ->expects ($ this ->once ())->method ('encode ' )->with ($ expectedPrices );
263286 $ this ->configurable ->getPricesJson ();
264287 }
288+
289+ /**
290+ * Tests that cache key contains query params.
291+ *
292+ * @return void
293+ */
294+ public function testGetCacheKey ()
295+ {
296+ $ requestParams = ['color ' => 59 , 'size ' => 1 , 'random_param ' => '123 ' ];
297+
298+ $ attr1 = $ this ->getMockForAbstractClass (AttributeInterface::class);
299+ $ attr1 ->method ('getAttributeCode ' )->willReturn ('color ' );
300+ $ attr2 = $ this ->getMockForAbstractClass (AttributeInterface::class);
301+ $ attr2 ->method ('getAttributeCode ' )->willReturn ('size ' );
302+ $ configurableAttributes = [$ attr1 , $ attr2 ];
303+
304+ $ currency = $ this ->createMock (AbstractModel::class);
305+ $ this ->priceCurrency ->method ('getCurrency ' )->willReturn ($ currency );
306+ $ this ->swatchHelper ->method ('getAttributesFromConfigurable ' )
307+ ->with ($ this ->product )
308+ ->willReturn ($ configurableAttributes );
309+
310+ $ this ->request ->method ('toArray ' )->willReturn ($ requestParams );
311+ $ this ->assertStringContainsString (
312+ sha1 (json_encode (['color ' => 59 , 'size ' => 1 ])),
313+ $ this ->configurable ->getCacheKey ()
314+ );
315+ }
316+
317+ /**
318+ * Returns context object mock.
319+ *
320+ * @return Context|MockObject
321+ */
322+ private function getContextMock ()
323+ {
324+ $ context = $ this ->createMock (Context::class);
325+ $ storeManager = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
326+ $ store = $ this ->getMockForAbstractClass (\Magento \Store \Api \Data \StoreInterface::class);
327+ $ storeManager ->method ('getStore ' )->willReturn ($ store );
328+ $ appState = $ this ->createMock (\Magento \Framework \App \State::class);
329+ $ resolver = $ this ->createMock (\Magento \Framework \View \Element \Template \File \Resolver::class);
330+ $ urlBuilder = $ this ->getMockForAbstractClass (\Magento \Framework \UrlInterface::class);
331+ $ registry = $ this ->createMock (\Magento \Framework \Registry::class);
332+ $ product = $ this ->createMock (\Magento \Catalog \Model \Product::class);
333+ $ productType = $ this ->createMock (\Magento \Catalog \Model \Product \Type \AbstractType::class);
334+ $ product ->method ('getTypeInstance ' )->willReturn ($ productType );
335+ $ product ->method ('getId ' )->willReturn (1 );
336+ $ registry ->method ('registry ' )->with ('product ' )->willReturn ($ product );
337+ $ context ->method ('getStoreManager ' )->willReturn ($ storeManager );
338+ $ context ->method ('getAppState ' )->willReturn ($ appState );
339+ $ context ->method ('getResolver ' )->willReturn ($ resolver );
340+ $ context ->method ('getUrlBuilder ' )->willReturn ($ urlBuilder );
341+ $ context ->method ('getRegistry ' )->willReturn ($ registry );
342+
343+ return $ context ;
344+ }
265345}
0 commit comments