Example #1
0
 /**
  * @expectedException \Exception
  * @expectedExceptionMessage Store instance cannot be created.
  */
 public function testFetchNewIncrementIdRollsBackTransactionAndRethrowsExceptionIfProgramFlowIsInterrupted()
 {
     $this->model->setIncrementModel('\\IncrementModel');
     $this->resourceMock->expects($this->once())->method('beginTransaction');
     // Interrupt program flow by exception
     $exception = new \Exception('Store instance cannot be created.');
     $this->storeFactoryMock->expects($this->once())->method('create')->will($this->throwException($exception));
     $this->resourceMock->expects($this->once())->method('rollBack');
     $this->resourceMock->expects($this->never())->method('commit');
     $this->model->fetchNewIncrementId();
 }
Example #2
0
 /**
  * Specify attribute entity type filter
  *
  * @param  Type|int $type
  * @return $this
  */
 public function setEntityTypeFilter($type)
 {
     if ($type instanceof Type) {
         $additionalTable = $type->getAdditionalAttributeTable();
         $id = $type->getId();
     } else {
         $additionalTable = $this->getResource()->getAdditionalAttributeTable($type);
         $id = $type;
     }
     $this->addFieldToFilter('main_table.entity_type_id', $id);
     if ($additionalTable) {
         $this->join(['additional_table' => $additionalTable], 'additional_table.attribute_id = main_table.attribute_id');
     }
     return $this;
 }
 protected function setUp()
 {
     parent::setUp();
     $this->eavConfigMock = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
     $this->eavValidationRulesMock = $this->getMockBuilder(EavValidationRules::class)->disableOriginalConstructor()->getMock();
     $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
     $this->groupCollectionFactoryMock = $this->getMockBuilder(GroupCollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock();
     $this->groupCollectionMock = $this->getMockBuilder(GroupCollection::class)->disableOriginalConstructor()->getMock();
     $this->attributeMock = $this->getMockBuilder(EavAttribute::class)->disableOriginalConstructor()->getMock();
     $this->groupMock = $this->getMockBuilder(Group::class)->disableOriginalConstructor()->setMethods(['getAttributeGroupCode'])->getMock();
     $this->entityTypeMock = $this->getMockBuilder(EntityType::class)->disableOriginalConstructor()->getMock();
     $this->attributeCollectionMock = $this->getMockBuilder(AttributeCollection::class)->disableOriginalConstructor()->getMock();
     $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMockForAbstractClass();
     $this->formElementMapperMock = $this->getMockBuilder(FormElementMapper::class)->disableOriginalConstructor()->getMock();
     $this->metaPropertiesMapperMock = $this->getMockBuilder(MetaPropertiesMapper::class)->disableOriginalConstructor()->getMock();
     $this->groupCollectionFactoryMock->expects($this->any())->method('create')->willReturn($this->groupCollectionMock);
     $this->groupCollectionMock->expects($this->any())->method('setAttributeSetFilter')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('setSortOrder')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('load')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([$this->groupMock]));
     $this->attributeCollectionMock->expects($this->any())->method('addFieldToSelect')->willReturnSelf();
     $this->attributeCollectionMock->expects($this->any())->method('load')->willReturnSelf();
     $this->eavConfigMock->expects($this->any())->method('getEntityType')->willReturn($this->entityTypeMock);
     $this->entityTypeMock->expects($this->any())->method('getAttributeCollection')->willReturn($this->attributeCollectionMock);
     $this->productMock->expects($this->any())->method('getAttributes')->willReturn([$this->attributeMock]);
 }
