コード例 #1
0
 /**
  * @param \Magento\Catalog\Model\Category $category
  * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
  */
 public function getCollection(\Magento\Catalog\Model\Category $category)
 {
     /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
     $collection = $this->collectionFactory->create();
     $collection->addCategoryFilter($category);
     return $collection;
 }
 /**
  * Update product(s) status action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $collection = $this->filter->getCollection($this->collectionFactory->create());
     $productIds = $collection->getAllIds();
     $storeId = (int) $this->getRequest()->getParam('store', 0);
     $attributeSetId = (int) $this->getRequest()->getParam('changeattributeset');
     try {
         foreach ($collection->getItems() as $product) {
             //echo get_class($product->getTypeInstance());die();
             if ($this->validateConfigurable($product, $attributeSetId, $storeId) == false) {
                 $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                 return $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
                 break;
             }
             $product->setAttributeSetId($attributeSetId)->setStoreId($storeId);
         }
         $collection->save();
         //$product->getTypeInstance();
         $this->messageManager->addSuccess(__('A total of %1 record(s) have been updated.', count($productIds)));
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $this->messageManager->addError($e->getMessage());
     } catch (\Exception $e) {
         $this->_getSession()->addException($e, __('Something went wrong while updating the product(s) atrribute set.'));
     }
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     return $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
 }
コード例 #3
0
ファイル: Products.php プロジェクト: swissup/core
 /**
  * Enable requested attribute for products, if no products are using it
  *
  * Date attributes is supported for attribute_from values only.
  *
  * @param  array $data Key => Value pairs of attribute_code and count
  * @return void
  */
 public function execute($data)
 {
     $visibility = $this->catalogProductVisibility->getVisibleInCatalogIds();
     $attributes = $this->attributeCollectionFactory->create()->addFieldToFilter('attribute_code', ['in' => array_keys($data)]);
     foreach ($attributes as $attribute) {
         $collection = $this->productCollectionFactory->create()->setPageSize(1)->setCurPage(1);
         switch ($attribute->getFrontendInput()) {
             case 'boolean':
                 $value = 1;
                 $collection->addAttributeToFilter($attribute, 1);
                 break;
             case 'date':
                 $value = $this->localeDate->date()->format('Y-m-d H:i:s');
                 $collection->addAttributeToFilter($attribute, [['date' => true, 'to' => $value]]);
                 break;
         }
         if ($collection->getSize()) {
             // customer already has some products with specified attribute
             continue;
         }
         foreach ($this->getStoreIds() as $storeId) {
             $visibleProducts = $this->productCollectionFactory->create()->setStoreId($storeId)->setVisibility($visibility)->addStoreFilter($storeId)->setPageSize($data[$attribute->getAttributeCode()])->setCurPage(1);
             if (!$visibleProducts->getSize()) {
                 continue;
             }
             foreach ($visibleProducts as $product) {
                 $product->addAttributeUpdate($attribute->getAttributeCode(), (int) in_array(0, $this->getStoreIds()), 0);
                 $product->setStoreId($storeId)->setData($attribute->getAttributeCode(), $value)->save();
             }
         }
     }
 }
コード例 #4
0
 /**
  * Construct
  *
  * @param string $name
  * @param string $primaryFieldName
  * @param string $requestFieldName
  * @param CollectionFactory $collectionFactory
  * @param \Magento\Ui\DataProvider\AddFieldToCollectionInterface[] $addFieldStrategies
  * @param \Magento\Ui\DataProvider\AddFilterToCollectionInterface[] $addFilterStrategies
  * @param array $meta
  * @param array $data
  */
 public function __construct($name, $primaryFieldName, $requestFieldName, CollectionFactory $collectionFactory, array $addFieldStrategies = [], array $addFilterStrategies = [], array $meta = [], array $data = [])
 {
     parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
     $this->collection = $collectionFactory->create();
     $this->addFieldStrategies = $addFieldStrategies;
     $this->addFilterStrategies = $addFilterStrategies;
 }
