/**
  * Environment setup
  */
 protected function setUp()
 {
     $this->attributeTypeMock = $this->getMock('OroB2B\\Bundle\\AttributeBundle\\AttributeType\\AttributeTypeInterface');
     $this->attributeTypeMock->expects($this->any())->method('getName')->will($this->returnValue(self::ATTRIBUTE_TYPE_NAME));
     /** @var \Symfony\Component\Translation\TranslatorInterface $translator */
     $translator = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface');
     $this->registry = new AttributeTypeRegistry($translator);
     $this->registry->addType($this->attributeTypeMock);
 }
 /**
  * @param Attribute $attribute
  * @return AttributeTypeInterface
  */
 protected function getAttributeType(Attribute $attribute)
 {
     $type = $attribute->getType();
     if (!$type) {
         throw new TransformationFailedException('Attribute type is not defined');
     }
     $typeObject = $this->typeRegistry->getTypeByName($type);
     if (!$typeObject) {
         throw new TransformationFailedException(sprintf('Unknown attribute type "%s"', $type));
     }
     return $typeObject;
 }
 /**
  * @param FormBuilderInterface $builder
  * @param AttributeTypeInterface $attributeType
  */
 protected function addPropertyFields(FormBuilderInterface $builder, AttributeTypeInterface $attributeType)
 {
     $builder->add('onProductView', WebsitePropertyType::NAME, ['label' => 'orob2b.attribute.attributeproperty.fields.on_product_view', 'required' => false, 'type' => 'checkbox'])->add('inProductListing', WebsitePropertyType::NAME, ['label' => 'orob2b.attribute.attributeproperty.fields.in_product_listing', 'required' => false, 'type' => 'checkbox'])->add('useInSorting', WebsitePropertyType::NAME, ['label' => 'orob2b.attribute.attributeproperty.fields.use_in_sorting', 'required' => false, 'type' => 'checkbox'])->add('onAdvancedSearch', WebsitePropertyType::NAME, ['label' => 'orob2b.attribute.attributeproperty.fields.on_advanced_search', 'required' => false, 'type' => 'checkbox'])->add('onProductComparison', WebsitePropertyType::NAME, ['label' => 'orob2b.attribute.attributeproperty.fields.on_product_comparison', 'required' => false, 'type' => 'checkbox']);
     if ($attributeType->isContainHtml()) {
         $builder->add('containHtml', 'checkbox', ['label' => 'orob2b.attribute.contain_html.label', 'required' => false]);
     }
     if ($attributeType->isUsedForSearch()) {
         $builder->add('useForSearch', WebsitePropertyType::NAME, ['label' => 'orob2b.attribute.attributeproperty.fields.use_for_search', 'required' => false, 'type' => 'checkbox']);
     }
     if ($attributeType->isUsedInFilters()) {
         $builder->add('useInFilters', WebsitePropertyType::NAME, ['label' => 'orob2b.attribute.attributeproperty.fields.use_in_filters', 'required' => false, 'type' => 'checkbox']);
     }
 }
 /**
  * @param AttributeTypeInterface $attributeType
  * @return array
  */
 protected function getAttributeTypePropertyFields($attributeType)
 {
     $fields = ['onProductView', 'inProductListing', 'useInSorting', 'onAdvancedSearch', 'onProductComparison'];
     if ($attributeType->isContainHtml()) {
         $fields[] = 'containHtml';
     }
     if ($attributeType->isUsedForSearch()) {
         $fields[] = 'useForSearch';
     }
     if ($attributeType->isUsedInFilters()) {
         $fields[] = 'useInFilters';
     }
     return $fields;
 }
 /**
  * @param AttributeTypeInterface|OptionAttributeTypeInterface $type
  * @param array $expected
  * @param Attribute|null $attribute
  * @param array $normalizationData
  * @dataProvider attributeTypeDataProvider
  */
 public function testAttributeTypes(AttributeTypeInterface $type, array $expected, Attribute $attribute = null, array $normalizationData = [])
 {
     if (!$attribute) {
         $attribute = new Attribute();
     }
     $this->assertEquals($expected['name'], $type->getName());
     $this->assertEquals($expected['typeField'], $type->getDataTypeField());
     $this->assertEquals($expected['isContainHtml'], $type->isContainHtml());
     $this->assertEquals($expected['isUsedForSearch'], $type->isUsedForSearch());
     $this->assertEquals($expected['isUsedInFilters'], $type->isUsedInFilters());
     $this->assertEquals($expected['formParameters'], $type->getFormParameters($attribute));
     $this->assertEquals($expected['requiredConstraints'], $type->getRequiredConstraints());
     $this->assertEquals($expected['optionalConstraints'], $type->getOptionalConstraints());
     $this->assertEquals($expected['canBeUnique'], $type->canBeUnique());
     $this->assertEquals($expected['canBeRequired'], $type->canBeRequired());
     if (isset($expected['defaultFormParameters'])) {
         $this->assertEquals($expected['defaultFormParameters'], $type->getDefaultValueFormParameters($attribute));
     }
     $testValue = 'test';
     if (!empty($normalizationData['normalize'])) {
         foreach ($normalizationData['normalize'] as $value) {
             $this->assertSame($value['to'], $type->normalize($value['from']));
         }
     } else {
         $this->assertSame($testValue, $type->normalize($testValue));
     }
     if (!empty($normalizationData['denormalize'])) {
         foreach ($normalizationData['denormalize'] as $value) {
             $this->assertSame($value['to'], $type->denormalize($value['from']));
         }
     } else {
         $this->assertSame($testValue, $type->denormalize($testValue));
     }
 }
 /**
  * @param Attribute $attribute
  * @param AttributeTypeInterface $attributeType
  * @param int|null $localeId
  * @param mixed $value
  */
 protected function setDefaultValueByField(Attribute $attribute, AttributeTypeInterface $attributeType, $localeId, $value)
 {
     $field = $attributeType->getDataTypeField();
     $defaultValue = $attribute->getDefaultValueByLocaleId($localeId);
     if (!$defaultValue) {
         $defaultValue = new AttributeDefaultValue();
         if ($localeId) {
             $defaultValue->setLocale($this->databaseHelper->findLocale($localeId));
         }
         $attribute->addDefaultValue($defaultValue);
     }
     if ($value instanceof FallbackType) {
         $this->propertyAccessor->setValue($defaultValue, $field, null);
         $defaultValue->setFallback($value->getType());
     } else {
         $this->propertyAccessor->setValue($defaultValue, $field, $attributeType->denormalize($value));
         $defaultValue->setFallback(null);
     }
 }