Example #4
0
 public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
 {
     $installer = $setup;
     $installer->startSetup();
     $attributeSetCollection = $this->eavEntitiyType->loadByCode('catalog_product')->getAttributeSetCollection();
     $entityTypeId = $this->eavEntitiyType->loadByCode('catalog_product')->getId();
     $model = $this->attributeFactory->create();
     $data['attribute_code'] = 'is_featured';
     $data['is_user_defined'] = 1;
     $data['frontend_input'] = 'boolean';
     $data += ['is_filterable' => 0, 'is_filterable_in_search' => 0, 'apply_to' => []];
     $data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
     $data['default_value'] = 0;
     $data['frontend_label'] = 'Is Featured';
     $model->addData($data);
     $model->setEntityTypeId($entityTypeId);
     $model->setIsUserDefined(1);
     try {
         foreach ($attributeSetCollection as $attributeSet) {
             $attributeSetId = $attributeSet->getId();
             $groupCollection = $this->groupCollectionFactory->create()->setAttributeSetFilter($attributeSetId)->load();
             $groupCode = 'product-details';
             $attributeGroupId = null;
             foreach ($groupCollection as $group) {
                 if ($group->getAttributeGroupCode() == $groupCode) {
                     $attributeGroupId = $group->getAttributeGroupId();
                     break;
                 }
             }
             $model->setAttributeSetId($attributeSetId);
             $model->setAttributeGroupId($attributeGroupId);
             $model->save();
         }
     } catch (\Exception $e) {
         echo $this->messageManager->addError($e->getMessage());
     }
     $installer->endSetup();
 }
 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->eventManagerMock = $this->getMock('Magento\\Framework\\Event\\ManagerInterface');
     $this->eavConfigMock = $this->getMock('Magento\\Eav\\Model\\Config', [], [], '', false);
     $this->entityTypeMock = $this->getMock('Magento\\Eav\\Model\\Entity\\Type', ['__wakeup'], [], '', false);
     $this->entityTypeMock->setAdditionalAttributeTable('some_extra_table');
     $this->eavConfigMock->expects($this->any())->method('getEntityType')->will($this->returnValue($this->entityTypeMock));
     $this->storeManagerMock = $this->getMock('Magento\\Store\\Model\\StoreManagerInterface');
     $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnSelf());
     $this->connectionMock = $this->getMock('Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql', ['select', 'describeTable', 'quoteIdentifier', '_connect', '_quote'], [], '', false);
     $this->select = new \Magento\Framework\DB\Select($this->connectionMock);
     $this->resourceMock = $this->getMockForAbstractClass('Magento\\Framework\\Model\\ModelResource\\Db\\AbstractDb', [], '', false, true, true, ['__wakeup', 'getConnection', 'getMainTable', 'getTable']);
     $this->connectionMock->expects($this->any())->method('select')->will($this->returnValue($this->select));
     $this->connectionMock->expects($this->any())->method('quoteIdentifier')->will($this->returnArgument(0));
     $this->connectionMock->expects($this->any())->method('describeTable')->will($this->returnvalueMap([['some_main_table', null, ['col1' => [], 'col2' => []]], ['some_extra_table', null, ['col2' => [], 'col3' => []]], [null, null, ['col2' => [], 'col3' => [], 'col4' => []]]]));
     $this->connectionMock->expects($this->any())->method('_quote')->will($this->returnArgument(0));
     $this->resourceMock->expects($this->any())->method('getConnection')->will($this->returnValue($this->connectionMock));
     $this->resourceMock->expects($this->any())->method('getMainTable')->will($this->returnValue('some_main_table'));
     $this->resourceMock->expects($this->any())->method('getTable')->will($this->returnValue('some_extra_table'));
 }
