7
7
8
8
namespace Magento \Swatches \Test \Unit \Block \Product \Renderer \Listing ;
9
9
10
+ use Magento \Catalog \Block \Product \Context ;
10
11
use Magento \Catalog \Helper \Image ;
11
12
use Magento \Catalog \Helper \Product ;
12
13
use Magento \Catalog \Model \Product \Attribute \Source \Status ;
17
18
use Magento \ConfigurableProduct \Model \Product \Type \Configurable \Attribute ;
18
19
use Magento \ConfigurableProduct \Model \Product \Type \Configurable \Variations \Prices ;
19
20
use Magento \Customer \Helper \Session \CurrentCustomer ;
21
+ use Magento \Eav \Api \Data \AttributeInterface ;
20
22
use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
21
23
use Magento \Framework \App \Config \ScopeConfigInterface ;
24
+ use Magento \Framework \App \Request \Http ;
25
+ use Magento \Framework \App \RequestInterface ;
22
26
use Magento \Framework \Json \EncoderInterface ;
27
+ use Magento \Framework \Model \AbstractModel ;
23
28
use Magento \Framework \Pricing \PriceCurrencyInterface ;
24
29
use Magento \Framework \Pricing \PriceInfo \Base ;
25
30
use Magento \Framework \Stdlib \ArrayUtils ;
26
31
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
32
+ use Magento \Store \Model \StoreManagerInterface ;
27
33
use Magento \Swatches \Block \Product \Renderer \Configurable ;
28
34
use Magento \Swatches \Block \Product \Renderer \Listing \Configurable as ConfigurableRenderer ;
29
35
use Magento \Swatches \Helper \Media ;
@@ -84,6 +90,11 @@ class ConfigurableTest extends TestCase
84
90
/** @var MockObject */
85
91
private $ variationPricesMock ;
86
92
93
+ /**
94
+ * @var RequestInterface|MockObject
95
+ */
96
+ private $ request ;
97
+
87
98
protected function setUp (): void
88
99
{
89
100
$ this ->arrayUtils = $ this ->createMock (ArrayUtils::class);
@@ -105,11 +116,22 @@ protected function setUp(): void
105
116
$ this ->variationPricesMock = $ this ->createMock (
106
117
Prices::class
107
118
);
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 );
108
129
109
130
$ objectManagerHelper = new ObjectManager ($ this );
110
131
$ this ->configurable = $ objectManagerHelper ->getObject (
111
132
ConfigurableRenderer::class,
112
133
[
134
+ 'context ' => $ context ,
113
135
'scopeConfig ' => $ this ->scopeConfig ,
114
136
'imageHelper ' => $ this ->imageHelper ,
115
137
'imageUrlBuilder ' => $ this ->imageUrlBuilder ,
@@ -123,7 +145,8 @@ protected function setUp(): void
123
145
'priceCurrency ' => $ this ->priceCurrency ,
124
146
'configurableAttributeData ' => $ this ->configurableAttributeData ,
125
147
'data ' => [],
126
- 'variationPrices ' => $ this ->variationPricesMock
148
+ 'variationPrices ' => $ this ->variationPricesMock ,
149
+ 'customerSession ' => $ customerSession ,
127
150
]
128
151
);
129
152
}
@@ -262,4 +285,61 @@ public function testGetPricesJson()
262
285
$ this ->jsonEncoder ->expects ($ this ->once ())->method ('encode ' )->with ($ expectedPrices );
263
286
$ this ->configurable ->getPricesJson ();
264
287
}
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
+ }
265
345
}
0 commit comments