示例#1
0
 protected function setUp()
 {
     parent::setUp();
     $this->configManager = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigManager')->disableOriginalConstructor()->getMock();
     $this->translator = $this->getMockBuilder('Oro\\Bundle\\TranslationBundle\\Translation\\Translator')->disableOriginalConstructor()->getMock();
     $this->translator->expects($this->any())->method('trans')->willReturnCallback(function ($id, $parameters) {
         if ($id === 'oro.entity_extend.form.data_type.inverse_relation') {
             return strtr('Reuse "%field_name%" of %entity_name%', $parameters);
         }
         return $id;
     });
     $this->fieldTypeProvider = $this->getMockBuilder('Oro\\Bundle\\EntityExtendBundle\\Provider\\FieldTypeProvider')->disableOriginalConstructor()->getMock();
     $this->fieldTypeProvider->expects($this->any())->method('getSupportedFieldTypes')->willReturn(array_keys($this->defaultFieldTypeChoices[self::FIELDS_GROUP]));
     $this->fieldTypeProvider->expects($this->any())->method('getSupportedRelationTypes')->willReturn(array_keys($this->defaultFieldTypeChoices[self::RELATIONS_GROUP]));
     $this->type = new FieldType($this->configManager, $this->translator, new ExtendDbIdentifierNameGenerator(), $this->fieldTypeProvider);
 }
 /**
  * @param FieldConfigModel $entity
  * @return array
  */
 protected function validateEntityFields(FieldConfigModel $entity)
 {
     $errors = [];
     $fieldProperties = $this->fieldTypeProvider->getFieldProperties($entity->getType());
     foreach ($fieldProperties as $scope => $properties) {
         $scopeData = $entity->toArray($scope);
         foreach ($properties as $code => $config) {
             if (!isset($scopeData[$code])) {
                 continue;
             }
             if ($scope === 'enum') {
                 foreach ($scopeData[$code] as $key => $enumFields) {
                     $result = $this->strategyHelper->validateEntity(EnumValue::createFromArray($enumFields));
                     if ($result) {
                         $errors[] = sprintf('%s.%s.%s: %s', $scope, $code, $key, implode(' ', $result));
                     }
                 }
             } elseif (isset($config['constraints'])) {
                 $result = $this->strategyHelper->validateEntity($scopeData[$code], $this->constraintFactory->parse($config['constraints']));
                 if ($result) {
                     $errors[] = sprintf('%s.%s: %s', $scope, $code, implode(' ', $result));
                 }
             }
         }
     }
     return $errors;
 }
 /**
  * {@inheritdoc}
  */
 protected function getBackendHeader()
 {
     $header = ['fieldName', 'is_serialized', 'type'];
     foreach ($this->fieldTypeProvider->getSupportedFieldTypes() as $fieldType) {
         $properties = $this->fieldTypeProvider->getFieldProperties($fieldType);
         foreach ($properties as $scope => $fields) {
             foreach ($fields as $code => $config) {
                 $field = sprintf('%s.%s', $scope, $code);
                 if (in_array($field, $this->excludedFields, true) || in_array($field, $header, true)) {
                     continue;
                 }
                 $header[] = $field;
             }
         }
     }
     return array_merge($header, ['enum.enum_options.0.label', 'enum.enum_options.0.is_default', 'enum.enum_options.1.label', 'enum.enum_options.1.is_default', 'enum.enum_options.2.label', 'enum.enum_options.2.is_default']);
 }
 public function testGetFieldProperties()
 {
     $configType = PropertyConfigContainer::TYPE_FIELD;
     $fieldType = 'string';
     $scope = 'testScope';
     $code = 'test_code';
     $providerConfig = [$code => ['options' => [], 'form' => []]];
     $propertyConfig = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Provider\\PropertyConfigContainer')->disableOriginalConstructor()->getMock();
     $propertyConfig->expects($this->once())->method('hasForm')->with($configType, $fieldType)->willReturn($propertyConfig);
     $propertyConfig->expects($this->once())->method('getFormItems')->with($configType, $fieldType)->willReturn($providerConfig);
     $configProvider = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProvider')->disableOriginalConstructor()->getMock();
     $configProvider->expects($this->once())->method('getPropertyConfig')->willReturn($propertyConfig);
     $configProvider->expects($this->once())->method('getScope')->willReturn($scope);
     $this->configManager->expects($this->once())->method('getProviders')->willReturn([$configProvider]);
     $provider = new FieldTypeProvider($this->configManager, [], []);
     $this->assertEquals([$scope => $providerConfig], $provider->getFieldProperties($fieldType, $configType));
 }
 /**
  * @param array $inputData
  * @param FieldConfigModel $expectedData
  *
  * @dataProvider denormalizeProvider
  */
 public function testDenormalize(array $inputData, FieldConfigModel $expectedData)
 {
     /* @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
     $objectManager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager');
     $this->registry->expects($this->once())->method('getManagerForClass')->with($inputData['configModel']['class'])->willReturn($objectManager);
     $objectManager->expects($this->once())->method('find')->with($inputData['configModel']['class'], $inputData['configModel']['id'])->willReturn($inputData['configModel']['object']);
     $this->fieldTypeProvider->expects($this->once())->method('getFieldProperties')->with($inputData['fieldType']['modelType'])->willReturn($inputData['fieldType']['fieldProperties']);
     $this->assertEquals($expectedData, $this->normalizer->denormalize($inputData['data'], $inputData['class']));
 }
示例#6
0
 /**
  * @param array $reverseRelationTypes
  *
  * @return array
  */
 protected function getFieldTypeChoices($reverseRelationTypes)
 {
     $fieldTypes = $relationTypes = [];
     foreach ($this->fieldTypeProvider->getSupportedFieldTypes() as $type) {
         $fieldTypes[$type] = $this->translator->trans(self::TYPE_LABEL_PREFIX . $type);
     }
     foreach ($this->fieldTypeProvider->getSupportedRelationTypes() as $type) {
         $relationTypes[$type] = $this->translator->trans(self::TYPE_LABEL_PREFIX . $type);
     }
     uasort($fieldTypes, 'strcasecmp');
     uasort($relationTypes, 'strcasecmp');
     if (!empty($reverseRelationTypes)) {
         uasort($reverseRelationTypes, 'strcasecmp');
         $relationTypes = array_merge($relationTypes, $reverseRelationTypes);
     }
     $result = [$this->translator->trans(self::GROUP_TYPE_PREFIX . self::GROUP_FIELDS) => $fieldTypes, $this->translator->trans(self::GROUP_TYPE_PREFIX . self::GROUP_RELATIONS) => $relationTypes];
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     $this->fieldTypeProvider = $this->getMockBuilder('Oro\\Bundle\\EntityExtendBundle\\Provider\\FieldTypeProvider')->disableOriginalConstructor()->getMock();
     $this->fieldTypeProvider->expects(static::any())->method('getSupportedFieldTypes')->willReturn(['type1', 'type2']);
     $this->fieldTypeProvider->expects(static::any())->method('getFieldProperties')->willReturn([]);
     $this->translator = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface');
     $this->translator->expects(static::any())->method('trans')->willReturnCallback(function ($value) {
         return $value;
     });
     /** @var FieldHelper $fieldHelper */
     $this->fieldHelper = $this->getMockBuilder('Oro\\Bundle\\ImportExportBundle\\Field\\FieldHelper')->disableOriginalConstructor()->getMock();
     /** @var ImportStrategyHelper $strategyHelper */
     $this->strategyHelper = $this->getMockBuilder('Oro\\Bundle\\ImportExportBundle\\Strategy\\Import\\ImportStrategyHelper')->disableOriginalConstructor()->getMock();
     $this->databaseHelper = $this->getMockBuilder('Oro\\Bundle\\ImportExportBundle\\Field\\DatabaseHelper')->disableOriginalConstructor()->getMock();
     $this->strategy = $this->createStrategy();
     /** @var ContextInterface $context */
     $context = $this->getMock('Oro\\Bundle\\ImportExportBundle\\Context\\ContextInterface');
     $this->strategy->setImportExportContext($context);
     $this->strategy->setEntityName('Oro\\Bundle\\EntityConfigBundle\\Entity\\FieldConfigModel');
     $this->strategy->setFieldTypeProvider($this->fieldTypeProvider);
     $this->strategy->setTranslator($this->translator);
 }
示例#8
0
 /**
  * {@inheritdoc}
  */
 public function fillEntityData($key, $entity)
 {
     if (!$entity instanceof FieldConfigModel) {
         return;
     }
     /** @var FieldConfigModel $entity */
     $entity->setType($key)->setFieldName('field_' . $key);
     foreach ($this->fieldTypeProvider->getFieldProperties($key) as $scope => $properties) {
         $values = [];
         foreach ($properties as $code => $config) {
             if ($scope === 'enum' && $code === 'enum_options') {
                 $values = array_merge($values, $this->getEnumValues());
             } else {
                 $value = $this->getValueFromConfig($config);
                 if (is_bool($value)) {
                     $value = $value ? 'yes' : 'no';
                 }
                 $values[$code] = $value === null ? $code . '_value' : $value;
             }
         }
         $entity->fromArray($scope, $values, []);
     }
 }
 /**
  * @param FieldConfigModel $model
  * @param array $options
  */
 protected function updateModelConfig(FieldConfigModel $model, array $options)
 {
     $fieldProperties = $this->fieldTypeProvider->getFieldProperties($model->getType());
     foreach ($fieldProperties as $scope => $properties) {
         $values = [];
         foreach ($properties as $code => $config) {
             if (!isset($options[$scope][$code])) {
                 continue;
             }
             $value = $this->denormalizeFieldValue(isset($config['options']) ? $config['options'] : [], $options[$scope][$code], $model->getType());
             if ($value !== null) {
                 $values[$code] = $value;
             }
         }
         $model->fromArray($scope, $values, []);
     }
 }
示例#10
0
 /**
  * @dataProvider fillEntityDataDataProvider
  *
  * @param string $type
  * @param object $entity
  * @param array $properties
  * @param object $expected
  */
 public function testFillEntityData($type, $entity, array $properties, $expected)
 {
     $this->fieldTypeProvider->expects($entity instanceof FieldConfigModel ? $this->once() : $this->never())->method('getFieldProperties')->with($type)->willReturn($properties);
     $this->fixture->fillEntityData($type, $entity);
     $this->assertEquals($expected, $entity);
 }