コード例 #5
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->appState->setAreaCode('catalog');
     /** @var ProductCollection $productCollection */
     $productCollection = $this->productCollectionFactory->create();
     $productIds = $productCollection->getAllIds();
     if (!count($productIds)) {
         $output->writeln("<info>No product images to resize</info>");
         return;
     }
     try {
         foreach ($productIds as $productId) {
             try {
                 /** @var Product $product */
                 $product = $this->productRepository->getById($productId);
             } catch (NoSuchEntityException $e) {
                 continue;
             }
             /** @var ImageCache $imageCache */
             $imageCache = $this->imageCacheFactory->create();
             $imageCache->generate($product);
             $output->write(".");
         }
     } catch (\Exception $e) {
         $output->writeln("<error>{$e->getMessage()}</error>");
         return;
     }
     $output->write("\n");
     $output->writeln("<info>Product images resized successfully</info>");
 }
コード例 #6
0
ファイル: Msrp.php プロジェクト: kidaa30/magento2-platformsh
 /**
  * @param SampleDataContext $sampleDataContext
  * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
  * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
  */
 public function __construct(SampleDataContext $sampleDataContext, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Framework\App\Config\Storage\WriterInterface $configWriter)
 {
     $this->fixtureManager = $sampleDataContext->getFixtureManager();
     $this->csvReader = $sampleDataContext->getCsvReader();
     $this->productCollection = $productCollectionFactory->create()->addAttributeToSelect('sku');
     $this->configWriter = $configWriter;
 }
コード例 #7
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->appState->setAreaCode('catalog');
     /** @var ProductCollection $productCollection */
     $productCollection = $this->productCollectionFactory->create();
     $productIds = $productCollection->getAllIds();
     if (!count($productIds)) {
         $output->writeln("<info>No product images to resize</info>");
         // we must have an exit code higher than zero to indicate something was wrong
         return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
     }
     try {
         foreach ($productIds as $productId) {
             try {
                 /** @var Product $product */
                 $product = $this->productRepository->getById($productId);
             } catch (NoSuchEntityException $e) {
                 continue;
             }
             /** @var ImageCache $imageCache */
             $imageCache = $this->imageCacheFactory->create();
             $imageCache->generate($product);
             $output->write(".");
         }
     } catch (\Exception $e) {
         $output->writeln("<error>{$e->getMessage()}</error>");
         // we must have an exit code higher than zero to indicate something was wrong
         return \Magento\Framework\Console\Cli::RETURN_FAILURE;
     }
     $output->write("\n");
     $output->writeln("<info>Product images resized successfully</info>");
 }
コード例 #8
0
 protected function setUp()
 {
     $this->productCollection = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock();
     $this->productCollectionFactory = $this->getMockBuilder(CollectionFactory::class)->setMethods(['create'])->disableOriginalConstructor()->getMock();
     $this->productCollectionFactory->expects($this->once())->method('create')->willReturn($this->productCollection);
     $this->attributeSetFinder = (new ObjectManager($this))->getObject(AttributeSetFinder::class, ['productCollectionFactory' => $this->productCollectionFactory]);
 }
コード例 #9
0
 /**
  * @param \Magento\Catalog\Model\Category\TreeFactory $categoryTreeFactory
  * @param \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryResourceTreeFactory
  * @param \Magento\Eav\Model\Config $eavConfig
  * @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
  * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory
  * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
  * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
  */
 public function __construct(\Magento\Catalog\Model\Category\TreeFactory $categoryTreeFactory, \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryResourceTreeFactory, \Magento\Eav\Model\Config $eavConfig, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory, \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory)
 {
     $this->categoryTree = $categoryTreeFactory->create(['categoryTree' => $categoryResourceTreeFactory->create(), 'categoryCollection' => $categoryCollectionFactory->create()]);
     $this->eavConfig = $eavConfig;
     $this->attributeCollectionFactory = $attributeCollectionFactory;
     $this->attrOptionCollectionFactory = $attrOptionCollectionFactory;
     $this->productCollection = $productCollectionFactory->create();
 }
コード例 #10
0
 /**
  * {@inheritdoc}
  */
 public function findAttributeSetIdsByProductIds(array $productIds)
 {
     /** @var $collection Collection */
     $collection = $this->productCollectionFactory->create();
     $select = $collection->getSelect()->reset(Select::COLUMNS)->columns(ProductInterface::ATTRIBUTE_SET_ID)->where('entity_id IN (?)', $productIds)->group(ProductInterface::ATTRIBUTE_SET_ID);
     $result = $collection->getConnection()->fetchCol($select);
     return $result;
 }
