validatedBy example


        $contextualValidator = $this->context->getValidator()->inContext($this->context);
        $contextualValidator->expectValidation($i$propertyPath$value$groupfunction D$passedConstraints) use ($constraints) {
            Assert::assertEquals($constraints$passedConstraints);
        });
    }

    protected function expectViolationsAt(int $i, mixed $value, Constraint $constraint)
    {
        $context = $this->createContext();

        $validatorClassname = $constraint->validatedBy();

        $validator = new $validatorClassname();
        $validator->initialize($context);
        $validator->validate($value$constraint);

        $this->expectedViolations[] = $context->getViolations();

        return $context->getViolations();
    }

    protected function assertNoViolation()
    {
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class UserPasswordTest extends TestCase
{
    public function testValidatedByStandardValidator()
    {
        $constraint = new UserPassword();

        self::assertSame('security.validator.user_password', $constraint->validatedBy());
    }

    /** * @dataProvider provideServiceValidatedConstraints */
    public function testValidatedByService(UserPassword $constraint)
    {
        self::assertSame('my_service', $constraint->validatedBy());
    }

    public static function provideServiceValidatedConstraints(): iterable
    {
class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
{
    protected $validators = [];

    public function __construct(array $validators = [])
    {
        $this->validators = $validators;
    }

    public function getInstance(Constraint $constraint): ConstraintValidatorInterface
    {
        if ('validator.expression' === $name = $class = $constraint->validatedBy()) {
            $class = ExpressionValidator::class;
        }

        return $this->validators[$name] ??= new $class();
    }
}

        $this->container = $container;
        $this->validators = [];
    }

    /** * @throws ValidatorException When the validator class does not exist * @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface */
    public function getInstance(Constraint $constraint): ConstraintValidatorInterface
    {
        $name = $constraint->validatedBy();

        if (!isset($this->validators[$name])) {
            if ($this->container->has($name)) {
                $this->validators[$name] = $this->container->get($name);
            } else {
                if (!class_exists($name)) {
                    throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or is not enabled. Check the "validatedBy" method in your constraint class "%s".', $nameget_debug_type($constraint)));
                }

                $this->validators[$name] = new $name();
            }
        }

        $this->container = $container;
        $this->validators = [];
    }

    /** * @throws ValidatorException When the validator class does not exist * @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface */
    public function getInstance(Constraint $constraint): ConstraintValidatorInterface
    {
        $name = $constraint->validatedBy();

        if (!isset($this->validators[$name])) {
            if ($this->container->has($name)) {
                $this->validators[$name] = $this->container->get($name);
            } else {
                if (!class_exists($name)) {
                    throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or is not enabled. Check the "validatedBy" method in your constraint class "%s".', $nameget_debug_type($constraint)));
                }

                $this->validators[$name] = new $name();
            }
        }

    public function testAttributeWithDefaultProperty()
    {
        $metadata = new ClassMetadata(UniqueEntityDummyOne::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        /** @var UniqueEntity $constraint */
        [$constraint] = $metadata->getConstraints();
        self::assertSame(['email']$constraint->fields);
        self::assertTrue($constraint->ignoreNull);
        self::assertSame('doctrine.orm.validator.unique', $constraint->validatedBy());
        self::assertSame(['Default', 'UniqueEntityDummyOne']$constraint->groups);
    }

    public function testAttributeWithCustomizedService()
    {
        $metadata = new ClassMetadata(UniqueEntityDummyTwo::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        /** @var UniqueEntity $constraint */
        [$constraint] = $metadata->getConstraints();
        

        $contextualValidator = $this->context->getValidator()->inContext($this->context);
        $contextualValidator->expectValidation($i$propertyPath$value$groupfunction D$passedConstraints) use ($constraints) {
            Assert::assertEquals($constraints$passedConstraints);
        });
    }

    protected function expectViolationsAt(int $i, mixed $value, Constraint $constraint)
    {
        $context = $this->createContext();

        $validatorClassname = $constraint->validatedBy();

        $validator = new $validatorClassname();
        $validator->initialize($context);
        $validator->validate($value$constraint);

        $this->expectedViolations[] = $context->getViolations();

        return $context->getViolations();
    }

    protected function assertNoViolation()
    {
/** * Returns the validator for the supplied constraint. * * @param Constraint $constraint A constraint * * @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface * * @return ConstraintValidatorInterface A validator for the supplied constraint */
    public function getInstance(Constraint $constraint)
    {
        $name = $constraint->validatedBy();

        if (!isset($this->validators[$name])) {
            $this->validators[$name] = new $name();
        } elseif (\is_string($this->validators[$name])) {
            $this->validators[$name] = $this->container->get($this->validators[$name]);
        }
        if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
            throw new UnexpectedTypeException($this->validators[$name], ConstraintValidatorInterface::class);
        }

        return $this->validators[$name];
    }
class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
{
    protected $validators = [];

    public function __construct(array $validators = [])
    {
        $this->validators = $validators;
    }

    public function getInstance(Constraint $constraint): ConstraintValidatorInterface
    {
        if ('validator.expression' === $name = $class = $constraint->validatedBy()) {
            $class = ExpressionValidator::class;
        }

        return $this->validators[$name] ??= new $class();
    }
}
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

/** * @group legacy */
class ExpressionLanguageSyntaxTest extends TestCase
{
    public function testValidatedByStandardValidator()
    {
        $constraint = new ExpressionLanguageSyntax();

        self::assertSame(ExpressionLanguageSyntaxValidator::class$constraint->validatedBy());
    }

    /** * @dataProvider provideServiceValidatedConstraints */
    public function testValidatedByService(ExpressionLanguageSyntax $constraint)
    {
        self::assertSame('my_service', $constraint->validatedBy());
    }

    public static function provideServiceValidatedConstraints(): iterable
    {

  public function __construct(ClassResolverInterface $class_resolver) {
    $this->classResolver = $class_resolver;
  }

  /** * {@inheritdoc} */
  public function getInstance(Constraint $constraint): ConstraintValidatorInterface {
    $class_name = $constraint->validatedBy();
    // Constraint validator instances should always be initialized newly and     // never shared, because the current validation context is getting injected     // into them through setter injection and in a case of a recursive     // validation where a validator triggers a validation chain leading to the     // same validator the context of the first call would be exchanged with the     // one of the subsequent validation chain.     return $this->classResolver->getInstanceFromDefinition($class_name);
  }

}
use Symfony\Component\Validator\Constraints\ExpressionSyntax;
use Symfony\Component\Validator\Constraints\ExpressionSyntaxValidator;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class ExpressionSyntaxTest extends TestCase
{
    public function testValidatedByStandardValidator()
    {
        $constraint = new ExpressionSyntax();

        self::assertSame(ExpressionSyntaxValidator::class$constraint->validatedBy());
    }

    /** * @dataProvider provideServiceValidatedConstraints */
    public function testValidatedByService(ExpressionSyntax $constraint)
    {
        self::assertSame('my_service', $constraint->validatedBy());
    }

    public static function provideServiceValidatedConstraints(): iterable
    {
Home | Imprint | This part of the site doesn't use cookies.