/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetProductCollection() { $storeId = 1; $categoryChildren = 'children'; $visibleInCatalogIds = 1; $this->visibility->expects($this->once())->method('getVisibleInCatalogIds')->will($this->returnValue($visibleInCatalogIds)); $products = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection', ['setStoreId', 'addAttributeToSort', 'setVisibility', 'setCurPage', 'setPageSize', 'addCountToCategories'], [], '', false); $resourceCollection = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Collection\\AbstractCollection', ['addAttributeToSelect', 'addAttributeToFilter', 'addIdFilter', 'load'], [], '', false); $resourceCollection->expects($this->exactly(3))->method('addAttributeToSelect')->will($this->returnSelf()); $resourceCollection->expects($this->once())->method('addAttributeToFilter')->will($this->returnSelf()); $resourceCollection->expects($this->once())->method('addIdFilter')->with($categoryChildren)->will($this->returnSelf()); $resourceCollection->expects($this->once())->method('load')->will($this->returnSelf()); $products->expects($this->once())->method('addCountToCategories')->with($resourceCollection); $products->expects($this->once())->method('addAttributeToSort')->with('updated_at', 'desc')->will($this->returnSelf()); $products->expects($this->once())->method('setVisibility')->with($visibleInCatalogIds)->will($this->returnSelf()); $products->expects($this->once())->method('setCurPage')->with(1)->will($this->returnSelf()); $products->expects($this->once())->method('setPageSize')->with(50)->will($this->returnSelf()); $products->expects($this->once())->method('setStoreId')->with($storeId); $this->collectionFactory->expects($this->once())->method('create')->will($this->returnValue($products)); $category = $this->getMock('Magento\\Catalog\\Model\\Category', ['getResourceCollection', 'getChildren', 'getProductCollection', '__wakeup'], [], '', false); $category->expects($this->once())->method('getResourceCollection')->will($this->returnValue($resourceCollection)); $category->expects($this->once())->method('getChildren')->will($this->returnValue($categoryChildren)); $category->expects($this->once())->method('getProductCollection')->will($this->returnValue($products)); $layer = $this->getMock('Magento\\Catalog\\Model\\Layer', ['setCurrentCategory', 'prepareProductCollection', 'getProductCollection', '__wakeup'], [], '', false); $layer->expects($this->once())->method('setCurrentCategory')->with($category)->will($this->returnSelf()); $layer->expects($this->once())->method('getProductCollection')->will($this->returnValue($products)); /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Layer\Resolver $layerResolver */ $layerResolver = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Layer\\Resolver')->disableOriginalConstructor()->setMethods(['get', 'create'])->getMock(); $layerResolver->expects($this->any())->method($this->anything())->will($this->returnValue($layer)); $this->categoryLayer->expects($this->once())->method('setStore')->with($storeId)->will($this->returnValue($layer)); $this->assertEquals($products, $this->model->getProductCollection($category, $storeId)); }
/** * Test public `createCollection` method and protected `getPageSize` method via `createCollection` * * @param bool $pagerEnable * @param int $productsCount * @param int $productsPerPage * @param int $expectedPageSize * @dataProvider createCollectionDataProvider */ public function testCreateCollection($pagerEnable, $productsCount, $productsPerPage, $expectedPageSize) { $this->visibility->expects($this->once())->method('getVisibleInCatalogIds')->willReturn([Visibility::VISIBILITY_IN_CATALOG, Visibility::VISIBILITY_BOTH]); $collection = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Resource\\Product\\Collection')->setMethods(['setVisibility', 'addMinimalPrice', 'addFinalPrice', 'addTaxPercents', 'addAttributeToSelect', 'addUrlRewrite', 'addStoreFilter', 'setPageSize', 'setCurPage'])->disableOriginalConstructor()->getMock(); $collection->expects($this->once())->method('setVisibility')->with([Visibility::VISIBILITY_IN_CATALOG, Visibility::VISIBILITY_BOTH])->willReturnSelf(); $collection->expects($this->once())->method('addMinimalPrice')->willReturnSelf(); $collection->expects($this->once())->method('addFinalPrice')->willReturnSelf(); $collection->expects($this->once())->method('addTaxPercents')->willReturnSelf(); $collection->expects($this->once())->method('addAttributeToSelect')->willReturnSelf(); $collection->expects($this->once())->method('addUrlRewrite')->willReturnSelf(); $collection->expects($this->once())->method('addStoreFilter')->willReturnSelf(); $collection->expects($this->once())->method('setPageSize')->with($expectedPageSize)->willReturnSelf(); $collection->expects($this->once())->method('setCurPage')->willReturnSelf(); $this->collectionFactory->expects($this->once())->method('create')->willReturn($collection); $this->productsList->setData('conditions_encoded', 'some_serialized_conditions'); $conditions = $this->getMockBuilder('\\Magento\\Rule\\Model\\Condition\\Combine')->setMethods(['collectValidatedAttributes'])->disableOriginalConstructor()->getMock(); $conditions->expects($this->once())->method('collectValidatedAttributes')->with($collection)->willReturnSelf(); $this->builder->expects($this->once())->method('attachConditionToCollection')->with($collection, $conditions)->willReturnSelf(); $this->rule->expects($this->once())->method('loadPost')->willReturnSelf(); $this->rule->expects($this->once())->method('getConditions')->willReturn($conditions); if ($productsPerPage) { $this->productsList->setData('products_per_page', $productsPerPage); } else { $this->productsList->unsetData('products_per_page'); } $this->productsList->setData('show_pager', $pagerEnable); $this->productsList->setData('products_count', $productsCount); $this->assertSame($collection, $this->productsList->createCollection()); }
public function testGetProductsCollection() { /** @var \DateTime|\PHPUnit_Framework_MockObject_MockObject $dateObject */ $dateObject = $this->getMock('DateTime'); $dateObject->expects($this->any())->method('setTime')->will($this->returnSelf()); $dateObject->expects($this->any())->method('format')->will($this->returnValue(date(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT))); $this->timezone->expects($this->exactly(2))->method('date')->will($this->returnValue($dateObject)); /** @var \Magento\Catalog\Model\Resource\Product\Collection $productCollection */ $productCollection = $this->getMock('Magento\\Catalog\\Model\\Resource\\Product\\Collection', [], [], '', false); $this->product->expects($this->once())->method('getResourceCollection')->will($this->returnValue($productCollection)); $storeId = 1; $productCollection->expects($this->once())->method('setStoreId')->with($storeId); $productCollection->expects($this->once())->method('addStoreFilter')->will($this->returnSelf()); $productCollection->expects($this->any())->method('addAttributeToFilter')->will($this->returnSelf()); $productCollection->expects($this->any())->method('addAttributeToSelect')->will($this->returnSelf()); $productCollection->expects($this->once())->method('addAttributeToSort')->will($this->returnSelf()); $productCollection->expects($this->once())->method('applyFrontendPriceLimitations')->will($this->returnSelf()); $visibleIds = [1, 3]; $this->visibility->expects($this->once())->method('getVisibleInCatalogIds')->will($this->returnValue($visibleIds)); $productCollection->expects($this->once())->method('setVisibility')->with($visibleIds)->will($this->returnSelf()); $products = $this->newProducts->getProductsCollection($storeId); $this->assertEquals($productCollection, $products); }