/** * @param array $attributes * @param array $values * @param string $currentCurrencyCode * @param string $baseCurrencyCode * @dataProvider addFiltersDataProvider */ public function testAddFiltersVerifyAddConditionsToRegistry(array $attributes, array $values, $currentCurrencyCode = 'GBP', $baseCurrencyCode = 'USD') { $registry = new \Magento\Framework\Registry(); $this->collection->expects($this->any())->method('addAttributeToSelect')->will($this->returnSelf()); $this->collection->expects($this->any())->method('setStore')->will($this->returnSelf()); $this->collection->expects($this->any())->method('addMinimalPrice')->will($this->returnSelf()); $this->collection->expects($this->any())->method('addTaxPercents')->will($this->returnSelf()); $this->collection->expects($this->any())->method('addStoreFilter')->will($this->returnSelf()); $this->collection->expects($this->any())->method('setVisibility')->will($this->returnSelf()); $this->resource->expects($this->any())->method('prepareCondition')->will($this->returnValue(['like' => '%simple%'])); $this->resource->expects($this->any())->method('getIdFieldName')->will($this->returnValue('entity_id')); $this->dataCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator($attributes))); $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $advancedFactory = $this->getMockBuilder('Magento\\CatalogSearch\\Model\\ResourceModel\\AdvancedFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $advancedFactory->expects($this->once())->method('create')->willReturn($this->resource); $productCollectionFactory = $this->getMockBuilder('Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $productCollectionFactory->expects($this->any())->method('create')->willReturn($this->collection); $this->store->expects($this->any())->method('getCurrentCurrencyCode')->willReturn($currentCurrencyCode); $this->store->expects($this->any())->method('getBaseCurrencyCode')->willReturn($baseCurrencyCode); $this->currency->expects($this->any())->method('getRate')->with($currentCurrencyCode)->willReturn(1.5); $currency = $this->getMockBuilder('Magento\\Directory\\Model\\Currency')->setMethods(['load', 'format'])->disableOriginalConstructor()->getMock(); $currency->expects($this->any())->method('load')->willReturnSelf(); $currency->expects($this->any())->method('format')->willReturnArgument(0); $currencyFactory = $this->getMockBuilder('Magento\\Directory\\Model\\CurrencyFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $currencyFactory->expects($this->any())->method('create')->willReturn($currency); /** @var \Magento\CatalogSearch\Model\Advanced $instance */ $instance = $objectManager->getObject('Magento\\CatalogSearch\\Model\\Advanced', ['registry' => $registry, 'resourceProvider' => $this->resourceProvider, 'data' => ['attributes' => $this->dataCollection], 'advancedFactory' => $advancedFactory, 'productCollectionFactory' => $productCollectionFactory, 'storeManager' => $this->storeManager, 'currencyFactory' => $currencyFactory]); $instance->addFilters($values); $this->assertNotNull($registry->registry('advanced_search_conditions')); }
public function testAddFiltersVerifyAddConditionsToRegistry() { $registry = new \Magento\Framework\Registry(); $values = ['sku' => 'simple']; $this->resourceProvider->expects($this->once())->method('getResource')->willReturn($this->resource); $this->resourceProvider->expects($this->once())->method('getResourceCollection')->willReturn($this->collection); $this->resourceProvider->expects($this->once())->method('getAdvancedResultCollection')->willReturn($this->collection); $this->skuAttribute->expects($this->once())->method('getTable')->will($this->returnValue('catalog_product_entity')); $this->collection->expects($this->any())->method('addAttributeToSelect')->will($this->returnSelf()); $this->collection->expects($this->any())->method('setStore')->will($this->returnSelf()); $this->collection->expects($this->any())->method('addMinimalPrice')->will($this->returnSelf()); $this->collection->expects($this->any())->method('addTaxPercents')->will($this->returnSelf()); $this->collection->expects($this->any())->method('addStoreFilter')->will($this->returnSelf()); $this->collection->expects($this->any())->method('setVisibility')->will($this->returnSelf()); $this->resource->expects($this->any())->method('prepareCondition')->will($this->returnValue(['like' => '%simple%'])); $this->resource->expects($this->any())->method('getIdFieldName')->will($this->returnValue('entity_id')); $this->attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue('sku')); $this->attribute->expects($this->any())->method('getStoreLabel')->will($this->returnValue('SKU')); $this->attribute->expects($this->any())->method('getFrontendInput')->will($this->returnValue('text')); $this->attribute->expects($this->any())->method('getBackend')->will($this->returnValue($this->skuAttribute)); $this->attribute->expects($this->any())->method('getBackendType')->will($this->returnValue('static')); $this->dataCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$this->attribute]))); $objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); /** @var \Magento\CatalogSearch\Model\Advanced $instance */ $instance = $objectManager->getObject('Magento\\CatalogSearch\\Model\\Advanced', ['registry' => $registry, 'resourceProvider' => $this->resourceProvider, 'data' => ['attributes' => $this->dataCollection]]); $instance->addFilters($values); $this->assertNotNull($registry->registry('advanced_search_conditions')); }
/** * Update specified argument * * @param \Magento\Framework\Data\Collection $argument * @return \Magento\Framework\Data\Collection */ public function update($argument) { $calls = $argument->getUpdaterCall(); $calls[] = 'updater call'; $argument->setUpdaterCall($calls); return $argument; }
/** * Apply term filter * * @param \Magento\Framework\Data\Collection $collection The collection * @param \Magento\Backend\Block\Widget\Grid\Column $column Columns to filter */ protected function filterTermsCondition($collection, $column) { if (!($value = $column->getFilter()->getValue())) { return; } $collection->setTermFilter($value); }
/** * {@inheritdoc} */ public function addFilter(Collection $collection, $field, $condition = null) { if (isset($condition['eq']) && $condition['eq']) { /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */ $collection->addStoreFilter($this->storeManager->getStore($condition['eq'])); } }
/** * {@inheritdoc} */ public function addFilter(Collection $collection, $field, $condition = null) { if (isset($condition['from'])) { $collection->getSelect()->where(AbstractCollection::ATTRIBUTE_TABLE_ALIAS_PREFIX . 'qty.qty >= ?', (double) $condition['from']); } if ($condition['to']) { $collection->getSelect()->where(AbstractCollection::ATTRIBUTE_TABLE_ALIAS_PREFIX . 'qty.qty <= ?', (double) $condition['to']); } }
/** * Add items to interval collection * * @param Collection $collection * @param string $from * @param string $to * @param string $periodType * @return void */ public function prepareIntervalsCollection($collection, $from, $to, $periodType = self::REPORT_PERIOD_TYPE_DAY) { $intervals = $this->getIntervals($from, $to, $periodType); foreach ($intervals as $interval) { $item = $this->_itemFactory->create(); $item->setPeriod($interval); $item->setIsEmpty(); $collection->addItem($item); } }
/** * Apply fulltext filters * * @param Collection $collection * @param Filter $filter * @return void */ public function apply(Collection $collection, Filter $filter) { if (!$collection instanceof AbstractDb) { throw new \InvalidArgumentException('Database collection required.'); } /** @var SearchResult $collection */ $mainTable = $collection->getMainTable(); $columns = $this->getFulltextIndexColumns($collection, $mainTable); if (!$columns) { return; } $columns = $this->addTableAliasToColumns($columns, $collection, $mainTable); $collection->getSelect()->where('MATCH(' . implode(',', $columns) . ') AGAINST(?)', $filter->getValue()); }
public function testGenerate() { $imageFile = 'image.jpg'; $imageItem = $this->objectManager->getObject('Magento\\Framework\\Object', ['data' => ['file' => $imageFile]]); $this->mediaGalleryCollection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$imageItem])); $this->product->expects($this->any())->method('getMediaGalleryImages')->willReturn($this->mediaGalleryCollection); $this->config->expects($this->once())->method('getVars')->with('Magento_Catalog')->willReturn($this->getTestData()); $this->viewConfig->expects($this->once())->method('getViewConfig')->with(['area' => Area::AREA_FRONTEND, 'themeModel' => 'Magento\\theme'])->willReturn($this->config); $this->themeCollection->expects($this->once())->method('loadRegisteredThemes')->willReturn(['Magento\\theme']); $this->imageHelper->expects($this->exactly(3))->method('init')->will($this->returnValueMap([[$this->product, 'image', $imageFile, $this->imageHelper], [$this->product, 'small_image', $imageFile, $this->imageHelper], [$this->product, 'thumbnail', $imageFile, $this->imageHelper]])); $this->imageHelper->expects($this->exactly(3))->method('resize')->will($this->returnValueMap([[300, 300, $this->imageHelper], [200, 200, $this->imageHelper], [100, 100, $this->imageHelper]])); $this->imageHelper->expects($this->exactly(3))->method('save')->will($this->returnSelf()); $this->model->generate($this->product); }
/** * Constructor * * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Magento\Framework\Config\ThemeFactory $themeConfigFactory * @param ThemePackageList $themePackageList * @param ReadFactory $dirReadFactory */ public function __construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Magento\Framework\Config\ThemeFactory $themeConfigFactory, ThemePackageList $themePackageList, ReadFactory $dirReadFactory) { parent::__construct($entityFactory); $this->themeConfigFactory = $themeConfigFactory; $this->themePackageList = $themePackageList; $this->dirReadFactory = $dirReadFactory; }
/** * @param EntityFactoryInterface $entityFactory * @param FilterBuilder $filterBuilder * @param SearchCriteriaBuilder $searchCriteriaBuilder * @param \Magento\Framework\Api\SortOrderBuilder $sortOrderBuilder */ public function __construct(EntityFactoryInterface $entityFactory, FilterBuilder $filterBuilder, SearchCriteriaBuilder $searchCriteriaBuilder, SortOrderBuilder $sortOrderBuilder) { parent::__construct($entityFactory); $this->filterBuilder = $filterBuilder; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->sortOrderBuilder = $sortOrderBuilder; }
public function testPossibleFlowWithItem() { $firstItemMock = $this->getMock('Magento\\Framework\\Object', [], [], '', false); $secondItemMock = $this->getMock('Magento\\Framework\\Object', [], [], '', false); $requiredFields = ['required_field_one', 'required_field_two']; $arrItems = ['totalRecords' => 1, 'items' => [0 => 'value']]; $items = ['item_id' => $firstItemMock, 0 => $secondItemMock]; $firstItemMock->expects($this->exactly(2))->method('getId')->will($this->returnValue('item_id')); $firstItemMock->expects($this->atLeastOnce())->method('getData')->with('colName')->will($this->returnValue('first_value')); $secondItemMock->expects($this->atLeastOnce())->method('getData')->with('colName')->will($this->returnValue('second_value')); $firstItemMock->expects($this->once())->method('toArray')->with($requiredFields)->will($this->returnValue('value')); /** add items and set them values */ $this->_model->addItem($firstItemMock); $this->assertEquals($arrItems, $this->_model->toArray($requiredFields)); $this->_model->addItem($secondItemMock); $this->_model->setDataToAll('column', 'value'); /** get items by column name */ $this->assertEquals(['first_value', 'second_value'], $this->_model->getColumnValues('colName')); $this->assertEquals([$secondItemMock], $this->_model->getItemsByColumnValue('colName', 'second_value')); $this->assertEquals($firstItemMock, $this->_model->getItemByColumnValue('colName', 'second_value')); $this->assertEquals([], $this->_model->getItemsByColumnValue('colName', 'non_existing_value')); $this->assertEquals(null, $this->_model->getItemByColumnValue('colName', 'non_existing_value')); /** get items */ $this->assertEquals(['item_id', 0], $this->_model->getAllIds()); $this->assertEquals($firstItemMock, $this->_model->getFirstItem()); $this->assertEquals($secondItemMock, $this->_model->getLastItem()); $this->assertEquals($items, $this->_model->getItems('item_id')); /** remove existing items */ $this->assertNull($this->_model->getItemById('not_existing_item_id')); $this->_model->removeItemByKey('item_id'); $this->assertEquals([$secondItemMock], $this->_model->getItems()); $this->_model->removeAllItems(); $this->assertEquals([], $this->_model->getItems()); }
/** * Collection constructor * * @param EntityFactoryInterface $entityFactory * @param ScopeTreeProviderInterface $scopeTree * @param MetadataProviderInterface $metadataProvider * @param ScopeConfigInterface $appConfig * @param ValueProcessor $valueProcessor */ public function __construct(EntityFactoryInterface $entityFactory, ScopeTreeProviderInterface $scopeTree, MetadataProviderInterface $metadataProvider, ScopeConfigInterface $appConfig, ValueProcessor $valueProcessor) { parent::__construct($entityFactory); $this->scopeTree = $scopeTree; $this->metadataProvider = $metadataProvider; $this->appConfig = $appConfig; $this->valueProcessor = $valueProcessor; }
/** * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Magento\Framework\Filesystem $filesystem * @param \Magento\Framework\Config\ThemeFactory $themeConfigFactory */ public function __construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Config\ThemeFactory $themeConfigFactory, \Magento\Framework\Module\ThemeDir $themeDirs) { parent::__construct($entityFactory); $this->_directory = $filesystem->getDirectoryRead(DirectoryList::THEMES); $this->rootDirectory = $filesystem->getDirectoryRead(DirectoryList::ROOT); $this->themeConfigFactory = $themeConfigFactory; $this->themeDirs = $themeDirs; }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGenerate() { $imageFile = 'image.jpg'; $imageItem = $this->objectManager->getObject('Magento\\Framework\\DataObject', ['data' => ['file' => $imageFile]]); $this->mediaGalleryCollection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$imageItem])); $this->product->expects($this->any())->method('getMediaGalleryImages')->willReturn($this->mediaGalleryCollection); $data = $this->getTestData(); $this->config->expects($this->once())->method('getMediaEntities')->with('Magento_Catalog')->willReturn($data); $themeMock = $this->getMockBuilder('Magento\\Theme\\Model\\Theme')->disableOriginalConstructor()->getMock(); $themeMock->expects($this->exactly(3))->method('getCode')->willReturn('Magento\\theme'); $this->themeCollection->expects($this->once())->method('loadRegisteredThemes')->willReturn([$themeMock]); $this->viewConfig->expects($this->once())->method('getViewConfig')->with(['area' => Area::AREA_FRONTEND, 'themeModel' => $themeMock])->willReturn($this->config); $this->imageHelper->expects($this->exactly(3))->method('init')->will($this->returnValueMap([[$this->product, 'product_image', $this->getImageData('product_image'), $this->imageHelper], [$this->product, 'product_small_image', $this->getImageData('product_small_image'), $this->imageHelper], [$this->product, 'product_thumbnail', $this->getImageData('product_thumbnail'), $this->imageHelper]])); $this->imageHelper->expects($this->exactly(3))->method('setImageFile')->with($imageFile)->willReturnSelf(); $this->imageHelper->expects($this->any())->method('keepAspectRatio')->with($data['product_image']['aspect_ratio'])->willReturnSelf(); $this->imageHelper->expects($this->any())->method('keepFrame')->with($data['product_image']['frame'])->willReturnSelf(); $this->imageHelper->expects($this->any())->method('keepTransparency')->with($data['product_image']['transparency'])->willReturnSelf(); $this->imageHelper->expects($this->any())->method('constrainOnly')->with($data['product_image']['constrain'])->willReturnSelf(); $this->imageHelper->expects($this->any())->method('backgroundColor')->with($data['product_image']['background'])->willReturnSelf(); $this->imageHelper->expects($this->exactly(3))->method('save')->will($this->returnSelf()); $this->model->generate($this->product); }
/** * Load collection * * @param bool $printQuery * @param bool $logQuery * @return $this */ public function load($printQuery = false, $logQuery = false) { if ($this->isLoaded()) { return $this; } parent::load($printQuery, $logQuery); $this->_setIsLoaded(); if ($this->_columnGroupBy !== null) { $this->_mergeWithEmptyData(); $this->_groupResourceData(); } return $this; }
public function getIterator() { $this->setPageSize(10); // hard coded here but could listen to configuration or request $iterator = parent::getIterator(); if (FALSE === ($size = $this->getPageSize())) { return $iterator; } $page = $this->getRequest()->getParam('p', 1); if ($page < 1) { return $iterator; } $offset = $size * $page - $size; return new \LimitIterator($iterator, $offset, $size); }
/** * Overridden to use _idFieldName by default. * * @param string $valueField * @param string $labelField * @return array */ protected function _toOptionHash($valueField = null, $labelField = 'name') { if ($valueField === null) { $valueField = $this->getIdFieldName(); } return parent::_toOptionHash($valueField, $labelField); }
/** * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate * @param \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $collectionFactory */ public function __construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $collectionFactory) { $this->_localeDate = $localeDate; $this->_collectionFactory = $collectionFactory; parent::__construct($entityFactory); }
/** * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory * @param \Magento\Framework\Mview\ConfigInterface $config * @param State\CollectionFactory $statesFactory */ public function __construct(\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Magento\Framework\Mview\ConfigInterface $config, \Magento\Framework\Mview\View\State\CollectionFactory $statesFactory) { $this->config = $config; $this->statesFactory = $statesFactory; parent::__construct($entityFactory); }
/** * Set collection for pagination * * @param \Magento\Framework\Data\Collection $collection * @return $this */ public function setCollection($collection) { $this->_collection = $collection->setCurPage($this->getCurrentPage()); // If not int - then not limit if ((int) $this->getLimit()) { $this->_collection->setPageSize($this->getLimit()); } $this->_setFrameInitialized(false); return $this; }
/** * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate * @param \Magento\Reports\Model\FlagFactory $reportsFlagFactory */ public function __construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Reports\Model\FlagFactory $reportsFlagFactory) { parent::__construct($entityFactory); $this->_localeDate = $localeDate; $this->_reportsFlagFactory = $reportsFlagFactory; }
/** * @param \Magento\Core\Model\EntityFactory $entityFactory * @param \Magento\Framework\Filesystem $filesystem */ public function __construct(\Magento\Core\Model\EntityFactory $entityFactory, \Magento\Framework\Filesystem $filesystem) { parent::__construct($entityFactory); $this->_directory = $filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem::THEMES_DIR); }
/** * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList */ public function __construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList) { $this->_cacheTypeList = $cacheTypeList; parent::__construct($entityFactory); }
/** * Retrieve collection page size * * @return int */ public function getPageSize() { $pageSize = parent::getPageSize(); return $pageSize === null ? static::TRANSACTION_MAXIMUM_COUNT : $pageSize; }
/** * {@inheritdoc} */ public function addField(Collection $collection, $field, $alias = null) { $collection->joinField('qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'); }
/** * Clean up attribute collection * * @param \Magento\Framework\Data\Collection $collection * @return \Magento\Framework\Data\Collection */ public function filterAttributeCollection(\Magento\Framework\Data\Collection $collection) { /** @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */ foreach ($collection as $attribute) { if (in_array($attribute->getAttributeCode(), $this->_disabledAttributes)) { $collection->removeItemByKey($attribute->getId()); } } return $collection; }
/** * @param \Magento\Core\Model\EntityFactory $entityFactory * @param \Magento\Framework\Stdlib\DateTime $dateTime */ public function __construct(\Magento\Core\Model\EntityFactory $entityFactory, \Magento\Framework\Stdlib\DateTime $dateTime) { $this->dateTime = $dateTime; parent::__construct($entityFactory); }
/** * {@inheritdoc} */ public function addField(Collection $collection, $field, $alias = null) { $collection->addFieldToSelect($field, $alias); }
/** * {@inheritdoc} */ public function addField(Collection $collection, $field, $alias = null) { $collection->addWebsiteNamesToResult(); }