protected function setUp() { parent::setUp(); $this->registry = $this->getMockBuilder('OroB2B\\Bundle\\AttributeBundle\\AttributeType\\AttributeTypeRegistry')->getMock(); $this->registry->expects($this->any())->method('getTypeByName')->will($this->returnValueMap([[Integer::NAME, new Integer()], [Float::NAME, new Float()], [String::NAME, new String()], [Boolean::NAME, new Boolean()], [Text::NAME, new Text()], [Date::NAME, new Date()], [DateTime::NAME, new DateTime()]])); $this->formType = new AttributeTypeConstraintType($this->registry); }
/** * {@inheritDoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setRequired(['attribute_type']); $resolver->setDefaults(['empty_value' => 'orob2b.attribute.attribute_type_constraint.none']); $resolver->setNormalizers(['choices' => function (Options $options, $value) { if (!empty($value)) { return $value; } $choices = []; if ($options['attribute_type'] instanceof AttributeTypeInterface) { $constraints = $options['attribute_type']->getOptionalConstraints(); } elseif (is_string($options['attribute_type'])) { $attributeType = $this->attributeTypeRegistry->getTypeByName($options['attribute_type']); if (!$attributeType) { throw new \LogicException(sprintf('Attribute type name "%s" is not exist in attribute type registry.', $options['attribute_type'])); } $constraints = $attributeType->getOptionalConstraints(); } else { throw new UnexpectedTypeException($options['attribute_type'], 'OroB2B\\Bundle\\AttributeBundle\\AttributeType\\AttributeTypeInterface or string'); } foreach ($constraints as $choice) { $choices[$choice->getAlias()] = 'orob2b.validation.constraint.alias.' . $choice->getAlias(); } return $choices; }]); }
/** * @return array */ protected function getChoices() { $choices = []; foreach ($this->registry->getTypes() as $type) { $choices[$type->getName()] = 'orob2b.attribute.attribute_type.' . $type->getName(); } return $choices; }
protected function setUp() { $localeRepository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $localeRepository->expects($this->any())->method('find')->will($this->returnValueMap([[1, $this->getLocale(1)], [2, $this->getLocale(2)]])); $websiteRepository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $websiteRepository->expects($this->any())->method('find')->will($this->returnValueMap([[1, $this->getWebsite(1)], [2, $this->getWebsite(2)]])); $repositoriesMap = [['OroB2BWebsiteBundle:Locale', null, $localeRepository], ['OroB2BWebsiteBundle:Website', null, $websiteRepository]]; $this->managerRegistry = $this->getMock('Doctrine\\Common\\Persistence\\ManagerRegistry'); $this->managerRegistry->expects($this->any())->method('getRepository')->will($this->returnValueMap($repositoriesMap)); $this->typeRegistry = new AttributeTypeRegistry(); $this->typeRegistry->addType(new Text()); $this->typeRegistry->addType(new Integer()); $this->typeRegistry->addType(new Select()); }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $attribute = $options['data']; if (!$attribute instanceof Attribute) { throw new UnexpectedTypeException($attribute, 'Attribute'); } $attributeType = $this->typeRegistry->getTypeByName($attribute->getType()); if (!$attributeType) { throw new LogicException(sprintf('Attribute type "%s" not found', $attribute->getType())); } $this->addMainFields($builder); $this->addValidationFields($builder, $attributeType); if ($attributeType instanceof OptionAttributeTypeInterface) { $this->addDefaultOptionsField($builder, $attribute, $attributeType); } else { $this->addDefaultValueField($builder, $attribute, $attributeType); } $this->addPropertyFields($builder, $attributeType); $builder->addViewTransformer(new AttributeTransformer($this->managerRegistry, $this->typeRegistry, $attribute)); $this->addDisabledFields($builder); $builder->addViewTransformer(new AttributeDisabledFieldsTransformer()); }
/** * @expectedException \Symfony\Component\Form\Exception\LogicException * @expectedExceptionMessage Form type is required for attribute type "invalid" */ public function testBuildFormNoFormTypeOptionsException() { $type = 'invalid'; /** @var \PHPUnit_Framework_MockObject_MockObject|AttributeTypeInterface $invalidAttributeType */ $invalidAttributeType = $this->getMock('OroB2B\\Bundle\\AttributeBundle\\AttributeType\\OptionAttributeTypeInterface'); $invalidAttributeType->expects($this->any())->method('getName')->will($this->returnValue($type)); $invalidAttributeType->expects($this->any())->method('getDefaultValueFormParameters')->will($this->returnValue([])); $this->typeRegistry->addType($invalidAttributeType); $attribute = new Attribute(); $attribute->setType($type); /** @var \PHPUnit_Framework_MockObject_MockObject|FormBuilderInterface $builder */ $builder = $this->getMock('Symfony\\Component\\Form\\FormBuilderInterface'); $builder->expects($this->any())->method('add')->willReturnSelf(); $this->formType->buildForm($builder, ['data' => $attribute]); }
public function testGetTypes() { $this->assertContains($this->attributeTypeMock, $this->registry->getTypes()); }