protected function setUp() { $this->entityFactoryMock = $this->getMock('Magento\\Framework\\Data\\Collection\\EntityFactory', [], [], '', false); $this->loggerMock = $this->getMock('Psr\\Log\\LoggerInterface'); $this->fetchStrategyMock = $this->getMock('Magento\\Framework\\Data\\Collection\\Db\\FetchStrategyInterface'); $this->managerInterfaceMock = $this->getMock('Magento\\Framework\\Event\\ManagerInterface'); $this->configMock = $this->getMock('Magento\\Eav\\Model\\Config', [], [], '', false); $this->resourceMock = $this->getMock('Magento\\Framework\\App\\Resource', [], [], '', false); $this->entityFactoryMock2 = $this->getMock('Magento\\Eav\\Model\\EntityFactory', [], [], '', false); $this->helperMock = $this->getMock('Magento\\Catalog\\Model\\Resource\\Helper', [], [], '', false); $entity = $this->getMock('Magento\\Eav\\Model\\Entity\\AbstractEntity', [], [], '', false); $adapter = $this->getMockForAbstractClass('Zend_Db_Adapter_Abstract', [], '', false); $entity->expects($this->any())->method('getReadConnection')->will($this->returnValue($adapter)); $entity->expects($this->any())->method('getDefaultAttributes')->will($this->returnValue([])); $this->universalFactoryMock = $this->getMock('Magento\\Framework\\Validator\\UniversalFactory', [], [], '', false); $this->universalFactoryMock->expects($this->any())->method('create')->will($this->returnValue($entity)); $this->storeManagerMock = $this->getMockForAbstractClass('Magento\\Store\\Model\\StoreManagerInterface'); $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnCallback(function ($store) { return is_object($store) ? $store : new \Magento\Framework\Object(['id' => 42]); })); $this->catalogHelperMock = $this->getMock('Magento\\Catalog\\Helper\\Data', [], [], '', false); $this->stateMock = $this->getMock('Magento\\Catalog\\Model\\Indexer\\Product\\Flat\\State', [], [], '', false); $this->scopeConfigInterfaceMock = $this->getMock('Magento\\Framework\\App\\Config\\ScopeConfigInterface'); $this->optionFactoryMock = $this->getMock('Magento\\Catalog\\Model\\Product\\OptionFactory', [], [], '', false); $this->urlMock = $this->getMock('Magento\\Catalog\\Model\\Resource\\Url', [], [], '', false); $this->timezoneInterfaceMock = $this->getMock('Magento\\Framework\\Stdlib\\DateTime\\TimezoneInterface'); $this->sessionMock = $this->getMock('Magento\\Customer\\Model\\Session', [], [], '', false); $this->dateTimeMock = $this->getMock('Magento\\Framework\\Stdlib\\DateTime'); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->collection = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Resource\\Product\\Link\\Product\\Collection', ['entityFactory' => $this->entityFactoryMock, 'logger' => $this->loggerMock, 'fetchStrategy' => $this->fetchStrategyMock, 'eventManager' => $this->managerInterfaceMock, 'eavConfig' => $this->configMock, 'resource' => $this->resourceMock, 'eavEntityFactory' => $this->entityFactoryMock2, 'resourceHelper' => $this->helperMock, 'universalFactory' => $this->universalFactoryMock, 'storeManager' => $this->storeManagerMock, 'catalogData' => $this->catalogHelperMock, 'catalogProductFlatState' => $this->stateMock, 'scopeConfig' => $this->scopeConfigInterfaceMock, 'productOptionFactory' => $this->optionFactoryMock, 'catalogUrl' => $this->urlMock, 'localeDate' => $this->timezoneInterfaceMock, 'customerSession' => $this->sessionMock, 'dateTime' => $this->dateTimeMock]); }
protected function setUp() { $this->coreEntityFactoryMock = $this->getMock('Magento\\Framework\\Data\\Collection\\EntityFactory', [], [], '', false); $this->loggerMock = $this->getMock('Psr\\Log\\LoggerInterface'); $this->fetchStrategyMock = $this->getMock('Magento\\Framework\\Data\\Collection\\Db\\FetchStrategyInterface', [], [], '', false); $this->eventManagerMock = $this->getMock('Magento\\Framework\\Event\\ManagerInterface', [], [], '', false); $this->configMock = $this->getMock('Magento\\Eav\\Model\\Config', [], [], '', false); $this->coreResourceMock = $this->getMock('Magento\\Framework\\App\\ResourceConnection', [], [], '', false); $this->resourceHelperMock = $this->getMock('Magento\\Eav\\Model\\ResourceModel\\Helper', [], [], '', false); $this->validatorFactoryMock = $this->getMock('Magento\\Framework\\Validator\\UniversalFactory', [], [], '', false); $this->entityFactoryMock = $this->getMock('Magento\\Eav\\Model\\EntityFactory', [], [], '', false); /** @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */ $connectionMock = $this->getMock('Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql', [], [], '', false); $this->statementMock = $this->getMock('Magento\\Framework\\DB\\Statement\\Pdo\\Mysql', ['fetch'], [], '', false); /** @var $selectMock \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject */ $selectMock = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false); $this->coreEntityFactoryMock->expects($this->any())->method('create')->will($this->returnCallback([$this, 'getMagentoObject'])); $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock)); $connectionMock->expects($this->any())->method('query')->willReturn($this->statementMock); $this->coreResourceMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock)); $entityMock = $this->getMock('Magento\\Eav\\Model\\Entity\\AbstractEntity', [], [], '', false); $entityMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock)); $entityMock->expects($this->any())->method('getDefaultAttributes')->will($this->returnValue([])); $this->validatorFactoryMock->expects($this->any())->method('create')->with('test_entity_model')->will($this->returnValue($entityMock)); $this->model = new AbstractCollectionStub($this->coreEntityFactoryMock, $this->loggerMock, $this->fetchStrategyMock, $this->eventManagerMock, $this->configMock, $this->coreResourceMock, $this->entityFactoryMock, $this->resourceHelperMock, $this->validatorFactoryMock, null); }
/** * Create Conversion id validator * * @param int|string $currentId * @return \Magento\Framework\Validator */ public function createConversionIdValidator($currentId) { $message = __('Conversion Id value is not valid "%1". Conversion Id should be an integer.', $currentId); /** @var \Magento\Framework\Validator\Builder $builder */ $builder = $this->_validatorBuilderFactory->create('Magento\\Framework\\Validator\\Builder', ['constraints' => [['alias' => 'Int', 'type' => '', 'class' => 'Magento\\Framework\\Validator\\IntUtils', 'options' => ['methods' => [['method' => 'setMessages', 'arguments' => [[IntUtils::NOT_INT => $message, IntUtils::INVALID => $message]]]]]]]]); return $builder->createValidator(); }
/** * Return default static virtual attribute that doesn't exists in EAV attributes * * @param \Magento\Eav\Model\Entity\AbstractEntity $resource * @param string $attributeCode * @return Attribute */ protected function _getDefaultAttribute(AbstractEntity $resource, $attributeCode) { $entityTypeId = $resource->getEntityType()->getId(); if (!isset($this->defaultAttributes[$entityTypeId][$attributeCode])) { $attribute = $this->objectManager->create($resource->getEntityType()->getAttributeModel())->setAttributeCode($attributeCode)->setBackendType(AbstractAttribute::TYPE_STATIC)->setIsGlobal(1)->setEntityType($resource->getEntityType())->setEntityTypeId($resource->getEntityType()->getId()); $this->defaultAttributes[$entityTypeId][$attributeCode] = $attribute; } return $this->defaultAttributes[$entityTypeId][$attributeCode]; }
/** * @param boolean $cacheEnabled * @param int $loadCalls * @param string $cachedValue * @param array $factoryCalls * @dataProvider getAttributeCacheDataProvider * @return void */ public function testGetAttributeCache($cacheEnabled, $loadCalls, $cachedValue, $factoryCalls) { $this->stateMock->expects($this->atLeastOnce())->method('isEnabled')->with(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER)->willReturn($cacheEnabled); $this->cacheMock->expects($this->exactly($loadCalls))->method('load')->with(Config::ATTRIBUTES_CACHE_ID)->willReturn($cachedValue); $collectionStub = new Object([['entity_type_code' => 'type_code_1', 'entity_type_id' => 1]]); $this->collectionFactoryMock->expects($this->any())->method('create')->willReturn($collectionStub); $this->typeFactoryMock->expects($this->any())->method('create')->willReturn(new Object(['id' => 101])); $this->universalFactoryMock->expects($this->exactly(count($factoryCalls)))->method('create')->will($this->returnValueMap($factoryCalls)); $entityType = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity\\Type')->setMethods(['getEntity'])->disableOriginalConstructor()->getMock(); $this->config->getAttribute($entityType, 'attribute_code_1'); }
/** * Create attribute from attribute data array * * @param string $entityType * @param array $attributeData * @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */ protected function _createAttribute($entityType, $attributeData) { $entityType = $this->getEntityType($entityType); $entityTypeCode = $entityType->getEntityTypeCode(); $attributeKey = $this->_getAttributeKey($entityTypeCode, $attributeData['attribute_code']); $attribute = $this->_load($attributeKey); if ($attribute) { $existsFullAttribute = $attribute->hasIsRequired(); $fullAttributeData = array_key_exists('is_required', $attributeData); if ($existsFullAttribute || !$existsFullAttribute && !$fullAttributeData) { return $attribute; } } if (!empty($attributeData['attribute_model'])) { $model = $attributeData['attribute_model']; } else { $model = $entityType->getAttributeModel(); } /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */ $attribute = $this->_universalFactory->create($model)->setData($attributeData); $this->_addAttributeReference($attributeData['attribute_id'], $attributeData['attribute_code'], $entityTypeCode); $attributeKey = $this->_getAttributeKey($entityTypeCode, $attributeData['attribute_code']); $this->_save($attribute, $attributeKey); return $attribute; }
/** * Retrieve option values collection * It is represented by an array in case of system attribute * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return array|\Magento\Eav\Model\Resource\Entity\Attribute\Option\Collection */ protected function _getOptionValuesCollection(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute) { if ($this->canManageOptionDefaultOnly()) { $options = $this->_universalFactory->create($attribute->getSourceModel())->setAttribute($attribute)->getAllOptions(); return $options; } else { return $this->_attrOptionCollectionFactory->create()->setAttributeFilter($attribute->getId())->setPositionOrder('asc', true)->load(); } }
/** * Create constraint validator instance * * @param array $data * @return \Magento\Framework\Validator\ValidatorInterface * @throws \InvalidArgumentException */ protected function _createConstraintValidator(array $data) { $validator = $this->_oneValidatorFactory->create($data['class'], isset($data['options']['arguments']) ? $this->_applyArgumentsCallback($data['options']['arguments']) : []); // Check validator type if (!$validator instanceof \Magento\Framework\Validator\ValidatorInterface) { throw new \InvalidArgumentException(sprintf('Constraint class "%s" must implement \\Magento\\Framework\\Validator\\ValidatorInterface', $data['class'])); } return $validator; }
/** * Create validator builder instance based on entity and group. * * @param string $entityName * @param string $groupName * @param array|null $builderConfig * @throws \InvalidArgumentException * @return \Magento\Framework\Validator\Builder */ public function createValidatorBuilder($entityName, $groupName, array $builderConfig = null) { if (!isset($this->_data[$entityName])) { throw new \InvalidArgumentException(sprintf('Unknown validation entity "%s"', $entityName)); } if (!isset($this->_data[$entityName][$groupName])) { throw new \InvalidArgumentException(sprintf('Unknown validation group "%s" in entity "%s"', $groupName, $entityName)); } $builderClass = isset($this->_data[$entityName][$groupName]['builder']) ? $this->_data[$entityName][$groupName]['builder'] : $this->_defaultBuilderClass; if (!class_exists($builderClass)) { throw new \InvalidArgumentException(sprintf('Builder class "%s" was not found', $builderClass)); } $builder = $this->_builderFactory->create($builderClass, ['constraints' => $this->_data[$entityName][$groupName]['constraints']]); if (!$builder instanceof \Magento\Framework\Validator\Builder) { throw new \InvalidArgumentException(sprintf('Builder "%s" must extend \\Magento\\Framework\\Validator\\Builder', $builderClass)); } if ($builderConfig) { $builder->addConfigurations($builderConfig); } return $builder; }
/** * Retrieve source instance * * @return \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource * @throws LocalizedException */ public function getSource() { if (empty($this->_source)) { if (!$this->getSourceModel()) { $this->setSourceModel($this->_getDefaultSourceModel()); } $source = $this->_universalFactory->create($this->getSourceModel()); if (!$source) { throw new LocalizedException(__('Source model "%1" not found for attribute "%2"', $this->getSourceModel(), $this->getAttributeCode())); } $this->_source = $source->setAttribute($this); } return $this->_source; }
/** * @param string $name * @param array $field * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function prepare($name, array &$field) { if ($name == self::CHILD_DATA_SOURCES) { foreach ($field as $childName => $childConfig) { $field[$childName] = $this->manager->getMetadata($childName); } return; } $options = []; if (isset($field['source'])) { if ($field['source'] == 'option') { $rawOptions = $this->manager->getData($field['reference']['target']); $options[] = ['label' => __('Please, select...'), 'value' => null]; foreach ($rawOptions as $rawOption) { $options[] = ['label' => $rawOption[$field['reference']['neededField']], 'value' => $rawOption[$field['reference']['targetField']]]; } } } else { if (isset($field['optionProvider'])) { list($source, $method) = explode('::', $field['optionProvider']); $sourceModel = $this->universalFactory->create($source); $options = $sourceModel->{$method}(); } } $attributeCodes = ['options' => ['eav_map' => 'options', 'default' => $options], 'dataType' => ['eav_map' => 'frontend_input', 'default' => 'text'], 'filterType' => ['default' => 'input_filter'], 'formElement' => ['default' => 'input'], 'displayArea' => ['default' => 'body'], 'visible' => ['eav_map' => 'is_visible', 'default' => true], 'required' => ['eav_map' => 'is_required', 'default' => false], 'label' => ['eav_map' => 'frontend_label'], 'sortOrder' => ['eav_map' => 'sort_order'], 'notice' => ['eav_map' => 'note'], 'default' => ['eav_map' => 'default_value'], 'unique' => [], 'description' => [], 'constraints' => [], 'customEntry' => [], 'size' => ['eav_map' => 'scope_multiline_count'], 'tooltip' => [], 'fieldGroup' => []]; foreach ($attributeCodes as $code => $info) { if (!isset($field[$code])) { if (isset($this->attributes[$name]) && isset($info['eav_map'])) { $field[$code] = $this->attributes[$name][$info['eav_map']]; } elseif (empty($field[$code]) && !empty($info['default'])) { $field[$code] = $info['default']; } } } if (isset($field['required']) && $field['required']) { $field['constraints']['validate']['required-entry'] = true; } }
public function setUp() { $this->coreEntityFactoryMock = $this->getMock('Magento\\Core\\Model\\EntityFactory', array(), array(), '', false); $this->loggerMock = $this->getMock('Magento\\Framework\\Logger', array(), array(), '', false); $this->fetchStrategyMock = $this->getMock('Magento\\Framework\\Data\\Collection\\Db\\FetchStrategyInterface', array(), array(), '', false); $this->eventManagerMock = $this->getMock('Magento\\Framework\\Event\\ManagerInterface', array(), array(), '', false); $this->configMock = $this->getMock('Magento\\Eav\\Model\\Config', array(), array(), '', false); $this->coreResourceMock = $this->getMock('Magento\\Framework\\App\\Resource', array('getConnection'), array(), '', false); $this->resourceHelperMock = $this->getMock('Magento\\Eav\\Model\\Resource\\Helper', array(), array(), '', false); $this->validatorFactoryMock = $this->getMock('Magento\\Framework\\Validator\\UniversalFactory', array(), array(), '', false); $this->entityFactoryMock = $this->getMock('Magento\\Eav\\Model\\EntityFactory', array(), array(), '', false); /** @var \Magento\Framework\DB\Adapter\Pdo\Mysql|\PHPUnit_Framework_MockObject_MockObject */ $connectionMock = $this->getMock('Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql', array(), array(), '', false); /** @var $selectMock \Zend_Db_Select|\PHPUnit_Framework_MockObject_MockObject */ $selectMock = $this->getMock('Zend_Db_Select', array(), array(), '', false); $this->coreEntityFactoryMock->expects($this->any())->method('create')->will($this->returnCallback(array($this, 'getMagentoObject'))); $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock)); $this->coreResourceMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock)); $entityMock = $this->getMock('Magento\\Eav\\Model\\Entity\\AbstractEntity', array(), array(), '', false); $entityMock->expects($this->once())->method('getReadConnection')->will($this->returnValue($connectionMock)); $entityMock->expects($this->once())->method('getDefaultAttributes')->will($this->returnValue(array())); $this->validatorFactoryMock->expects($this->once())->method('create')->with('test_entity_model')->will($this->returnValue($entityMock)); $this->model = new \Magento\Eav\Model\Entity\Collection\AbstractCollectionStub($this->coreEntityFactoryMock, $this->loggerMock, $this->fetchStrategyMock, $this->eventManagerMock, $this->configMock, $this->coreResourceMock, $this->entityFactoryMock, $this->resourceHelperMock, $this->validatorFactoryMock, null); }
/** * Retrieve Object instance with original data * * @param \Magento\Framework\Object $object * @return \Magento\Framework\Object */ protected function _getOrigObject($object) { $className = get_class($object); $origObject = $this->_universalFactory->create($className); $origObject->setData(array()); $this->load($origObject, $object->getData($this->getEntityIdField())); return $origObject; }
/** * Standard resource collection initialization * * @param string $model * @param string $entityModel * @return $this */ protected function _init($model, $entityModel) { $this->setItemObjectClass($model); $entity = $this->_universalFactory->create($entityModel); $this->setEntity($entity); return $this; }
/** * Get EAV Entity Form Attribute Collection * * @return mixed */ protected function _getFormAttributeCollection() { return $this->_universalFactory->create(str_replace('_', '\\', $this->_moduleName) . '\\Model\\Resource\\Form\\Attribute\\Collection'); }
/** * Retrieve resource entity object * * @return \Magento\Framework\Model\Resource\AbstractResource */ public function getEntity() { return $this->_universalFactory->create($this->_data['entity_model']); }