5
5
*/
6
6
namespace Magento \Catalog \Test \Unit \Block \Product \Widget ;
7
7
8
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
9
+ use Magento \Catalog \Block \Product \Widget \NewWidget ;
10
+
11
+ /**
12
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13
+ */
8
14
class NewWidgetTest extends \PHPUnit_Framework_TestCase
9
15
{
10
16
/**
@@ -22,26 +28,69 @@ class NewWidgetTest extends \PHPUnit_Framework_TestCase
22
28
*/
23
29
protected $ requestMock ;
24
30
31
+ /** @var \Magento\Backend\Block\Context|\PHPUnit_Framework_MockObject_MockObject */
32
+ protected $ context ;
33
+
34
+ /** @var ObjectManagerHelper */
35
+ protected $ objectManager ;
36
+
37
+ /** @var \Magento\Framework\Event\Manager|\PHPUnit_Framework_MockObject_MockObject */
38
+ protected $ eventManager ;
39
+
40
+ /** @var \Magento\Framework\App\Config|\PHPUnit_Framework_MockObject_MockObject */
41
+ protected $ scopeConfig ;
42
+
43
+ /** @var \Magento\Framework\App\Cache\State|\PHPUnit_Framework_MockObject_MockObject */
44
+ protected $ cacheState ;
45
+
46
+ /** @var \Magento\Catalog\Model\Config|\PHPUnit_Framework_MockObject_MockObject */
47
+ protected $ catalogConfig ;
48
+
49
+ /** @var \Magento\Framework\Stdlib\DateTime\Timezone|\PHPUnit_Framework_MockObject_MockObject */
50
+ protected $ localDate ;
51
+
52
+ /** @var \Magento\Catalog\Model\Resource\Product\Collection|\PHPUnit_Framework_MockObject_MockObject */
53
+ protected $ productCollection ;
54
+
25
55
protected function setUp ()
26
56
{
27
- $ objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
28
- $ contextMock = $ this ->getMock ('Magento\Catalog\Block\Product\Context ' , [], [], '' , false , false );
57
+ $ this ->objectManager = new ObjectManagerHelper ($ this );
58
+ $ this ->eventManager = $ this ->getMock ('Magento\Framework\Event\Manager ' , ['dispatch ' ], [], '' , false , false );
59
+ $ this ->scopeConfig = $ this ->getMock ('Magento\Framework\App\Config ' , ['getValue ' ], [], '' , false , false );
60
+ $ this ->cacheState = $ this ->getMock ('Magento\Framework\App\Cache\State ' , ['isEnabled ' ], [], '' , false , false );
61
+ $ this ->localDate = $ this ->getMock ('Magento\Framework\Stdlib\DateTime\Timezone ' , [], [], '' , false , false );
62
+ $ this ->catalogConfig = $ this ->getMockBuilder ('Magento\Catalog\Model\Config ' )
63
+ ->setMethods (['getProductAttributes ' ])
64
+ ->disableOriginalConstructor ()
65
+ ->getMock ();
29
66
$ this ->layout = $ this ->getMock ('Magento\Framework\View\Layout ' , [], [], '' , false );
30
67
$ this ->requestMock = $ this ->getMockBuilder ('Magento\Framework\App\RequestInterface ' )
31
68
->disableOriginalConstructor ()
32
69
->getMock ();
33
70
34
- $ contextMock ->expects ($ this ->once ())
71
+ $ this ->context = $ this ->getMockBuilder ('Magento\Catalog\Block\Product\Context ' )
72
+ ->setMethods (
73
+ [
74
+ 'getEventManager ' , 'getScopeConfig ' , 'getLayout ' ,
75
+ 'getRequest ' , 'getCacheState ' , 'getCatalogConfig ' ,
76
+ 'getLocaleDate '
77
+ ]
78
+ )
79
+ ->disableOriginalConstructor ()
80
+ ->disableArgumentCloning ()
81
+ ->getMock ();
82
+
83
+ $ this ->context ->expects ($ this ->any ())
35
84
->method ('getLayout ' )
36
- ->will ($ this ->returnValue ( $ this -> layout ) );
37
- $ contextMock -> expects ($ this ->once ())
85
+ ->willReturn ($ this ->layout );
86
+ $ this -> context -> expects ($ this ->any ())
38
87
->method ('getRequest ' )
39
88
->willReturn ($ this ->requestMock );
40
89
41
- $ this ->block = $ objectManager ->getObject (
90
+ $ this ->block = $ this -> objectManager ->getObject (
42
91
'Magento\Catalog\Block\Product\Widget\NewWidget ' ,
43
92
[
44
- 'context ' => $ contextMock
93
+ 'context ' => $ this -> context
45
94
]
46
95
);
47
96
}
@@ -61,10 +110,10 @@ public function testGetProductPriceHtml()
61
110
</span>
62
111
</div> ' ;
63
112
$ type = 'widget-new-list ' ;
64
- $ productMock = $ this ->getMock ('Magento\Catalog\Model\Product ' , [], [], '' , false , false );
113
+ $ productMock = $ this ->getMock ('Magento\Catalog\Model\Product ' , [' getId ' ], [], '' , false , false );
65
114
$ productMock ->expects ($ this ->once ())
66
115
->method ('getId ' )
67
- ->will ( $ this -> returnValue ( $ id ) );
116
+ ->willReturn ( $ id );
68
117
$ arguments = [
69
118
'price_id ' => 'old-price- ' . $ id . '- ' . $ type ,
70
119
'display_minimal_price ' => true ,
@@ -77,12 +126,12 @@ public function testGetProductPriceHtml()
77
126
$ this ->layout ->expects ($ this ->once ())
78
127
->method ('getBlock ' )
79
128
->with ($ this ->equalTo ('product.price.render.default ' ))
80
- ->will ( $ this -> returnValue ( $ priceBoxMock) );
129
+ ->willReturn ( $ priceBoxMock );
81
130
82
131
$ priceBoxMock ->expects ($ this ->once ())
83
132
->method ('render ' )
84
133
->with ($ this ->equalTo ('final_price ' ), $ this ->equalTo ($ productMock ), $ this ->equalTo ($ arguments ))
85
- ->will ( $ this -> returnValue ( $ expectedHtml) );
134
+ ->willReturn ( $ expectedHtml );
86
135
87
136
$ result = $ this ->block ->getProductPriceHtml ($ productMock , $ type );
88
137
$ this ->assertEquals ($ expectedHtml , $ result );
@@ -111,4 +160,174 @@ public function getCurrentPageDataProvider()
111
160
[10 , 10 ]
112
161
];
113
162
}
163
+
164
+ public function testGetProductsCount ()
165
+ {
166
+ $ this ->assertEquals (10 , $ this ->block ->getProductsCount ());
167
+ $ this ->block ->setProductsCount (2 );
168
+ $ this ->assertEquals (2 , $ this ->block ->getProductsCount ());
169
+ }
170
+
171
+ protected function generalGetProductCollection ()
172
+ {
173
+ $ this ->eventManager ->expects ($ this ->once ())->method ('dispatch ' )
174
+ ->will ($ this ->returnValue (true ));
175
+ $ this ->scopeConfig ->expects ($ this ->once ())->method ('getValue ' )->withAnyParameters ()
176
+ ->willReturn (false );
177
+ $ this ->cacheState ->expects ($ this ->atLeastOnce ())->method ('isEnabled ' )->withAnyParameters ()
178
+ ->willReturn (false );
179
+ $ this ->catalogConfig ->expects ($ this ->once ())->method ('getProductAttributes ' )
180
+ ->willReturn ([]);
181
+ $ this ->localDate ->expects ($ this ->any ())->method ('date ' )
182
+ ->willReturn (new \DateTime ('now ' , new \DateTimeZone ('UTC ' )));
183
+
184
+ $ this ->context ->expects ($ this ->once ())->method ('getEventManager ' )->willReturn ($ this ->eventManager );
185
+ $ this ->context ->expects ($ this ->once ())->method ('getScopeConfig ' )->willReturn ($ this ->scopeConfig );
186
+ $ this ->context ->expects ($ this ->once ())->method ('getCacheState ' )->willReturn ($ this ->cacheState );
187
+ $ this ->context ->expects ($ this ->once ())->method ('getCatalogConfig ' )->willReturn ($ this ->catalogConfig );
188
+ $ this ->context ->expects ($ this ->once ())->method ('getLocaleDate ' )->willReturn ($ this ->localDate );
189
+
190
+ $ this ->productCollection = $ this ->getMockBuilder ('Magento\Catalog\Model\Resource\Product\Collection ' )
191
+ ->setMethods (
192
+ [
193
+ 'setVisibility ' , 'addMinimalPrice ' , 'addFinalPrice ' ,
194
+ 'addTaxPercents ' , 'addAttributeToSelect ' , 'addUrlRewrite ' ,
195
+ 'addStoreFilter ' , 'addAttributeToSort ' , 'setPageSize ' ,
196
+ 'setCurPage ' , 'addAttributeToFilter '
197
+ ]
198
+ )
199
+ ->disableOriginalConstructor ()
200
+ ->getMock ();
201
+ $ this ->productCollection ->expects ($ this ->once ())->method ('setVisibility ' )
202
+ ->willReturnSelf ();
203
+ $ this ->productCollection ->expects ($ this ->once ())->method ('addMinimalPrice ' )
204
+ ->willReturnSelf ();
205
+ $ this ->productCollection ->expects ($ this ->once ())->method ('addFinalPrice ' )
206
+ ->willReturnSelf ();
207
+ $ this ->productCollection ->expects ($ this ->once ())->method ('addTaxPercents ' )
208
+ ->willReturnSelf ();
209
+ $ this ->productCollection ->expects ($ this ->once ())->method ('addAttributeToSelect ' )
210
+ ->willReturnSelf ();
211
+ $ this ->productCollection ->expects ($ this ->once ())->method ('addUrlRewrite ' )
212
+ ->willReturnSelf ();
213
+ $ this ->productCollection ->expects ($ this ->once ())->method ('addStoreFilter ' )
214
+ ->willReturnSelf ();
215
+ $ this ->productCollection ->expects ($ this ->once ())->method ('addAttributeToSort ' )
216
+ ->willReturnSelf ();
217
+ $ this ->productCollection ->expects ($ this ->atLeastOnce ())->method ('setCurPage ' )
218
+ ->willReturnSelf ();
219
+ $ this ->productCollection ->expects ($ this ->any ())->method ('addAttributeToFilter ' )
220
+ ->willReturnSelf ();
221
+ }
222
+
223
+ /**
224
+ * @param string $displayType
225
+ * @param bool $pagerEnable
226
+ * @param int $productsCount
227
+ * @param int $productsPerPage
228
+ */
229
+ protected function startTestGetProductCollection ($ displayType , $ pagerEnable , $ productsCount , $ productsPerPage )
230
+ {
231
+ $ productCollectionFactory = $ this ->getMock (
232
+ 'Magento\Catalog\Model\Resource\Product\CollectionFactory ' ,
233
+ ['create ' ],
234
+ [],
235
+ '' ,
236
+ false ,
237
+ false
238
+ );
239
+ $ productCollectionFactory ->expects ($ this ->atLeastOnce ())->method ('create ' )
240
+ ->willReturn ($ this ->productCollection );
241
+
242
+ $ this ->block = $ this ->objectManager ->getObject (
243
+ 'Magento\Catalog\Block\Product\Widget\NewWidget ' ,
244
+ [
245
+ 'context ' => $ this ->context ,
246
+ 'productCollectionFactory ' => $ productCollectionFactory
247
+ ]
248
+ );
249
+
250
+ if (null === $ productsPerPage ) {
251
+ $ this ->block ->unsetData ('products_per_page ' );
252
+ } else {
253
+ $ this ->block ->setData ('products_per_page ' , $ productsPerPage );
254
+ }
255
+
256
+ $ this ->block ->setData ('show_pager ' , $ pagerEnable );
257
+ $ this ->block ->setData ('display_type ' , $ displayType );
258
+ $ this ->block ->setProductsCount ($ productsCount );
259
+ $ this ->block ->toHtml ();
260
+ }
261
+
262
+ /**
263
+ * Test protected `_getProductCollection` and `getPageSize` methods via public `toHtml` method,
264
+ * for display_type == DISPLAY_TYPE_NEW_PRODUCTS.
265
+ *
266
+ * @param bool $pagerEnable
267
+ * @param int $productsCount
268
+ * @param int $productsPerPage
269
+ * @param int $expectedPageSize
270
+ * @dataProvider getProductCollectionDataProvider
271
+ */
272
+ public function testGetProductNewCollection ($ pagerEnable , $ productsCount , $ productsPerPage , $ expectedPageSize )
273
+ {
274
+ $ this ->generalGetProductCollection ();
275
+
276
+ $ this ->productCollection ->expects ($ this ->exactly (2 ))->method ('setPageSize ' )
277
+ ->withConsecutive (
278
+ [$ productsCount ],
279
+ [$ expectedPageSize ]
280
+ )
281
+ ->willReturnSelf ();
282
+
283
+ $ this ->startTestGetProductCollection (
284
+ NewWidget::DISPLAY_TYPE_NEW_PRODUCTS ,
285
+ $ pagerEnable ,
286
+ $ productsCount ,
287
+ $ productsPerPage
288
+ );
289
+ }
290
+
291
+ /**
292
+ * Test protected `_getProductCollection` and `getPageSize` methods via public `toHtml` method,
293
+ * for display_type == DISPLAY_TYPE_ALL_PRODUCTS.
294
+ *
295
+ * @param bool $pagerEnable
296
+ * @param int $productsCount
297
+ * @param int $productsPerPage
298
+ * @param int $expectedPageSize
299
+ * @dataProvider getProductCollectionDataProvider
300
+ */
301
+ public function testGetProductAllCollection ($ pagerEnable , $ productsCount , $ productsPerPage , $ expectedPageSize )
302
+ {
303
+ $ this ->generalGetProductCollection ();
304
+
305
+ $ this ->productCollection ->expects ($ this ->atLeastOnce ())->method ('setPageSize ' )->with ($ expectedPageSize )
306
+ ->willReturnSelf ();
307
+
308
+ $ this ->startTestGetProductCollection (
309
+ NewWidget::DISPLAY_TYPE_ALL_PRODUCTS ,
310
+ $ pagerEnable ,
311
+ $ productsCount ,
312
+ $ productsPerPage
313
+ );
314
+ }
315
+
316
+ public function getProductCollectionDataProvider ()
317
+ {
318
+ return [
319
+ [true , 1 , null , 5 ],
320
+ [true , 5 , null , 5 ],
321
+ [true , 10 , null , 5 ],
322
+ [true , 1 , 2 , 2 ],
323
+ [true , 5 , 3 , 3 ],
324
+ [true , 10 , 7 , 7 ],
325
+ [false , 1 , null , 1 ],
326
+ [false , 3 , null , 3 ],
327
+ [false , 5 , null , 5 ],
328
+ [false , 1 , 3 , 1 ],
329
+ [false , 3 , 5 , 3 ],
330
+ [false , 5 , 10 , 5 ]
331
+ ];
332
+ }
114
333
}
0 commit comments