See also: ExecutionContextInterface
Since: 2.5
Author: Bernhard Schussek (bschussek@gmail.com)
Inheritance: implements Symfony\Component\Validator\Context\ExecutionContextInterface
 public function testValidateWithTypeRegistered()
 {
     $value = 'foobar';
     $this->registry->expects($this->once())->method('has')->with($value)->willReturn(true);
     $this->context->expects($this->never())->method('buildViolation');
     $this->subject->validate($value, new JobType());
 }
Ejemplo n.º 2
0
 public function constraintValidation(ExecutionContext $context)
 {
     $customNotBlankConstraint = new NotBlank();
     $customNotBlankConstraint->message = "Este valor no puede estar vacio";
     $customLengthConstraint = new Length(16);
     $customLengthConstraint->minMessage = "cardNoMinLength";
     $customLengthConstraint->maxMessage = "cardNoMaxLength";
     $collectionConstraint = new Collection(array("firstname" => array($customNotBlankConstraint), "lastname" => array($customNotBlankConstraint), "cardNo" => array(new Regex("/^\\d+\$/"), $customNotBlankConstraint, $customLengthConstraint), "cardType" => array($customNotBlankConstraint), "CVV" => array($customNotBlankConstraint)));
     /**
      * validateValue expects either a scalar value and its constraint or an array and a constraint Collection
      */
     $errors = $this->validator->validateValue(array("firstname" => $this->firstname, "lastname" => $this->lastname, "cardNo" => $this->cardNo, "cardType" => $this->cardType, "CVV" => $this->CVV), $collectionConstraint);
     /**
      * Count is used as this is not an array but a ConstraintViolationList
      */
     if (count($errors) !== 0) {
         $path = $context->getPropertyPath();
         foreach ($errors as $error) {
             $string = str_replace('[', '', $error->getPropertyPath());
             $string = str_replace(']', '', $string);
             $propertyPath = $path . '.' . $string;
             $context->addViolationAt($string, $error->getMessage(), array(), null);
         }
     }
 }
Ejemplo n.º 3
0
 public function validate($data, ExecutionContext $context, $param = null)
 {
     $parameters = $this->app['request']->get('install_step4');
     if ($parameters['database'] != 'pdo_sqlite') {
         $context->validateValue($data, array(new Assert\NotBlank()));
     }
 }
 /**
  * @dataProvider provideInvalidExpressions
  * @param string $expression
  */
 public function testValidateWithInvalidExpression($expression)
 {
     $builder = $this->createMock(ConstraintViolationBuilderInterface::class);
     $this->context->expects($this->once())->method('buildViolation')->willReturn($builder);
     $builder->expects($this->once())->method('setParameter')->with('{{string}}', $expression)->willReturn($builder);
     $builder->expects($this->once())->method('addViolation');
     $this->subject->validate($expression, new CronExpression());
 }
 public function testValidateWithTypeRegistered()
 {
     $validator = $this->createMock(ValidatorInterface::class);
     $contextualValidator = $this->createMock(ContextualValidatorInterface::class);
     $constraint = $this->createMock(Constraint::class);
     $this->registry->expects($this->once())->method('has')->with('ScheduleType')->willReturn(true);
     $this->registry->expects($this->once())->method('get')->with('ScheduleType')->willReturn($constraint);
     $this->context->expects($this->once())->method('getValidator')->willReturn($validator);
     $validator->expects($this->once())->method('inContext')->with($this->context)->willReturn($contextualValidator);
     $contextualValidator->expects($this->once())->method('validate')->with('foobar', $constraint);
     $this->subject->validate('foobar', new Expression(['type' => 'ScheduleType']));
 }