Example #6
0
 /**
  * Prepare Eav config mock object
  *
  * @return \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  */
 protected function prepareEavConfig()
 {
     $attributeMock = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', ['getAttributeCode', 'getBackend', '__wakeup'], [], '', false);
     $attributeMock->expects($this->any())->method('getAttributeCode')->willReturn('entity_id');
     $attributeMock->expects($this->any())->method('getBackend')->willReturn($this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend', [], [], '', false));
     $this->eavConfigType = $this->getMock('Magento\\Eav\\Model\\Entity\\Type', ['getEntityIdField', 'getId', 'getEntityTable', '__wakeup'], [], '', false);
     $this->eavConfigType->expects($this->any())->method('getEntityIdField')->willReturn(false);
     $this->eavConfigType->expects($this->any())->method('getId')->willReturn(false);
     $this->eavConfigType->expects($this->any())->method('getEntityTable')->willReturn('customer_address_entity');
     $eavConfig = $this->getMock('Magento\\Eav\\Model\\Config', ['getEntityType', 'getEntityAttributeCodes', 'getAttribute'], [], '', false);
     $eavConfig->expects($this->any())->method('getEntityType')->with('customer_address')->willReturn($this->eavConfigType);
     $eavConfig->expects($this->any())->method('getEntityAttributeCodes')->with($this->eavConfigType)->willReturn(['entity_type_id', 'attribute_set_id', 'created_at', 'updated_at', 'parent_id', 'increment_id', 'entity_id']);
     $eavConfig->expects($this->any())->method('getAttribute')->willReturnMap([[$this->eavConfigType, 'entity_type_id', $attributeMock], [$this->eavConfigType, 'attribute_set_id', $attributeMock], [$this->eavConfigType, 'created_at', $attributeMock], [$this->eavConfigType, 'updated_at', $attributeMock], [$this->eavConfigType, 'parent_id', $attributeMock], [$this->eavConfigType, 'increment_id', $attributeMock], [$this->eavConfigType, 'entity_id', $attributeMock]]);
     return $eavConfig;
 }
Example #7
0
 protected function setUp()
 {
     parent::setUp();
     $this->objectManager = new ObjectManager($this);
     $this->eavConfigMock = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
     $this->eavValidationRulesMock = $this->getMockBuilder(EavValidationRules::class)->disableOriginalConstructor()->getMock();
     $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
     $this->groupCollectionFactoryMock = $this->getMockBuilder(GroupCollectionFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock();
     $this->groupCollectionMock = $this->getMockBuilder(GroupCollection::class)->disableOriginalConstructor()->getMock();
     $this->attributeMock = $this->getMockBuilder(EavAttribute::class)->disableOriginalConstructor()->getMock();
     $this->groupMock = $this->getMockBuilder(Group::class)->disableOriginalConstructor()->setMethods(['getAttributeGroupCode'])->getMock();
     $this->entityTypeMock = $this->getMockBuilder(EntityType::class)->disableOriginalConstructor()->getMock();
     $this->attributeCollectionMock = $this->getMockBuilder(AttributeCollection::class)->disableOriginalConstructor()->getMock();
     $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMockForAbstractClass();
     $this->formElementMapperMock = $this->getMockBuilder(FormElementMapper::class)->disableOriginalConstructor()->getMock();
     $this->metaPropertiesMapperMock = $this->getMockBuilder(MetaPropertiesMapper::class)->disableOriginalConstructor()->getMock();
     $this->searchCriteriaBuilderMock = $this->getMockBuilder(SearchCriteriaBuilder::class)->disableOriginalConstructor()->getMock();
     $this->attributeGroupRepositoryMock = $this->getMockBuilder(ProductAttributeGroupRepositoryInterface::class)->getMockForAbstractClass();
     $this->attributeGroupMock = $this->getMockBuilder(AttributeGroupInterface::class)->setMethods(['getAttributeGroupCode', 'getApplyTo'])->getMockForAbstractClass();
     $this->attributeRepositoryMock = $this->getMockBuilder(ProductAttributeRepositoryInterface::class)->getMockForAbstractClass();
     $this->searchCriteriaMock = $this->getMockBuilder(SearchCriteria::class)->disableOriginalConstructor()->setMethods(['getItems'])->getMock();
     $this->sortOrderBuilderMock = $this->getMockBuilder(SortOrderBuilder::class)->disableOriginalConstructor()->getMock();
     $this->searchResultsMock = $this->getMockBuilder(SearchResultsInterface::class)->getMockForAbstractClass();
     $this->eavAttributeMock = $this->getMockBuilder(Attribute::class)->setMethods(['getAttributeGroupCode', 'getApplyTo', 'getFrontendInput', 'getAttributeCode'])->disableOriginalConstructor()->getMock();
     $this->groupCollectionFactoryMock->expects($this->any())->method('create')->willReturn($this->groupCollectionMock);
     $this->groupCollectionMock->expects($this->any())->method('setAttributeSetFilter')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('setSortOrder')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('load')->willReturnSelf();
     $this->groupCollectionMock->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([$this->groupMock]));
     $this->attributeCollectionMock->expects($this->any())->method('addFieldToSelect')->willReturnSelf();
     $this->attributeCollectionMock->expects($this->any())->method('load')->willReturnSelf();
     $this->eavConfigMock->expects($this->any())->method('getEntityType')->willReturn($this->entityTypeMock);
     $this->entityTypeMock->expects($this->any())->method('getAttributeCollection')->willReturn($this->attributeCollectionMock);
     $this->productMock->expects($this->any())->method('getAttributes')->willReturn([$this->attributeMock]);
     $this->storeMock = $this->getMockBuilder(StoreInterface::class)->setMethods(['load', 'getId', 'getConfig', 'getBaseCurrencyCode'])->getMockForAbstractClass();
     $this->currencyMock = $this->getMockBuilder(Currency::class)->disableOriginalConstructor()->setMethods(['toCurrency'])->getMock();
     $this->currencyLocaleMock = $this->getMockBuilder(CurrencyLocale::class)->disableOriginalConstructor()->setMethods(['getCurrency'])->getMock();
     $this->eav = $this->getModel();
     $this->objectManager->setBackwardCompatibleProperty($this->eav, 'localeCurrency', $this->currencyLocaleMock);
 }
 /**
  * Get attributes meta
  *
  * @param Type $entityType
  * @return array
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function getAttributesMeta(Type $entityType)
 {
     $meta = [];
     $attributes = $entityType->getAttributeCollection();
     /* @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
     foreach ($attributes as $attribute) {
         $code = $attribute->getAttributeCode();
         // use getDataUsingMethod, since some getters are defined and apply additional processing of returning value
         foreach ($this->metaProperties as $metaName => $origName) {
             $value = $attribute->getDataUsingMethod($origName);
             $meta[$code][$metaName] = $metaName === 'label' ? __($value) : $value;
             if ('frontend_input' === $origName) {
                 $meta[$code]['formElement'] = isset($this->formElement[$value]) ? $this->formElement[$value] : $value;
             }
             if ($attribute->usesSource()) {
                 $meta[$code]['options'] = $attribute->getSource()->getAllOptions();
             }
         }
         $rules = $this->eavValidationRules->build($attribute, $meta[$code]);
         if (!empty($rules)) {
             $meta[$code]['validation'] = $rules;
         }
     }
     return $meta;
 }
 public function testNextValue()
 {
     $this->type->expects($this->once())->method('fetchNewIncrementId')->with(1)->willReturn(2);
     $this->eavConfig->expects($this->once())->method('getEntityType')->with('order')->willReturn($this->type);
     $this->assertEquals(2, $this->model->getNextValue(1));
 }
 /**
  * Get attributes meta
  *
  * @param Type $entityType
  * @return array
  * @throws \Magento\Framework\Exception\LocalizedException
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function getAttributesMeta(Type $entityType)
 {
     $meta = [];
     $attributes = $entityType->getAttributeCollection();
     /* @var EavAttribute $attribute */
     foreach ($attributes as $attribute) {
         $code = $attribute->getAttributeCode();
         // use getDataUsingMethod, since some getters are defined and apply additional processing of returning value
         foreach ($this->metaProperties as $metaName => $origName) {
             $value = $attribute->getDataUsingMethod($origName);
             $meta[$code][$metaName] = $value;
             if ('frontend_input' === $origName) {
                 $meta[$code]['formElement'] = isset($this->formElement[$value]) ? $this->formElement[$value] : $value;
             }
             if ($attribute->usesSource()) {
                 $meta[$code]['options'] = $attribute->getSource()->getAllOptions();
             }
         }
         $rules = $this->eavValidationRules->build($attribute, $meta[$code]);
         if (!empty($rules)) {
             $meta[$code]['validation'] = $rules;
         }
         $meta[$code]['scopeLabel'] = $this->getScopeLabel($attribute);
         $meta[$code]['componentType'] = Field::NAME;
     }
     $result = [];
     foreach ($meta as $key => $item) {
         $result[$key] = $item;
         $result[$key]['sortOrder'] = 0;
     }
     $result = $this->getDefaultMetaData($result);
     return $result;
 }