Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductsListTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 
19 class ProductsListTest extends \PHPUnit\Framework\TestCase
20 {
24  protected $productsList;
25 
29  protected $collectionFactory;
30 
34  protected $visibility;
35 
39  protected $httpContext;
40 
44  protected $builder;
45 
49  protected $rule;
50 
55 
59  protected $storeManager;
60 
64  protected $design;
65 
69  protected $request;
70 
74  protected $layout;
75 
79  private $priceCurrency;
80 
84  private $serializer;
85 
86  protected function setUp()
87  {
88  $this->collectionFactory =
89  $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class)
90  ->setMethods(['create'])
91  ->disableOriginalConstructor()->getMock();
92  $this->visibility = $this->getMockBuilder(\Magento\Catalog\Model\Product\Visibility::class)
93  ->setMethods(['getVisibleInCatalogIds'])
94  ->disableOriginalConstructor()
95  ->getMock();
96  $this->httpContext = $this->createMock(\Magento\Framework\App\Http\Context::class);
97  $this->builder = $this->createMock(\Magento\Rule\Model\Condition\Sql\Builder::class);
98  $this->rule = $this->createMock(\Magento\CatalogWidget\Model\Rule::class);
99  $this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
100  $this->widgetConditionsHelper = $this->getMockBuilder(\Magento\Widget\Helper\Conditions::class)
101  ->disableOriginalConstructor()
102  ->getMock();
103  $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
104  $this->design = $this->createMock(\Magento\Framework\View\DesignInterface::class);
105 
106  $objectManagerHelper = new ObjectManagerHelper($this);
107  $arguments = $objectManagerHelper->getConstructArguments(
108  \Magento\CatalogWidget\Block\Product\ProductsList::class,
109  [
110  'productCollectionFactory' => $this->collectionFactory,
111  'catalogProductVisibility' => $this->visibility,
112  'httpContext' => $this->httpContext,
113  'sqlBuilder' => $this->builder,
114  'rule' => $this->rule,
115  'conditionsHelper' => $this->widgetConditionsHelper,
116  'storeManager' => $this->storeManager,
117  'design' => $this->design,
118  'json' => $this->serializer
119  ]
120  );
121  $this->request = $arguments['context']->getRequest();
122  $this->layout = $arguments['context']->getLayout();
123  $this->priceCurrency = $this->createMock(PriceCurrencyInterface::class);
124 
125  $this->productsList = $objectManagerHelper->getObject(
126  \Magento\CatalogWidget\Block\Product\ProductsList::class,
127  $arguments
128  );
129  $objectManagerHelper->setBackwardCompatibleProperty($this->productsList, 'priceCurrency', $this->priceCurrency);
130  }
131 
132  public function testGetCacheKeyInfo()
133  {
134  $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
135  ->disableOriginalConstructor()->setMethods(['getId'])->getMock();
136  $store->expects($this->once())->method('getId')->willReturn(1);
137  $this->storeManager->expects($this->once())->method('getStore')->willReturn($store);
138 
139  $theme = $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class);
140  $theme->expects($this->once())->method('getId')->willReturn('blank');
141  $this->design->expects($this->once())->method('getDesignTheme')->willReturn($theme);
142 
143  $this->httpContext->expects($this->once())->method('getValue')->willReturn('context_group');
144  $this->productsList->setData('conditions', 'some_serialized_conditions');
145 
146  $this->productsList->setData('page_var_name', 'page_number');
147  $this->productsList->setTemplate('test_template');
148  $this->productsList->setData('title', 'test_title');
149  $this->request->expects($this->once())->method('getParam')->with('page_number')->willReturn(1);
150 
151  $this->request->expects($this->once())->method('getParams')->willReturn('request_params');
152  $currency = $this->createMock(\Magento\Directory\Model\Currency::class);
153  $currency->expects($this->once())->method('getCode')->willReturn('USD');
154  $this->priceCurrency->expects($this->once())->method('getCurrency')->willReturn($currency);
155 
156  $this->serializer->expects($this->any())
157  ->method('serialize')
158  ->willReturnCallback(function ($value) {
159  return json_encode($value);
160  });
161 
162  $cacheKey = [
163  'CATALOG_PRODUCTS_LIST_WIDGET',
164  'USD',
165  1,
166  'blank',
167  'context_group',
168  1,
169  5,
170  'some_serialized_conditions',
171  json_encode('request_params'),
172  'test_template',
173  'test_title'
174  ];
175  $this->assertEquals($cacheKey, $this->productsList->getCacheKeyInfo());
176  }
177 
178  public function testGetProductPriceHtml()
179  {
180  $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
181  ->setMethods(['getId'])
182  ->disableOriginalConstructor()
183  ->getMock();
184  $product->expects($this->once())->method('getId')->willReturn(1);
185 
186  $priceRenderer = $this->getMockBuilder(\Magento\Framework\Pricing\Render::class)
187  ->setMethods(['render'])
188  ->disableOriginalConstructor()
189  ->getMock();
190  $priceRenderer->expects($this->once())
191  ->method('render')
192  ->with('final_price', $product, [
193  'include_container' => false,
194  'display_minimal_price' => false,
195  'zone' => 'item_list',
196  'price_id' => 'old-price-1-some-price-type'
197  ])
198  ->willReturn('<html>');
199  $this->layout->expects($this->once())->method('getBlock')->willReturn($priceRenderer);
200 
201  $this->assertEquals('<html>', $this->productsList->getProductPriceHtml(
202  $product,
203  'some-price-type',
204  \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
205  [
206  'include_container' => false,
207  'display_minimal_price' => false
208  ]
209  ));
210  }
211 
212  public function testGetPagerHtmlEmpty()
213  {
214  $this->assertEquals('', $this->productsList->getPagerHtml());
215  }
216 
217  public function testGetPagerHtml()
218  {
219  $collection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
220  ->setMethods(['getSize'])
221  ->disableOriginalConstructor()
222  ->getMock();
223  $collection->expects($this->once())->method('getSize')->willReturn(3);
224 
225  $this->productsList->setData('show_pager', true);
226  $this->productsList->setData('products_per_page', 2);
227  $this->productsList->setData('product_collection', $collection);
228 
229  $pagerBlock = $this->getMockBuilder(\Magento\Catalog\Block\Product\Widget\Html\Pager::class)
230  ->setMethods([
231  'toHtml',
232  'setUseContainer',
233  'setShowAmounts',
234  'setShowPerPage',
235  'setPageVarName',
236  'setLimit',
237  'setTotalLimit',
238  'setCollection',
239  ])->disableOriginalConstructor()->getMock();
240 
241  $pagerBlock->expects($this->once())->method('setUseContainer')->willReturnSelf();
242  $pagerBlock->expects($this->once())->method('setShowAmounts')->willReturnSelf();
243  $pagerBlock->expects($this->once())->method('setShowPerPage')->willReturnSelf();
244  $pagerBlock->expects($this->once())->method('setPageVarName')->willReturnSelf();
245  $pagerBlock->expects($this->once())->method('setLimit')->willReturnSelf();
246  $pagerBlock->expects($this->once())->method('setTotalLimit')->willReturnSelf();
247  $pagerBlock->expects($this->once())->method('setCollection')->with($collection)->willReturnSelf();
248 
249  $pagerBlock->expects($this->once())->method('toHtml')->willReturn('<pager_html>');
250  $this->layout->expects($this->once())->method('createBlock')->willReturn($pagerBlock);
251  $this->assertEquals('<pager_html>', $this->productsList->getPagerHtml());
252  }
253 
264  public function testCreateCollection($pagerEnable, $productsCount, $productsPerPage, $expectedPageSize)
265  {
266  $this->visibility->expects($this->once())->method('getVisibleInCatalogIds')
268  $collection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
269  ->setMethods([
270  'setVisibility',
271  'addMinimalPrice',
272  'addFinalPrice',
273  'addTaxPercents',
274  'addAttributeToSelect',
275  'addUrlRewrite',
276  'addStoreFilter',
277  'setPageSize',
278  'setCurPage',
279  'distinct'
280  ])->disableOriginalConstructor()
281  ->getMock();
282  $collection->expects($this->once())->method('setVisibility')
284  ->willReturnSelf();
285  $collection->expects($this->once())->method('addMinimalPrice')->willReturnSelf();
286  $collection->expects($this->once())->method('addFinalPrice')->willReturnSelf();
287  $collection->expects($this->once())->method('addTaxPercents')->willReturnSelf();
288  $collection->expects($this->once())->method('addAttributeToSelect')->willReturnSelf();
289  $collection->expects($this->once())->method('addUrlRewrite')->willReturnSelf();
290  $collection->expects($this->once())->method('addStoreFilter')->willReturnSelf();
291  $collection->expects($this->once())->method('setPageSize')->with($expectedPageSize)->willReturnSelf();
292  $collection->expects($this->once())->method('setCurPage')->willReturnSelf();
293  $collection->expects($this->once())->method('distinct')->willReturnSelf();
294 
295  $this->collectionFactory->expects($this->once())->method('create')->willReturn($collection);
296  $this->productsList->setData('conditions_encoded', 'some_serialized_conditions');
297 
298  $this->widgetConditionsHelper->expects($this->once())
299  ->method('decode')
300  ->with('some_serialized_conditions')
301  ->willReturn([]);
302 
303  $this->builder->expects($this->once())->method('attachConditionToCollection')
304  ->with($collection, $this->getConditionsForCollection($collection))
305  ->willReturnSelf();
306 
307  if ($productsPerPage) {
308  $this->productsList->setData('products_per_page', $productsPerPage);
309  } else {
310  $this->productsList->unsetData('products_per_page');
311  }
312 
313  $this->productsList->setData('show_pager', $pagerEnable);
314  $this->productsList->setData('products_count', $productsCount);
315 
316  $this->assertSame($collection, $this->productsList->createCollection());
317  }
318 
323  {
324  return [
325  [true, 1, null, 5],
326  [true, 5, null, 5],
327  [true, 10, null, 5],
328  [true, 1, 2, 2],
329  [true, 5, 3, 3],
330  [true, 10, 7, 7],
331  [false, 1, null, 1],
332  [false, 3, null, 3],
333  [false, 5, null, 5],
334  [false, 1, 3, 1],
335  [false, 3, 5, 3],
336  [false, 5, 10, 5]
337  ];
338  }
339 
340  public function testGetProductsCount()
341  {
342  $this->assertEquals(10, $this->productsList->getProductsCount());
343  $this->productsList->setProductsCount(2);
344  $this->assertEquals(2, $this->productsList->getProductsCount());
345  }
346 
347  public function testGetProductsPerPage()
348  {
349  $this->productsList->setData('products_per_page', 2);
350  $this->assertEquals(2, $this->productsList->getProductsPerPage());
351  }
352 
354  {
355  $this->assertEquals(ProductsList::DEFAULT_PRODUCTS_PER_PAGE, $this->productsList->getProductsPerPage());
356  }
357 
358  public function testShowPager()
359  {
360  $this->assertEquals(false, $this->productsList->showPager());
361  $this->productsList->setData('show_pager', true);
362  $this->assertEquals(true, $this->productsList->showPager());
363  }
364 
365  public function testGetIdentities()
366  {
367  $collection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
368  ->setMethods([
369  'addAttributeToSelect',
370  'getIterator',
371  ])->disableOriginalConstructor()
372  ->getMock();
373 
374  $product = $this->createPartialMock(\Magento\Framework\DataObject\IdentityInterface::class, ['getIdentities']);
375  $notProduct = $this->getMockBuilder('NotProduct')
376  ->setMethods(['getIdentities'])
377  ->disableOriginalConstructor()
378  ->getMock();
379  $product->expects($this->once())->method('getIdentities')->willReturn(['product_identity']);
380  $collection->expects($this->once())->method('getIterator')->willReturn(
381  new \ArrayIterator([$product, $notProduct])
382  );
383  $this->productsList->setData('product_collection', $collection);
384 
385  $this->assertEquals(
386  ['product_identity'],
387  $this->productsList->getIdentities()
388  );
389  }
390 
396  private function getConditionsForCollection($collection)
397  {
398  $conditions = $this->getMockBuilder(\Magento\Rule\Model\Condition\Combine::class)
399  ->setMethods(['collectValidatedAttributes'])
400  ->disableOriginalConstructor()
401  ->getMock();
402  $conditions->expects($this->once())->method('collectValidatedAttributes')
403  ->with($collection)
404  ->willReturnSelf();
405 
406  $this->rule->expects($this->once())->method('loadPost')->willReturnSelf();
407  $this->rule->expects($this->once())->method('getConditions')->willReturn($conditions);
408  return $conditions;
409  }
410 
411  public function testGetTitle()
412  {
413  $this->assertEmpty($this->productsList->getTitle());
414  }
415 
416  public function testGetNonDefaultTitle()
417  {
418  $this->productsList->setTitle('Custom Title');
419  $this->assertEquals('Custom Title', $this->productsList->getTitle());
420  }
421 
422  public function testScope()
423  {
424  $this->assertFalse($this->productsList->isScopePrivate());
425  }
426 }
testCreateCollection($pagerEnable, $productsCount, $productsPerPage, $expectedPageSize)
$value
Definition: gender.phtml:16
$theme
$arguments