public function testApplyEnumOptionsWithDuplicatedIdsAndGeneratedIdEqualsRemovingId()
 {
     $enumValueClassName = 'Test\\EnumValue';
     $locale = 'fr';
     $enumOptions = [['id' => 'option_1', 'label' => 'Existing Option 1', 'priority' => 1, 'is_default' => true], ['id' => 'option_1_1', 'label' => 'Existing Option 11', 'priority' => 3, 'is_default' => false], ['id' => '', 'label' => 'Option 1', 'priority' => 2, 'is_default' => true]];
     $value1 = new TestEnumValue('option_1', 'Existing Option 1', 1, true);
     $value2 = new TestEnumValue('option_1_1', 'Existing Option 11', 3, true);
     $value3 = new TestEnumValue('option_1_2', 'Existing Option 12', 2, false);
     $newValue = new TestEnumValue('option_1_2', 'Option 1', 2, true);
     $values = [$value1, $value2, $value3];
     $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $this->doctrine->expects($this->once())->method('getManagerForClass')->with($enumValueClassName)->will($this->returnValue($em));
     $enumRepo = $this->setApplyEnumOptionsQueryExpectation($em, $enumValueClassName, $locale, $values);
     $em->expects($this->once())->method('remove')->with($this->identicalTo($value3));
     $enumRepo->expects($this->once())->method('createEnumValue')->with('Option 1', 2, true, 'option_1_2')->will($this->returnValue($newValue));
     $em->expects($this->once())->method('persist')->with($this->identicalTo($newValue));
     $em->expects($this->once())->method('flush');
     $this->dbTranslationMetadataCache->expects($this->once())->method('updateTimestamp')->with($locale);
     $this->synchronizer->applyEnumOptions($enumValueClassName, $enumOptions, $locale);
     $expectedValue1 = new TestEnumValue('option_1', 'Existing Option 1', 1, true);
     $this->assertEquals($expectedValue1, $value1);
     $expectedValue2 = new TestEnumValue('option_1_1', 'Existing Option 11', 3, false);
     $expectedValue2->setLocale($locale);
     $this->assertEquals($expectedValue2, $value2);
     $expectedNewValue = new TestEnumValue('option_1_2', 'Option 1', 2, true);
     $expectedNewValue->setLocale($locale);
     $this->assertEquals($expectedNewValue, $newValue);
 }
 /**
  * Post submit event handler
  *
  * @param FormEvent $event
  *
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function postSubmit(FormEvent $event)
 {
     $form = $event->getForm();
     $configModel = $form->getConfig()->getOption('config_model');
     if (!$configModel instanceof FieldConfigModel) {
         return;
     }
     if (!in_array($configModel->getType(), ['enum', 'multiEnum'])) {
         return;
     }
     if (!$form->isValid()) {
         return;
     }
     $data = $event->getData();
     $enumConfig = $configModel->toArray('enum');
     $enumName = $this->getValue($data['enum'], 'enum_name');
     $enumCode = $this->getValue($enumConfig, 'enum_code');
     if (empty($enumCode)) {
         $enumCode = $enumName !== null ? ExtendHelper::buildEnumCode($enumName) : ExtendHelper::generateEnumCode($configModel->getEntity()->getClassName(), $configModel->getFieldName());
     }
     $locale = $this->translator->getLocale();
     $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
     $enumConfigProvider = $this->configManager->getProvider('enum');
     if ($enumConfigProvider->hasConfig($enumValueClassName)) {
         // existing enum
         if ($configModel->getId()) {
             if ($enumName !== null) {
                 $this->enumSynchronizer->applyEnumNameTrans($enumCode, $enumName, $locale);
             }
             $enumOptions = $this->getValue($data['enum'], 'enum_options');
             if ($enumOptions !== null) {
                 $this->enumSynchronizer->applyEnumOptions($enumValueClassName, $enumOptions, $locale);
             }
             $enumPublic = $this->getValue($data['enum'], 'enum_public');
             if ($enumPublic !== null) {
                 $this->enumSynchronizer->applyEnumEntityOptions($enumValueClassName, $enumPublic);
             }
         }
         unset($data['enum']['enum_name']);
         unset($data['enum']['enum_options']);
         unset($data['enum']['enum_public']);
         $event->setData($data);
     } else {
         // new enum
         $this->sortOptions($data['enum']['enum_options']);
         $data['enum']['enum_locale'] = $locale;
         $event->setData($data);
     }
 }
Example #3
0
 /**
  * @param array $data
  * @param string $className
  * @param string $fieldName
  */
 protected function setEnumData(array $data, $className, $fieldName)
 {
     $provider = $this->configManager->getProvider('enum');
     if (!$provider) {
         return;
     }
     $enumCode = $provider->getConfig($className, $fieldName)->get('enum_code');
     if (!$enumCode || !isset($data['enum_options'])) {
         return;
     }
     $enumValueClassName = ExtendHelper::buildEnumValueClassName($enumCode);
     $enumOptions = array_map(function ($option) {
         if (!isset($option['id'])) {
             $option['id'] = $option['label'];
         }
         return $option;
     }, $data['enum_options']);
     if ($provider->hasConfig($enumValueClassName)) {
         $this->enumSynchronizer->applyEnumOptions($enumValueClassName, $enumOptions, $this->translationHelper->getLocale());
     }
 }