コード例 #11
0
 /**
  * @return void
  */
 protected function setUp()
 {
     $this->objectManager = new ObjectManager($this);
     $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
     $this->collectionMock = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->setMethods(['toArray', 'isLoaded', 'addAttributeToFilter', 'load', 'getSize', 'addFilterByRequiredOptions', 'addStoreFilter'])->getMock();
     $this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock();
     $this->collectionFactoryMock->expects($this->any())->method('create')->willReturn($this->collectionMock);
     $this->dataHelperMock = $this->getMockBuilder(Data::class)->disableOriginalConstructor()->setMethods(['getAllowedSelectionTypes'])->getMock();
 }
コード例 #12
0
 /**
  * get featured product collection
  */
 public function getFeaturedProduct()
 {
     $limit = $this->getProductLimit();
     $collection = $this->_productCollectionFactory->create();
     $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
     $collection->addMinimalPrice()->addFinalPrice()->addTaxPercents()->setPageSize($limit)->addAttributeToSelect('*');
     $collection->addAttributeToFilter('et_featured', '1');
     return $collection;
 }
コード例 #13
0
 public function testGetDisabledCount()
 {
     $statusDisabled = 2;
     $productsMock = $this->getMock('\\Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection', [], [], '', false);
     $this->productsFactoryMock->expects($this->once())->method('create')->willReturn($productsMock);
     $productsMock->expects($this->once())->method('addAttributeToFilter')->with('status', $statusDisabled)->willReturnSelf();
     $productsMock->expects($this->once())->method('getSize')->willReturn('expected');
     $this->assertEquals('expected', $this->model->getCount($statusDisabled));
 }
コード例 #14
0
 protected function setUp()
 {
     $this->objectManager = new ObjectManager($this);
     $this->collectionMock = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock();
     $this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock();
     $this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->collectionMock);
     $this->poolMock = $this->getMockBuilder(Pool::class)->disableOriginalConstructor()->getMock();
     $this->modifierMockOne = $this->getMockBuilder(ModifierInterface::class)->setMethods(['getData', 'getMeta'])->getMockForAbstractClass();
     $this->model = $this->objectManager->getObject(ProductDataProvider::class, ['name' => 'testName', 'primaryFieldName' => 'testPrimaryFieldName', 'requestFieldName' => 'testRequestFieldName', 'collectionFactory' => $this->collectionFactoryMock, 'pool' => $this->poolMock]);
 }
コード例 #15
0
 protected function setUp()
 {
     $this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock();
     $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
     $this->collectionMock = $this->getMockBuilder(AbstractCollection::class)->disableOriginalConstructor()->setMethods(['load', 'getSelect', 'getTable', 'getIterator', 'isLoaded', 'toArray', 'getSize'])->getMockForAbstractClass();
     $this->dbSelectMock = $this->getMockBuilder(DbSelect::class)->disableOriginalConstructor()->getMock();
     $this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->collectionMock);
     $this->objectManagerHelper = new ObjectManagerHelper($this);
     $this->dataProvider = $this->objectManagerHelper->getObject(ProductCustomOptionsDataProvider::class, ['collectionFactory' => $this->collectionFactoryMock, 'request' => $this->requestMock]);
 }
コード例 #16
0
 /**
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $collection = $this->filter->getCollection($this->collectionFactory->create());
     $productDeleted = 0;
     foreach ($collection->getItems() as $product) {
         $product->delete();
         $productDeleted++;
     }
     $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $productDeleted));
     return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('catalog/*/index');
 }
コード例 #17
0
ファイル: Edit.php プロジェクト: nblair/magescotch
 /**
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function executeInternal()
 {
     $collection = $this->filter->getCollection($this->collectionFactory->create());
     $this->attributeHelper->setProductIds($collection->getAllIds());
     if (!$this->_validateProducts()) {
         return $this->resultRedirectFactory->create()->setPath('catalog/product/', ['_current' => true]);
     }
     $resultPage = $this->resultPageFactory->create();
     $resultPage->getConfig()->getTitle()->prepend(__('Update Attributes'));
     return $resultPage;
 }
コード例 #18
0
 /**
  * @return Collection
  */
 protected function _getProductCollection()
 {
     if ($this->_productCollection === null) {
         /** @var Collection $collection */
         $collection = $this->_productCollectionFactory->create();
         $this->_catalogLayer->prepareProductCollection($collection);
         $collection->addAttributeToFilter('promotion', 1)->addStoreFilter();
         $this->_productCollection = $collection;
     }
     return $this->_productCollection;
 }