Ejemplo n.º 6
0
 public function testValidateWithType()
 {
     $job = new Job();
     $job->setType('foobar');
     $job->setParameters(['JobParameters']);
     $validator = $this->createMock(ValidatorInterface::class);
     $contextualValidator = $this->createMock(ContextualValidatorInterface::class);
     $this->context->expects($this->once())->method('getValidator')->willReturn($validator);
     $validator->expects($this->once())->method('inContext')->with($this->context)->willReturn($contextualValidator);
     $contextualValidator->expects($this->once())->method('atPath')->with('parameters')->willReturn($contextualValidator);
     $contextualValidator->expects($this->once())->method('validate')->with(['JobParameters'], new Parameters(['type' => $job->getType()]));
     $this->subject->validate($job, new JobConstraint());
 }
 public function testValidateWithType()
 {
     $value = new \Abc\Bundle\SchedulerBundle\Model\Schedule();
     $value->setType('foobar');
     $value->setExpression('ScheduleExpression');
     $validator = $this->createMock(ValidatorInterface::class);
     $contextualValidator = $this->createMock(ContextualValidatorInterface::class);
     $this->context->expects($this->once())->method('getValidator')->willReturn($validator);
     $validator->expects($this->once())->method('inContext')->with($this->context)->willReturn($contextualValidator);
     $contextualValidator->expects($this->once())->method('atPath')->with('expression')->willReturn($contextualValidator);
     $contextualValidator->expects($this->once())->method('validate')->with('ScheduleExpression', new Expression(['type' => $value->getType()]));
     $this->subject->validate($value, new Schedule());
 }
 /**
  * @dataProvider provideValuesWithConstraints
  * @param array $parameters
  * @param array $constraints
  */
 public function testValidateWithValue(array $parameters, array $constraints)
 {
     $validator = $this->createMock(ValidatorInterface::class);
     $contextualValidator = $this->createMock(ContextualValidatorInterface::class);
     $provider = $this->createMock(ConstraintProviderInterface::class);
     $this->subject->register($provider);
     $this->registry->expects($this->once())->method('getTypeChoices')->willReturn(['foobar']);
     $provider->expects($this->any())->method('getConstraints')->with('foobar')->willReturn($constraints);
     $this->context->expects($this->once())->method('getValidator')->willReturn($validator);
     $validator->expects($this->once())->method('inContext')->with($this->context)->willReturn($contextualValidator);
     $contextualValidator->expects($this->exactly(count($constraints)))->method('atPath')->willReturn($contextualValidator);
     $contextualValidator->expects($this->exactly(count($constraints)))->method('validate');
     $this->subject->validate($parameters, new ParametersConstraint(['type' => 'foobar']));
 }
 /**
  * {@inheritdoc}
  */
 public function addViolation($message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
 {
     if (func_num_args() > 2) {
         $this->buildViolation($message, $parameters)->setInvalidValue($invalidValue)->setPlural($plural)->setCode($code)->addViolation();
         return;
     }
     parent::addViolation($message, $parameters);
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function addViolation($message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
 {
     if (func_num_args() > 2) {
         trigger_error('The parameters $invalidValue, $plural and $code in method ' . __METHOD__ . ' are deprecated since version 2.5 and will be removed in 3.0. Use the ' . __CLASS__ . '::buildViolation method instead.', E_USER_DEPRECATED);
         $this->buildViolation($message, $parameters)->setInvalidValue($invalidValue)->setPlural($plural)->setCode($code)->addViolation();
         return;
     }
     parent::addViolation($message, $parameters);
 }
Ejemplo n.º 11
0
 /**
  * Test validate()
  *
  * @return void
  */
 public function testValidateAllowedNeeded()
 {
     $extref = ExtRef::create(__FILE__, __FUNCTION__);
     $constraint = new ExtReference();
     $constraint->collections = [__FILE__];
     $this->context->expects($this->never())->method('addViolation');
     $validator = $this->createValidator();
     $validator->validate($extref, $constraint);
 }
Ejemplo n.º 12
0
 protected function createContext()
 {
     $translator = $this->getMockBuilder('Symfony\\Component\\Translation\\TranslatorInterface')->getMock();
     $validator = $this->getMockBuilder('Symfony\\Component\\Validator\\Validator\\ValidatorInterface')->getMock();
     $contextualValidator = $this->getMockBuilder('Symfony\\Component\\Validator\\Validator\\ContextualValidatorInterface')->getMock();
     $context = new ExecutionContext($validator, $this->root, $translator);
     $context->setGroup($this->group);
     $context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath);
     $context->setConstraint($this->constraint);
     $validator->expects($this->any())->method('inContext')->with($context)->will($this->returnValue($contextualValidator));
     return $context;
 }
 /**
  * Testing circular reference
  *
  * @return void
  */
 public function testCircularReferenceFatalErrorOnGetParents()
 {
     $this->context->expects($this->once())->method('addViolationAt')->with('Parent', sprintf('You cannot set object #%s as parent for object #%s because of circular reference', 'test2', 'test1'));
     $channel1 = new CircularReferenceEntity();
     $channel1->setIds(array('id' => 'test1'));
     $channel2 = new CircularReferenceEntity();
     $channel2->setIds(array('id' => 'test2'));
     $channel3 = new CircularReferenceEntity();
     $channel3->setIds(array('id' => 'test3'));
     $constraint = new CircularReference();
     /*
      * Validation should go after each item
      * because we don't have batch update
      * operations for now
      */
     $channel2->setParent($channel1);
     $this->validator->validate($channel2, $constraint);
     $channel1->setParent($channel2);
     $this->validator->validate($channel1, $constraint);
     $channel3->setParent($channel2);
     $this->validator->validate($channel3, $constraint);
 }
Ejemplo n.º 14
0
 /**
  * Creates a new context.
  *
  * @see ExecutionContext::__construct()
  *
  * @internal Called by {@link LegacyExecutionContextFactory}. Should not be used
  *           in user code.
  */
 public function __construct(ValidatorInterface $validator, $root, MetadataFactoryInterface $metadataFactory, TranslatorInterface $translator, $translationDomain = null)
 {
     parent::__construct($validator, $root, $translator, $translationDomain);
     $this->metadataFactory = $metadataFactory;
 }
Ejemplo n.º 15
0
 public function validateConstraintWithCustomDomain(ExecutionContext $context)
 {
     $context->buildViolation('entity.custom-domain')->setTranslationDomain('custom-domain')->addViolation();
 }
Ejemplo n.º 16
0
 /**
  * @Assert\Callback
  */
 public function validate(ExecutionContext $context)
 {
     $parentTaxon = $this->getParent();
     while ($parentTaxon) {
         if ($parentTaxon == $this) {
             $context->buildViolation('Circular taxonomy reference detected. Please check your parent taxonomy.')->atPath('parent')->addViolation();
             break;
         }
         $parentTaxon = $parentTaxon->getParent();
     }
 }
Ejemplo n.º 17
0
 /**
  * callback-валидатор для dailyBudget
  *
  * @param ExecutionContext $context
  *
  * @Assert\Callback(
  *      groups={"default"}
  * )
  */
 public function isDailyLimitValid(ExecutionContext $context)
 {
     if ($this->dailyBudget != 0 && $this->dailyBudget * 10 < $this->generalBudget) {
         $context->addViolationAt('dailyBudget', 'Дневной бюджет не должен быть меньше 10% от бюджета кампании');
     }
 }