Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NewTest.php
Go to the documentation of this file.
1 <?php
7 
14 class NewTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $_block;
20 
21  protected function setUp()
22  {
27  ->get(\Magento\Customer\Api\GroupManagementInterface::class);
28  $notLoggedInId = $groupManagement->getNotLoggedInGroup()->getId();
29 
30  \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea(\Magento\Framework\App\Area::AREA_FRONTEND);
32  \Magento\Framework\App\Http\Context::class
33  )->setValue(
34  \Magento\Customer\Model\Context::CONTEXT_GROUP,
35  $notLoggedInId,
36  $notLoggedInId
37  );
39  \Magento\Framework\View\LayoutInterface::class
40  )->createBlock(
41  \Magento\Catalog\Block\Product\NewProduct::class
42  );
43  }
44 
45  public function testGetCacheKeyInfo()
46  {
47  $info = $this->_block->getCacheKeyInfo();
48  $keys = array_keys($info);
49 
52  $this->assertSame(0, array_shift($keys));
53  $this->assertEquals('CATALOG_PRODUCT_NEW', $info[0]);
54 
55  $this->assertSame(1, array_shift($keys));
56  $this->assertEquals(
57  \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
58  \Magento\Store\Model\StoreManagerInterface::class
59  )->getStore()->getId(),
60  $info[1]
61  );
62 
63  $this->assertSame(2, array_shift($keys));
64 
66  \Magento\Framework\View\DesignInterface::class
67  )->getDesignTheme();
68 
69  $this->assertEquals($themeModel->getId() ?: null, $info[2]);
70 
71  $this->assertSame(3, array_shift($keys));
72  $this->assertEquals(
73  \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
74  \Magento\Customer\Model\Session::class
75  )->getCustomerGroupId(),
76  $info[3]
77  );
78 
79  $this->assertSame('template', array_shift($keys));
80 
85  $this->assertSame(4, array_shift($keys));
86  $this->assertNotEquals('', $info[4]);
87  }
88 
89  public function testSetGetProductsCount()
90  {
91  $this->assertEquals(
93  $this->_block->getProductsCount()
94  );
95  $this->_block->setProductsCount(100);
96  $this->assertEquals(100, $this->_block->getProductsCount());
97  }
98 
99  public function testToHtml()
100  {
101  $this->assertEmpty($this->_block->getProductCollection());
102 
103  $this->_block->setProductsCount(5);
104  $this->_block->setTemplate('product/widget/new/content/new_list.phtml');
105  $this->_block->setLayout(
106  \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
107  \Magento\Framework\View\LayoutInterface::class
108  )
109  );
110 
111  $html = $this->_block->toHtml();
112  $this->assertNotEmpty($html);
113  $this->assertContains('New Product', $html);
114  $this->assertInstanceOf(
115  \Magento\Catalog\Model\ResourceModel\Product\Collection::class,
116  $this->_block->getProductCollection()
117  );
118  }
119 
124  {
126  \Magento\Framework\View\LayoutInterface::class
127  )->createBlock(
128  \Magento\Catalog\Block\Product\Widget\NewWidget::class
129  );
130 
131  $requestParams = ['test' => 'data'];
132 
133  $block->getRequest()->setParams($requestParams);
134 
135  $info = $block->getCacheKeyInfo();
136 
137  $this->assertEquals('CATALOG_PRODUCT_NEW', $info[0]);
138  $this->assertEquals(json_encode($requestParams), $info[8]);
139  }
140 }
$block
Definition: block.php:8
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52