コード例 #19
0
 /**
  * {@inheritdoc}
  */
 public function getProducts(ProductInterface $product)
 {
     if (!isset($this->products[$product->getId()])) {
         if ($this->requestSafety->isSafeMethod()) {
             $productIds = $this->resource->getConnection()->fetchCol('(' . implode(') UNION (', $this->linkedProductSelectBuilder->build($product->getId())) . ')');
             $this->products[$product->getId()] = $this->collectionFactory->create()->addAttributeToSelect(['price', 'special_price'])->addIdFilter($productIds);
         } else {
             $this->products[$product->getId()] = $this->configurable->getUsedProducts($product);
         }
     }
     return $this->products[$product->getId()];
 }
コード例 #20
0
 protected function setUp()
 {
     $this->objectManager = new ObjectManager($this);
     $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
     $this->productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class)->getMockForAbstractClass();
     $this->productLinkRepositoryMock = $this->getMockBuilder(ProductLinkRepositoryInterface::class)->getMockForAbstractClass();
     $this->productMock = $this->getMockBuilder(ProductInterface::class)->getMockForAbstractClass();
     $this->collectionMock = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock();
     $this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)->setMethods(['create'])->disableOriginalConstructor()->getMock();
     $this->productRepositoryMock->expects($this->any())->method('getById')->willReturn($this->productMock);
     $this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->collectionMock);
 }
コード例 #21
0
ファイル: Random.php プロジェクト: pradeep-wagento/magento2
 /**
  * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
  */
 protected function _getProductCollection()
 {
     if ($this->_productCollection === null) {
         /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
         $collection = $this->_productCollectionFactory->create();
         $this->_catalogLayer->prepareProductCollection($collection);
         $collection->getSelect()->order('rand()');
         $collection->addStoreFilter();
         $numProducts = $this->getNumProducts() ? $this->getNumProducts() : 0;
         $collection->setPage(1, $numProducts);
         $this->_productCollection = $collection;
     }
     return $this->_productCollection;
 }
コード例 #22
0
 /**
  * {@inheritdoc}
  */
 public function getCount($status = null)
 {
     $products = $this->productsFactory->create();
     /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $products */
     switch ($status) {
         case Status::STATUS_ENABLED:
             $products->addAttributeToFilter('status', Status::STATUS_ENABLED);
             break;
         case Status::STATUS_DISABLED:
             $products->addAttributeToFilter('status', Status::STATUS_DISABLED);
             break;
     }
     return $products->getSize();
 }
コード例 #23
0
ファイル: Category.php プロジェクト: pradeep-wagento/magento2
 /**
  * @param \Magento\Catalog\Model\Category $category
  * @param int $storeId
  * @return $this
  */
 public function getProductCollection(\Magento\Catalog\Model\Category $category, $storeId)
 {
     /** @var $layer \Magento\Catalog\Model\Layer */
     $layer = $this->catalogLayer->setStore($storeId);
     $collection = $category->getResourceCollection();
     $collection->addAttributeToSelect('url_key')->addAttributeToSelect('name')->addAttributeToSelect('is_anchor')->addAttributeToFilter('is_active', 1)->addIdFilter($category->getChildren())->load();
     /** @var $productCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */
     $productCollection = $this->collectionFactory->create();
     $currentCategory = $layer->setCurrentCategory($category);
     $layer->prepareProductCollection($productCollection);
     $productCollection->addCountToCategories($collection);
     $category->getProductCollection()->setStoreId($storeId);
     $products = $currentCategory->getProductCollection()->addAttributeToSort('updated_at', 'desc')->setVisibility($this->visibility->getVisibleInCatalogIds())->setCurPage(1)->setPageSize(50);
     return $products;
 }
