function it_validates_attribute_value_based_on_their_type($attributeTypesRegistry, AttributeInterface $attribute, AttributeTypeInterface $attributeType, AttributeValueInterface $attributeValue, ValidAttributeValue $attributeValueConstraint)
 {
     $attributeValue->getType()->willReturn(TextAttributeType::TYPE);
     $attributeTypesRegistry->get('text')->willReturn($attributeType);
     $attributeValue->getAttribute()->willReturn($attribute);
     $attribute->getConfiguration()->willReturn(array('min' => 2, 'max' => 255));
     $attributeType->validate($attributeValue, Argument::any(ExecutionContextInterface::class), array('min' => 2, 'max' => 255))->shouldBeCalled();
     $this->validate($attributeValue, $attributeValueConstraint);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function validate(AttributeValueInterface $attributeValue, ExecutionContextInterface $context, array $configuration)
 {
     if (!isset($configuration['min']) || !isset($configuration['max'])) {
         return;
     }
     $value = $attributeValue->getValue();
     foreach ($this->getValidationErrors($context, $value, $configuration) as $error) {
         $context->buildViolation($error->getMessage())->atPath('value')->addViolation();
     }
 }
 function it_builds_options_base_on_product_attribute(FormEvent $event, Form $form, AttributeValueInterface $productAttribute, Form $valueField, $formFactory)
 {
     $productAttribute->getType()->willReturn('choice');
     $productAttribute->getConfiguration()->willReturn(array('choices' => array('red' => 'Red', 'blue' => 'Blue')));
     $productAttribute->getName()->willReturn('My name');
     $event->getData()->willReturn($productAttribute);
     $event->getForm()->willReturn($form);
     $formFactory->createNamed('value', 'choice', null, array('label' => 'My name', 'auto_initialize' => false, 'choices' => array('red' => 'Red', 'blue' => 'Blue')))->willReturn($valueField)->shouldBeCalled();
     $form->remove('attribute')->shouldBeCalled()->willReturn($form);
     $form->add($valueField)->shouldBeCalled()->willReturn($form);
     $this->buildForm($event);
 }
 function it_checks_if_attribute_value_is_valid(AttributeInterface $attribute, AttributeValueInterface $attributeValue, ConstraintViolationBuilderInterface $constraintViolationBuilder, ConstraintViolationInterface $constraintViolation, ConstraintViolationListInterface $constraintViolationList, ExecutionContextInterface $context, ValidatorInterface $validator)
 {
     $attributeValue->getAttribute()->willReturn($attribute);
     $attributeValue->getValue()->willReturn('X');
     $context->getValidator()->willReturn($validator);
     $validator->validate('X', Argument::type(Length::class))->willReturn($constraintViolationList);
     $constraintViolationList->rewind()->shouldBeCalled();
     $constraintViolationList->valid()->willReturn(true, false);
     $constraintViolationList->current()->willReturn($constraintViolation);
     $constraintViolationList->next()->shouldBeCalled();
     $constraintViolation->getMessage()->willReturn('error message');
     $context->buildViolation('error message')->willReturn($constraintViolationBuilder);
     $constraintViolationBuilder->atPath('value')->willReturn($constraintViolationBuilder);
     $constraintViolationBuilder->addViolation()->shouldBeCalled();
     $this->validate($attributeValue, $context, ['min' => 2, 'max' => 255]);
 }
 /**
  * Verify value before set to form.
  *
  * @param AttributeValueInterface $attributeValue
  */
 protected function verifyValue(AttributeValueInterface $attributeValue)
 {
     switch ($attributeValue->getType()) {
         case AttributeTypes::CHECKBOX:
             if (!is_bool($attributeValue->getValue())) {
                 $attributeValue->setValue(false);
             }
             break;
         case AttributeTypes::MONEY:
         case AttributeTypes::NUMBER:
         case AttributeTypes::PERCENTAGE:
             if (!is_numeric($attributeValue->getValue())) {
                 $attributeValue->setValue(null);
             }
             break;
     }
 }