89 protected function setUp()
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'])
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);
108 $this->layoutMock = $this->createMock(\
Magento\Framework\
View\LayoutInterface::class);
109 $this->toolbarMock = $this->createMock(\
Magento\Catalog\Block\
Product\ProductList\Toolbar::class);
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);
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);
124 'registry' => $this->registryMock,
125 'context' => $this->context,
126 'layerResolver' => $layerResolver,
127 'cartHelper' => $this->cartHelperMock,
128 'postDataHelper' => $this->postDataHelperMock,
129 'urlHelper' => $this->urlHelperMock,
132 $this->block->setToolbarBlockName(
'mock');
142 $productTag =
'cat_p_1';
143 $categoryTag =
'cat_c_p_1';
145 $this->productMock->expects($this->once())
146 ->method(
'getIdentities')
147 ->will($this->returnValue([$productTag]));
149 $this->productMock->expects($this->once())
150 ->method(
'getCategoryCollection')
151 ->will($this->returnValue($this->catCollectionMock));
153 $this->catCollectionMock->expects($this->once())
155 ->will($this->returnValue($this->catCollectionMock));
157 $this->catCollectionMock->expects($this->once())
159 ->will($this->returnValue($this->catCollectionMock));
161 $this->catCollectionMock->expects($this->once())
163 ->will($this->returnValue(1));
165 $this->registryMock->expects($this->any())
167 ->will($this->returnValue($this->productMock));
169 $currentCategory = $this->createMock(\
Magento\Catalog\Model\Category::class);
170 $currentCategory->expects($this->any())
172 ->will($this->returnValue(
'1'));
174 $this->catCollectionMock->expects($this->once())
175 ->method(
'getIterator')
176 ->will($this->returnValue([$currentCategory]));
178 $this->prodCollectionMock->expects($this->any())
179 ->method(
'getIterator')
180 ->will($this->returnValue(
new \ArrayIterator([$this->productMock])));
182 $this->layerMock->expects($this->any())
183 ->method(
'getCurrentCategory')
184 ->will($this->returnValue($currentCategory));
186 $this->layerMock->expects($this->once())
187 ->method(
'getProductCollection')
188 ->will($this->returnValue($this->prodCollectionMock));
190 $this->layoutMock->expects($this->once())
192 ->will($this->returnValue($this->toolbarMock));
195 [$categoryTag, $productTag],
196 $this->block->getIdentities()
200 $this->block->getCategoryId()
206 $url =
'http://localhost.com/dev/';
208 $uenc = strtr(base64_encode(
$url),
'+/=',
'-_,');
209 $expectedPostData = [
211 'data' => [
'product' =>
$id,
'uenc' => $uenc],
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);
238 $this->renderer->expects($this->once())
240 ->with(
'is_product_list',
true)
242 $this->layoutMock->expects($this->once())
244 ->with(
'product.price.render.default')
245 ->willReturn($this->renderer);
247 $this->block->getProductPrice($this->productMock);
testGetAddToCartPostParams()
testSetIsProductListFlagOnGetProductPrice()