コード例 #24
0
 /**
  * Premare block data
  * @return $this
  */
 protected function _prepareCollection()
 {
     $post = $this->_coreRegistry->registry('current_blog_post');
     $this->_itemCollection = $this->_productCollectionFactory->create()->addAttributeToSelect('required_options')->addStoreFilter()->addAttributeToFilter('entity_id', array('in' => $post->getRelatedProductIds() ?: array(0)));
     if ($this->_moduleManager->isEnabled('Magento_Checkout')) {
         $this->_addProductAttributesAndPrices($this->_itemCollection);
     }
     $this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
     $this->_itemCollection->setPageSize((int) $this->_scopeConfig->getValue('mfblog/post_view/related_products/number_of_products', \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
     $this->_itemCollection->load();
     foreach ($this->_itemCollection as $product) {
         $product->setDoNotUseCategoryId(true);
     }
     return $this;
 }
コード例 #25
0
 /**
  * {@inheritdoc}
  */
 public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
 {
     /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
     $collection = $this->collectionFactory->create();
     $this->extensionAttributesJoinProcessor->process($collection);
     foreach ($this->metadataService->getList($this->searchCriteriaBuilder->create())->getItems() as $metadata) {
         $collection->addAttributeToSelect($metadata->getAttributeCode());
     }
     $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
     $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
     //Add filters from root filter group to the collection
     foreach ($searchCriteria->getFilterGroups() as $group) {
         $this->addFilterGroupToCollection($group, $collection);
     }
     /** @var SortOrder $sortOrder */
     foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) {
         $field = $sortOrder->getField();
         $collection->addOrder($field, $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC');
     }
     $collection->setCurPage($searchCriteria->getCurrentPage());
     $collection->setPageSize($searchCriteria->getPageSize());
     $collection->load();
     $searchResult = $this->searchResultsFactory->create();
     $searchResult->setSearchCriteria($searchCriteria);
     $searchResult->setItems($collection->getItems());
     $searchResult->setTotalCount($collection->getSize());
     return $searchResult;
 }
コード例 #26
0
ファイル: Post.php プロジェクト: mirasvit/module-blog
 /**
  * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
  */
 public function getRelatedProducts()
 {
     $ids = $this->getRelatedProductIds();
     $ids[] = 0;
     $collection = $this->productCollectionFactory->create()->addAttributeToSelect('*')->addFieldToFilter('entity_id', $ids);
     return $collection;
 }
コード例 #27
0
 /**
  * Prepare collection
  *
  * @return Extended
  */
 protected function _prepareCollection()
 {
     $post = $this->getPost();
     $collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*')->joinField('websites', 'catalog_product_website', 'website_id', 'product_id=entity_id', null, 'left');
     $this->setCollection($collection);
     return parent::_prepareCollection();
 }
コード例 #28
0
ファイル: Sku.php プロジェクト: pradeep-wagento/magento2
 /**
  * Get catalog product resource collection instance
  *
  * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
  */
 protected function _getCpCollectionInstance()
 {
     if (!$this->_cpCollectionInstance) {
         $this->_cpCollectionInstance = $this->_cpCollection->create();
     }
     return $this->_cpCollectionInstance;
 }
コード例 #29
0
 /**
  * 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\\ResourceModel\\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());
 }
コード例 #30
0
ファイル: EditTest.php プロジェクト: Doability/magento2dev
 public function testExecutePageRequested()
 {
     $this->request->expects($this->any())->method('getParam')->with('filters')->willReturn(['placeholder' => true]);
     $this->request->expects($this->any())->method('getParams')->willReturn(['namespace' => 'product_listing', 'exclude' => true, 'filters' => ['placeholder' => true]]);
     $this->attributeHelper->expects($this->any())->method('getProductIds')->willReturn([1, 2, 3]);
     $this->attributeHelper->expects($this->any())->method('setProductIds')->with([1, 2, 3]);
     $collection = $this->getMockBuilder('Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection')->setMethods(['getAllIds'])->disableOriginalConstructor()->getMock();
     $collection->expects($this->any())->method('getAllIds')->willReturn([1, 2, 3]);
     $this->filter->expects($this->any())->method('getCollection')->with($collection)->willReturn($collection);
     $this->collectionFactory->expects($this->any())->method('create')->willReturn($collection);
     $title = $this->getMockBuilder('Magento\\Framework\\View\\Page\\Title')->setMethods(['prepend'])->disableOriginalConstructor()->getMock();
     $config = $this->getMockBuilder('Magento\\Framework\\View\\Page\\Config')->setMethods(['getTitle'])->disableOriginalConstructor()->getMock();
     $config->expects($this->any())->method('getTitle')->willReturn($title);
     $this->resultPage->expects($this->any())->method('getConfig')->willReturn($config);
     $this->assertSame($this->resultPage, $this->object->execute());
 }