Ejemplo n.º 1
0
 /**
  * @dataProvider valueDataProvider
  * @param mixed $fieldValue
  */
 public function testFixEntityAssociationFieldsEntity($fieldValue)
 {
     $entity = new \stdClass();
     $entity->field = $fieldValue;
     if ($fieldValue instanceof ArrayCollection) {
         $linkedEntity = $fieldValue->getIterator()->offsetGet(0);
     } else {
         $linkedEntity = $fieldValue;
     }
     $this->fieldHelper->expects($this->once())->method('getRelations')->with(get_class($entity))->will($this->returnValue([['name' => 'field']]));
     $metadata = $this->getMockBuilder('Doctrine\\ORM\\Mapping\\ClassMetadata')->disableOriginalConstructor()->getMock();
     $metadata->expects($this->once())->method('getIdentifierValues')->with($linkedEntity)->will($this->returnValue('id'));
     $this->entityManager->expects($this->once())->method('getClassMetadata')->with(get_class($entity))->will($this->returnValue($metadata));
     $uow = $this->getMockBuilder('\\Doctrine\\ORM\\UnitOfWork')->disableOriginalConstructor()->getMock();
     $uow->expects($this->once())->method('getEntityState')->with($linkedEntity)->will($this->returnValue(UnitOfWork::STATE_DETACHED));
     $this->entityManager->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow));
     $this->entityManager->expects($this->once())->method('getReference')->with(get_class($entity), 'id')->will($this->returnCallback(function () use($entity) {
         $entity->reloaded = true;
         return $entity;
     }));
     $this->fixer->fixEntityAssociationFields($entity, 0);
     if ($fieldValue instanceof ArrayCollection) {
         $this->assertTrue($entity->field->getIterator()->offsetGet(0)->reloaded);
     } else {
         $this->assertTrue($entity->field->reloaded);
     }
 }
Ejemplo n.º 2
0
 /**
  * @param string $entityName
  * @param string $fieldName
  * @return string|null
  */
 protected function getRelationEntityName($entityName, $fieldName)
 {
     $fields = $this->fieldHelper->getFields($entityName, true);
     foreach ($fields as $field) {
         if ($field['name'] == $fieldName && $this->fieldHelper->isMultipleRelation($field)) {
             return $field['related_entity_name'];
         }
     }
     return null;
 }
Ejemplo n.º 3
0
 /**
  * @param mixed $expected
  * @param array $data
  *
  * @dataProvider denormalizeDataProvider
  */
 public function testDenormalize($expected, array $data)
 {
     $this->fieldHelper->expects($this->once())->method('getFields')->willReturn([['name' => 'incrementId'], ['name' => 'paymentDetails']]);
     $this->fieldHelper->expects($this->once())->method('setObjectValue')->will($this->returnCallback(function ($result, $fieldName, $value) {
         $propertyAccessor = PropertyAccess::createPropertyAccessor();
         $propertyAccessor->setValue($result, $fieldName, $value);
     }));
     /** @var Order $order */
     $order = $this->normalizer->denormalize($data, 'OroCRM\\Bundle\\MagentoBundle\\Entity\\Order');
     $this->assertEquals($expected, $order->getPaymentDetails());
 }
Ejemplo n.º 4
0
 /**
  * @param object $basicEntity
  * @param object $importedEntity
  * @param array $excludedProperties
  * @throws InvalidArgumentException
  */
 public function importEntity($basicEntity, $importedEntity, array $excludedProperties = array())
 {
     $basicEntityClass = ClassUtils::getClass($basicEntity);
     if ($basicEntityClass != ClassUtils::getClass($importedEntity)) {
         throw new InvalidArgumentException('Basic and imported entities must be instances of the same class');
     }
     $entityMetadata = $this->getEntityManager($basicEntityClass)->getClassMetadata($basicEntityClass);
     $importedEntityProperties = array_diff(array_merge($entityMetadata->getFieldNames(), $entityMetadata->getAssociationNames()), $excludedProperties);
     foreach ($importedEntityProperties as $propertyName) {
         $importedValue = $this->fieldHelper->getObjectValue($importedEntity, $propertyName);
         $this->fieldHelper->setObjectValue($basicEntity, $propertyName, $importedValue);
     }
 }
