Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ListProductTest.php
Go to the documentation of this file.
1 <?php
7 
13 
17 class ListProductTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $block;
23 
27  protected $registryMock;
28 
32  protected $layerMock;
33 
38 
42  protected $productMock;
43 
47  protected $cartHelperMock;
48 
52  protected $typeInstanceMock;
53 
57  protected $urlHelperMock;
58 
62  protected $catCollectionMock;
63 
68 
72  protected $layoutMock;
73 
77  protected $toolbarMock;
78 
82  private $context;
83 
87  private $renderer;
88 
89  protected function setUp()
90  {
91  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
92  $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
93  $this->layerMock = $this->createMock(\Magento\Catalog\Model\Layer::class);
95  $layerResolver = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Resolver::class)
96  ->disableOriginalConstructor()
97  ->setMethods(['get', 'create'])
98  ->getMock();
99  $layerResolver->expects($this->any())
100  ->method($this->anything())
101  ->will($this->returnValue($this->layerMock));
102  $this->postDataHelperMock = $this->createMock(\Magento\Framework\Data\Helper\PostHelper::class);
103  $this->typeInstanceMock = $this->createMock(\Magento\Catalog\Model\Product\Type\Simple::class);
104  $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
105  $this->cartHelperMock = $this->createMock(\Magento\Checkout\Helper\Cart::class);
106  $this->catCollectionMock = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category\Collection::class);
107  $this->prodCollectionMock = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);
108  $this->layoutMock = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
109  $this->toolbarMock = $this->createMock(\Magento\Catalog\Block\Product\ProductList\Toolbar::class);
110 
111  $this->urlHelperMock = $this->getMockBuilder(Data::class)->disableOriginalConstructor()->getMock();
112  $this->context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
113  $this->renderer = $this->getMockBuilder(Render::class)->disableOriginalConstructor()->getMock();
114  $eventManager = $this->getMockForAbstractClass(ManagerInterface::class, [], '', false);
115 
116  $this->context->expects($this->any())->method('getRegistry')->willReturn($this->registryMock);
117  $this->context->expects($this->any())->method('getCartHelper')->willReturn($this->cartHelperMock);
118  $this->context->expects($this->any())->method('getLayout')->willReturn($this->layoutMock);
119  $this->context->expects($this->any())->method('getEventManager')->willReturn($eventManager);
120 
121  $this->block = $objectManager->getObject(
122  \Magento\Catalog\Block\Product\ListProduct::class,
123  [
124  'registry' => $this->registryMock,
125  'context' => $this->context,
126  'layerResolver' => $layerResolver,
127  'cartHelper' => $this->cartHelperMock,
128  'postDataHelper' => $this->postDataHelperMock,
129  'urlHelper' => $this->urlHelperMock,
130  ]
131  );
132  $this->block->setToolbarBlockName('mock');
133  }
134 
135  protected function tearDown()
136  {
137  $this->block = null;
138  }
139 
140  public function testGetIdentities()
141  {
142  $productTag = 'cat_p_1';
143  $categoryTag = 'cat_c_p_1';
144 
145  $this->productMock->expects($this->once())
146  ->method('getIdentities')
147  ->will($this->returnValue([$productTag]));
148 
149  $this->productMock->expects($this->once())
150  ->method('getCategoryCollection')
151  ->will($this->returnValue($this->catCollectionMock));
152 
153  $this->catCollectionMock->expects($this->once())
154  ->method('load')
155  ->will($this->returnValue($this->catCollectionMock));
156 
157  $this->catCollectionMock->expects($this->once())
158  ->method('setPage')
159  ->will($this->returnValue($this->catCollectionMock));
160 
161  $this->catCollectionMock->expects($this->once())
162  ->method('count')
163  ->will($this->returnValue(1));
164 
165  $this->registryMock->expects($this->any())
166  ->method('registry')
167  ->will($this->returnValue($this->productMock));
168 
169  $currentCategory = $this->createMock(\Magento\Catalog\Model\Category::class);
170  $currentCategory->expects($this->any())
171  ->method('getId')
172  ->will($this->returnValue('1'));
173 
174  $this->catCollectionMock->expects($this->once())
175  ->method('getIterator')
176  ->will($this->returnValue([$currentCategory]));
177 
178  $this->prodCollectionMock->expects($this->any())
179  ->method('getIterator')
180  ->will($this->returnValue(new \ArrayIterator([$this->productMock])));
181 
182  $this->layerMock->expects($this->any())
183  ->method('getCurrentCategory')
184  ->will($this->returnValue($currentCategory));
185 
186  $this->layerMock->expects($this->once())
187  ->method('getProductCollection')
188  ->will($this->returnValue($this->prodCollectionMock));
189 
190  $this->layoutMock->expects($this->once())
191  ->method('getBlock')
192  ->will($this->returnValue($this->toolbarMock));
193 
194  $this->assertEquals(
195  [$categoryTag, $productTag],
196  $this->block->getIdentities()
197  );
198  $this->assertEquals(
199  '1',
200  $this->block->getCategoryId()
201  );
202  }
203 
204  public function testGetAddToCartPostParams()
205  {
206  $url = 'http://localhost.com/dev/';
207  $id = 1;
208  $uenc = strtr(base64_encode($url), '+/=', '-_,');
209  $expectedPostData = [
210  'action' => $url,
211  'data' => ['product' => $id, 'uenc' => $uenc],
212  ];
213 
214  $this->typeInstanceMock->expects($this->once())
215  ->method('isPossibleBuyFromList')
216  ->with($this->equalTo($this->productMock))
217  ->will($this->returnValue(true));
218  $this->cartHelperMock->expects($this->any())
219  ->method('getAddUrl')
220  ->with($this->equalTo($this->productMock), $this->equalTo([]))
221  ->will($this->returnValue($url));
222  $this->productMock->expects($this->once())
223  ->method('getEntityId')
224  ->will($this->returnValue($id));
225  $this->productMock->expects($this->once())
226  ->method('getTypeInstance')
227  ->will($this->returnValue($this->typeInstanceMock));
228  $this->urlHelperMock->expects($this->once())
229  ->method('getEncodedUrl')
230  ->with($this->equalTo($url))
231  ->will($this->returnValue($uenc));
232  $result = $this->block->getAddToCartPostParams($this->productMock);
233  $this->assertEquals($expectedPostData, $result);
234  }
235 
237  {
238  $this->renderer->expects($this->once())
239  ->method('setData')
240  ->with('is_product_list', true)
241  ->willReturnSelf();
242  $this->layoutMock->expects($this->once())
243  ->method('getBlock')
244  ->with('product.price.render.default')
245  ->willReturn($this->renderer);
246 
247  $this->block->getProductPrice($this->productMock);
248  }
249 }
$objectManager
Definition: bootstrap.php:17
$id
Definition: fieldset.phtml:14