Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NewWidgetTest.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class NewWidgetTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $block;
20 
24  protected $layout;
25 
29  protected $requestMock;
30 
32  protected $context;
33 
35  protected $objectManager;
36 
38  protected $eventManager;
39 
41  protected $scopeConfig;
42 
44  protected $cacheState;
45 
47  protected $catalogConfig;
48 
50  protected $localDate;
51 
53  protected $productCollection;
54 
58  protected function setUp()
59  {
60  $this->objectManager = new ObjectManagerHelper($this);
61  $this->eventManager = $this->createPartialMock(\Magento\Framework\Event\Manager::class, ['dispatch']);
62  $this->scopeConfig = $this->createMock(\Magento\Framework\App\Config::class);
63  $this->cacheState = $this->createPartialMock(\Magento\Framework\App\Cache\State::class, ['isEnabled']);
64  $this->localDate = $this->createMock(\Magento\Framework\Stdlib\DateTime\Timezone::class);
65  $this->catalogConfig = $this->getMockBuilder(\Magento\Catalog\Model\Config::class)
66  ->setMethods(['getProductAttributes'])
67  ->disableOriginalConstructor()
68  ->getMock();
69  $this->layout = $this->createMock(\Magento\Framework\View\Layout::class);
70  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73 
74  $this->context = $this->getMockBuilder(\Magento\Catalog\Block\Product\Context::class)
75  ->setMethods(
76  [
77  'getEventManager', 'getScopeConfig', 'getLayout',
78  'getRequest', 'getCacheState', 'getCatalogConfig',
79  'getLocaleDate'
80  ]
81  )
82  ->disableOriginalConstructor()
83  ->disableArgumentCloning()
84  ->getMock();
85 
86  $this->context->expects($this->any())
87  ->method('getLayout')
88  ->willReturn($this->layout);
89  $this->context->expects($this->any())
90  ->method('getRequest')
91  ->willReturn($this->requestMock);
92 
93  $this->block = $this->objectManager->getObject(
94  \Magento\Catalog\Block\Product\Widget\NewWidget::class,
95  [
96  'context' => $this->context
97  ]
98  );
99  }
100 
104  protected function tearDown()
105  {
106  $this->block = null;
107  }
108 
109  public function testGetProductPriceHtml()
110  {
111  $id = 6;
112  $expectedHtml = '
113  <div class="price-box price-final_price">
114  <span class="regular-price" id="product-price-' . $id . '">
115  <span class="price">$0.00</span>
116  </span>
117  </div>';
118  $type = 'widget-new-list';
119  $productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getId']);
120  $productMock->expects($this->once())
121  ->method('getId')
122  ->willReturn($id);
123  $arguments = [
124  'price_id' => 'old-price-' . $id . '-' . $type,
125  'display_minimal_price' => true,
126  'include_container' => true,
128  ];
129 
130  $priceBoxMock = $this->createPartialMock(\Magento\Framework\Pricing\Render::class, ['render']);
131 
132  $this->layout->expects($this->once())
133  ->method('getBlock')
134  ->with($this->equalTo('product.price.render.default'))
135  ->willReturn($priceBoxMock);
136 
137  $priceBoxMock->expects($this->once())
138  ->method('render')
139  ->with($this->equalTo('final_price'), $this->equalTo($productMock), $this->equalTo($arguments))
140  ->willReturn($expectedHtml);
141 
142  $result = $this->block->getProductPriceHtml($productMock, $type);
143  $this->assertEquals($expectedHtml, $result);
144  }
145 
151  public function testGetCurrentPage($pageNumber, $expectedResult)
152  {
153  $this->block->setData('page_var_name', 'page_number');
154 
155  $this->requestMock->expects($this->any())
156  ->method('getParam')
157  ->with('page_number')
158  ->willReturn($pageNumber);
159 
160  $this->assertEquals($expectedResult, $this->block->getCurrentPage());
161  }
162 
166  public function getCurrentPageDataProvider()
167  {
168  return [
169  [1, 1],
170  [5, 5],
171  [10, 10]
172  ];
173  }
174 
175  public function testGetProductsCount()
176  {
177  $this->assertEquals(10, $this->block->getProductsCount());
178  $this->block->setProductsCount(2);
179  $this->assertEquals(2, $this->block->getProductsCount());
180  }
181 
185  protected function generalGetProductCollection()
186  {
187  $this->eventManager->expects($this->exactly(2))->method('dispatch')
188  ->will($this->returnValue(true));
189  $this->scopeConfig->expects($this->once())->method('getValue')->withAnyParameters()
190  ->willReturn(false);
191  $this->cacheState->expects($this->atLeastOnce())->method('isEnabled')->withAnyParameters()
192  ->willReturn(false);
193  $this->catalogConfig->expects($this->once())->method('getProductAttributes')
194  ->willReturn([]);
195  $this->localDate->expects($this->any())->method('date')
196  ->willReturn(new \DateTime('now', new \DateTimeZone('UTC')));
197 
198  $this->context->expects($this->once())->method('getEventManager')->willReturn($this->eventManager);
199  $this->context->expects($this->once())->method('getScopeConfig')->willReturn($this->scopeConfig);
200  $this->context->expects($this->once())->method('getCacheState')->willReturn($this->cacheState);
201  $this->context->expects($this->once())->method('getCatalogConfig')->willReturn($this->catalogConfig);
202  $this->context->expects($this->once())->method('getLocaleDate')->willReturn($this->localDate);
203 
204  $this->productCollection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
205  ->setMethods(
206  [
207  'setVisibility', 'addMinimalPrice', 'addFinalPrice',
208  'addTaxPercents', 'addAttributeToSelect', 'addUrlRewrite',
209  'addStoreFilter', 'addAttributeToSort', 'setPageSize',
210  'setCurPage', 'addAttributeToFilter'
211  ]
212  )
213  ->disableOriginalConstructor()
214  ->getMock();
215  $this->productCollection->expects($this->once())->method('setVisibility')
216  ->willReturnSelf();
217  $this->productCollection->expects($this->once())->method('addMinimalPrice')
218  ->willReturnSelf();
219  $this->productCollection->expects($this->once())->method('addFinalPrice')
220  ->willReturnSelf();
221  $this->productCollection->expects($this->once())->method('addTaxPercents')
222  ->willReturnSelf();
223  $this->productCollection->expects($this->once())->method('addAttributeToSelect')
224  ->willReturnSelf();
225  $this->productCollection->expects($this->once())->method('addUrlRewrite')
226  ->willReturnSelf();
227  $this->productCollection->expects($this->once())->method('addStoreFilter')
228  ->willReturnSelf();
229  $this->productCollection->expects($this->once())->method('addAttributeToSort')
230  ->willReturnSelf();
231  $this->productCollection->expects($this->atLeastOnce())->method('setCurPage')
232  ->willReturnSelf();
233  $this->productCollection->expects($this->any())->method('addAttributeToFilter')
234  ->willReturnSelf();
235  }
236 
243  protected function startTestGetProductCollection($displayType, $pagerEnable, $productsCount, $productsPerPage)
244  {
245  $productCollectionFactory = $this->createPartialMock(
246  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class,
247  ['create']
248  );
249  $productCollectionFactory->expects($this->atLeastOnce())->method('create')
250  ->willReturn($this->productCollection);
251 
252  $this->block = $this->objectManager->getObject(
253  \Magento\Catalog\Block\Product\Widget\NewWidget::class,
254  [
255  'context' => $this->context,
256  'productCollectionFactory' => $productCollectionFactory
257  ]
258  );
259 
260  if (null === $productsPerPage) {
261  $this->block->unsetData('products_per_page');
262  } else {
263  $this->block->setData('products_per_page', $productsPerPage);
264  }
265 
266  $this->block->setData('show_pager', $pagerEnable);
267  $this->block->setData('display_type', $displayType);
268  $this->block->setProductsCount($productsCount);
269  $this->block->toHtml();
270  }
271 
282  public function testGetProductNewCollection($pagerEnable, $productsCount, $productsPerPage, $expectedPageSize)
283  {
285 
286  $this->productCollection->expects($this->exactly(2))->method('setPageSize')
287  ->withConsecutive(
288  [$productsCount],
289  [$expectedPageSize]
290  )
291  ->willReturnSelf();
292 
295  $pagerEnable,
296  $productsCount,
297  $productsPerPage
298  );
299  }
300 
311  public function testGetProductAllCollection($pagerEnable, $productsCount, $productsPerPage, $expectedPageSize)
312  {
314 
315  $this->productCollection->expects($this->atLeastOnce())->method('setPageSize')->with($expectedPageSize)
316  ->willReturnSelf();
317 
320  $pagerEnable,
321  $productsCount,
322  $productsPerPage
323  );
324  }
325 
330  {
331  return [
332  [true, 1, null, 5],
333  [true, 5, null, 5],
334  [true, 10, null, 5],
335  [true, 1, 2, 2],
336  [true, 5, 3, 3],
337  [true, 10, 7, 7],
338  [false, 1, null, 1],
339  [false, 3, null, 3],
340  [false, 5, null, 5],
341  [false, 1, 3, 1],
342  [false, 3, 5, 3],
343  [false, 5, 10, 5]
344  ];
345  }
346 }
$id
Definition: fieldset.phtml:14
testGetProductNewCollection($pagerEnable, $productsCount, $productsPerPage, $expectedPageSize)
$type
Definition: item.phtml:13
startTestGetProductCollection($displayType, $pagerEnable, $productsCount, $productsPerPage)
testGetProductAllCollection($pagerEnable, $productsCount, $productsPerPage, $expectedPageSize)
$arguments