Ejemplo n.º 5
0
 protected function setUp()
 {
     $this->eventDispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
     $this->fieldHelper = $this->getMockBuilder('Oro\\Bundle\\ImportExportBundle\\Field\\FieldHelper')->disableOriginalConstructor()->getMock();
     $this->fieldHelper->expects($this->any())->method('getIdentityValues')->willReturn([]);
     $this->fieldHelper->expects($this->any())->method('getFields')->willReturn([]);
     $this->databaseHelper = $this->getMockBuilder('Oro\\Bundle\\ImportExportBundle\\Field\\DatabaseHelper')->disableOriginalConstructor()->getMock();
     $this->strategyHelper = $this->getMockBuilder('Oro\\Bundle\\ImportExportBundle\\Strategy\\Import\\ImportStrategyHelper')->disableOriginalConstructor()->getMock();
     $this->defaultOwnerHelper = $this->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\ImportExport\\Helper\\DefaultOwnerHelper')->disableOriginalConstructor()->getMock();
     $this->channelHelper = $this->getMockBuilder('OroCRM\\Bundle\\ChannelBundle\\ImportExport\\Helper\\ChannelHelper')->disableOriginalConstructor()->getMock();
     $this->addressHelper = $this->getMockBuilder('OroCRM\\Bundle\\MagentoBundle\\ImportExport\\Strategy\\StrategyHelper\\AddressImportHelper')->disableOriginalConstructor()->getMock();
     $this->stepExecution = $this->getMockBuilder('Akeneo\\Bundle\\BatchBundle\\Entity\\StepExecution')->disableOriginalConstructor()->getMock();
     $this->jobExecution = $this->getMockBuilder('Akeneo\\Bundle\\BatchBundle\\Entity\\JobExecution')->disableOriginalConstructor()->getMock();
     $this->stepExecution->expects($this->any())->method('getJobExecution')->will($this->returnValue($this->jobExecution));
     $this->logger = new NullLogger();
 }
 /**
  * @param string $entityName
  * @param bool $fullData
  * @param int $singleRelationDeepLevel
  * @param int $multipleRelationDeepLevel
  * @return array
  */
 protected function getEntityRulesAndBackendHeaders($entityName, $fullData = false, $singleRelationDeepLevel = 0, $multipleRelationDeepLevel = 0)
 {
     // get fields data
     $fields = $this->fieldHelper->getFields($entityName, true);
     $rules = array();
     $backendHeaders = array();
     $defaultOrder = 10000;
     // generate conversion rules and backend header
     foreach ($fields as $field) {
         $fieldName = $field['name'];
         if ($this->fieldHelper->getConfigValue($entityName, $fieldName, 'excluded')) {
             continue;
         }
         // get import/export config parameters
         $fieldHeader = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'header', $field['label']);
         $fieldOrder = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'order');
         if ($fieldOrder === null || $fieldOrder === '') {
             $fieldOrder = $defaultOrder;
             $defaultOrder++;
         }
         $fieldOrder = (int) $fieldOrder;
         // process relations
         if ($this->fieldHelper->isRelation($field) && !$this->fieldHelper->processRelationAsScalar($entityName, $fieldName)) {
             $isSingleRelation = $this->fieldHelper->isSingleRelation($field) && $singleRelationDeepLevel > 0;
             $isMultipleRelation = $this->fieldHelper->isMultipleRelation($field) && $multipleRelationDeepLevel > 0;
             // if relation must be included
             if ($fullData && ($isSingleRelation || $isMultipleRelation)) {
                 $relatedEntityName = $field['related_entity_name'];
                 $fieldFullData = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'full', false);
                 // process and merge relation rules and backend header for relation
                 list($relationRules, $relationBackendHeaders) = $this->getEntityRulesAndBackendHeaders($relatedEntityName, $fieldFullData, $singleRelationDeepLevel - 1, $multipleRelationDeepLevel - 1);
                 $relationRules = $this->buildRelationRules($relationRules, $isSingleRelation, $isMultipleRelation, $fieldName, $fieldHeader, $fieldOrder);
                 $rules = array_merge($rules, $relationRules);
                 $relationBackendHeaders = $this->buildBackendHeaders($relationBackendHeaders, $isSingleRelation, $isMultipleRelation, $entityName, $fieldName, $fieldOrder);
                 $backendHeaders = array_merge($backendHeaders, $relationBackendHeaders);
             }
         } else {
             // process scalars
             if ($fullData || $this->fieldHelper->getConfigValue($entityName, $fieldName, 'identity')) {
                 $rules[$fieldHeader] = array('value' => $fieldName, 'order' => $fieldOrder);
                 $backendHeaders[] = $rules[$fieldHeader];
             }
         }
     }
     return array($this->sortData($rules), $this->sortData($backendHeaders));
 }
Ejemplo n.º 7
0
 public function testGetRelations()
 {
     $entityName = 'TestEntity';
     $expectedRelations = [['name' => 'field']];
     $this->fieldProvider->expects($this->once())->method('getRelations')->with($entityName)->will($this->returnValue($expectedRelations));
     $this->assertEquals($expectedRelations, $this->helper->getRelations($entityName));
     // do not call twice
     $this->assertEquals($expectedRelations, $this->helper->getRelations($entityName));
 }
 /**
  * @param string $entityName
  * @return bool
  */
 protected function hasIdentityFields($entityName)
 {
     $fields = $this->fieldHelper->getFields($entityName, true);
     foreach ($fields as $field) {
         $fieldName = $field['name'];
         if ($this->fieldHelper->getConfigValue($entityName, $fieldName, 'identity')) {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 9
0
 public function testGetIdentityValues()
 {
     $this->config['stdClass'] = array('excludedField' => array('excluded' => true), 'identityField' => array('identity' => true), 'regularField' => array());
     $fields = array(array('name' => 'excludedField'), array('name' => 'identityField'), array('name' => 'regularField'));
     $entity = new \stdClass();
     $entity->excludedField = 'excludedValue';
     $entity->identityField = 'identityValue';
     $entity->regularField = 'regularValue';
     $this->fieldProvider->expects($this->once())->method('getFields')->with(get_class($entity), true)->will($this->returnValue($fields));
     $this->assertEquals(array('identityField' => 'identityValue'), $this->helper->getIdentityValues($entity, $fields));
 }
 /**
  * {@inheritdoc}
  */
 public function getMaxRelatedEntities($entityName, $fieldName)
 {
     $maxFields = 1;
     $fixtures = $this->templateManager->getEntityFixture($entityName)->getData();
     foreach ($fixtures as $fixture) {
         try {
             $fieldValue = $this->fieldHelper->getObjectValue($fixture, $fieldName);
             if ($fieldValue instanceof \Countable || is_array($fieldValue)) {
                 $itemsCount = count($fieldValue);
                 if ($itemsCount > $maxFields) {
                     $maxFields = $itemsCount;
                 }
             }
         } catch (\Exception $e) {
             // there is no $fieldName in fixture
             continue;
         }
     }
     return $maxFields;
 }
Ejemplo n.º 11
0
 /**
  * @param object $entity
  * @param array $searchContext
  *
  * @return object|null
  */
 protected function findExistingEntityByIdentityFields($entity, array $searchContext = [])
 {
     $entityName = ClassUtils::getClass($entity);
     $identityValues = $searchContext;
     $identityValues += $this->fieldHelper->getIdentityValues($entity);
     foreach ($identityValues as $fieldName => $value) {
         if ($value !== null || $this->fieldHelper->isRequiredIdentityField($entityName, $fieldName)) {
             continue;
         }
         unset($identityValues[$fieldName]);
     }
     return $this->findEntityByIdentityValues($entityName, $identityValues);
 }
Ejemplo n.º 12
0
 /**
  * Finds detached properties in entity and reloads them from UnitOfWork
  *
  * @param object $entity doctrine entity
  * @param int $level maximum nesting level
  */
 public function fixEntityAssociationFields($entity, $level = 0)
 {
     if ($level < 0) {
         return;
     }
     // we should use entityFieldProvider through fieldHelper
     // to get relations data and avoid deleted relations in result list
     $relations = $this->fieldHelper->getRelations(ClassUtils::getClass($entity));
     if (!$relations) {
         return;
     }
     foreach ($relations as $associationMapping) {
         $fieldName = $associationMapping['name'];
         $value = $this->propertyAccessor->getValue($entity, $fieldName);
         if ($value && is_object($value)) {
             if ($value instanceof Collection) {
                 $this->fixCollectionField($value, $level);
             } else {
                 $this->fixEntityField($entity, $fieldName, $value, $level);
             }
         }
     }
 }
Ejemplo n.º 13
0
 /**
  * @param object $entity
  * @param array $searchContext
  * @return null|object
  */
 protected function findExistingEntity($entity, array $searchContext = array())
 {
     $entityName = ClassUtils::getClass($entity);
     $identifier = $this->databaseHelper->getIdentifier($entity);
     $existingEntity = null;
     // find by identifier
     if ($identifier) {
         $existingEntity = $this->databaseHelper->find($entityName, $identifier);
     }
     // find by identity fields
     if (!$existingEntity && (!$searchContext || $this->databaseHelper->getIdentifier(current($searchContext)))) {
         $identityValues = $searchContext;
         $identityValues += $this->fieldHelper->getIdentityValues($entity);
         $existingEntity = $this->findEntityByIdentityValues($entityName, $identityValues);
     }
     return $existingEntity;
 }
 /**
  * @param string $entityName
  * @param bool $fullData
  * @param int $singleRelationDeepLevel
  * @param int $multipleRelationDeepLevel
  * @param array $field
  * @param string $fieldHeader
  * @param int $fieldOrder
  *
  * @return array
  */
 protected function getRelatedEntityRulesAndBackendHeaders($entityName, $fullData, $singleRelationDeepLevel, $multipleRelationDeepLevel, $field, $fieldHeader, $fieldOrder, $isIdentifier = false)
 {
     $fieldName = $field['name'];
     $relationRules = [];
     $relationBackendHeaders = [];
     $isSingleRelation = $this->fieldHelper->isSingleRelation($field) && $singleRelationDeepLevel > 0;
     $isMultipleRelation = $this->fieldHelper->isMultipleRelation($field) && $multipleRelationDeepLevel > 0;
     // if relation must be included
     if ($fullData && ($isSingleRelation || $isMultipleRelation)) {
         $relatedEntityName = $field['related_entity_name'];
         $fieldFullData = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'full', false);
         // process and merge relation rules and backend header for relation
         list($relationRules, $relationBackendHeaders) = $this->getEntityRulesAndBackendHeaders($relatedEntityName, $fieldFullData, $singleRelationDeepLevel - 1, $multipleRelationDeepLevel - 1);
         $relationRules = $this->buildRelationRules($relationRules, $isSingleRelation, $isMultipleRelation, $fieldName, $fieldHeader, $fieldOrder);
         $relationBackendHeaders = $this->buildBackendHeaders($relationBackendHeaders, $isSingleRelation, $isMultipleRelation, $entityName, $fieldName, $fieldOrder);
     }
     return [$relationRules, $relationBackendHeaders];
 }