AttributeLoader example

/** * @dataProvider getIbansWithInvalidCountryCode */
    public function testIbansWithInvalidCountryCode($iban)
    {
        $this->assertViolationRaised($iban, Iban::INVALID_COUNTRY_CODE_ERROR);
    }

    public function testLoadFromAttribute()
    {
        $classMetadata = new ClassMetadata(IbanDummy::class);
        (new AttributeLoader())->loadClassMetadata($classMetadata);

        [$constraint] = $classMetadata->properties['iban']->constraints;

        $this->validator->validate('DE89 3704 0044 0532 0130 01', $constraint);

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', '"DE89 3704 0044 0532 0130 01"')
            ->setCode(Iban::CHECKSUM_FAILED_ERROR)
            ->assertRaised();
    }

    
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Json;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class JsonTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(JsonDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(['Default', 'JsonDummy']$bConstraint->groups);

        [$cConstraint] = $metadata->properties['c']->getConstraints();
        self::assertSame(['my_group']$cConstraint->groups);
        self::assertSame('some attached data', $cConstraint->payload);
    }
}

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class UniqueEntityTest extends TestCase
{
    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()
    {
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Time;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class TimeTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(TimeDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(['Default', 'TimeDummy']$bConstraint->groups);

        [$cConstraint] = $metadata->properties['c']->getConstraints();
        self::assertSame(['my_group']$cConstraint->groups);
        self::assertSame('some attached data', $cConstraint->payload);
    }
}

use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\IdenticalTo;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class IdenticalToTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(IdenticalToDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame(2, $aConstraint->value);
        self::assertNull($aConstraint->propertyPath);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame(4711, $bConstraint->value);
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(['Default', 'IdenticalToDummy']$bConstraint->groups);

        [
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Negative;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class NegativeTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(NegativeDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame(0, $aConstraint->value);
        self::assertNull($aConstraint->propertyPath);
        self::assertSame(['Default', 'NegativeDummy']$aConstraint->groups);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(['foo']$bConstraint->groups);
    }
}
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\DivisibleBy;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class DivisibleByTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(DivisibleByDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame(2, $aConstraint->value);
        self::assertNull($aConstraint->propertyPath);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame(4711, $bConstraint->value);
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(['Default', 'DivisibleByDummy']$bConstraint->groups);

        [
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class ImageTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(ImageDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertNull($aConstraint->minWidth);
        self::assertNull($aConstraint->maxWidth);
        self::assertNull($aConstraint->minHeight);
        self::assertNull($aConstraint->maxHeight);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame(50, $bConstraint->minWidth);
        self::assertSame(200, $bConstraint->maxWidth);
        
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class TypeTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(TypeDummy::class);
        self::assertTrue((new AttributeLoader())->loadClassMetadata($metadata));

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame('integer', $aConstraint->type);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame(\DateTimeImmutable::class$bConstraint->type);
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(['Default', 'TypeDummy']$bConstraint->groups);

        [$cConstraint] = $metadata->properties['c']->getConstraints();
        self::assertSame(['string', 'array']$cConstraint->type);
        
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class CountTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(CountDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame(42, $aConstraint->min);
        self::assertSame(42, $aConstraint->max);
        self::assertNull($aConstraint->divisibleBy);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame(1, $bConstraint->min);
        self::assertSame(4711, $bConstraint->max);
        self::assertNull($bConstraint->divisibleBy);
        
'groups' => ['Default', 'WhenTestWithAnnotations'],
            ]),
            new NotBlank([
                'groups' => ['Default', 'WhenTestWithAnnotations'],
            ]),
        ]$bazConstraint->constraints);
        self::assertSame(['Default', 'WhenTestWithAnnotations']$bazConstraint->groups);
    }

    public function testAttributes()
    {
        $loader = new AttributeLoader();
        $metadata = new ClassMetadata(WhenTestWithAttributes::class);

        self::assertTrue($loader->loadClassMetadata($metadata));

        [$classConstraint] = $metadata->getConstraints();

        self::assertInstanceOf(When::class$classConstraint);
        self::assertSame('true', $classConstraint->expression);
        self::assertEquals([
            new Callback([
                'callback' => 'callback',
                
public function testInvalidNormalizerObjectThrowsException()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).');
        new Ip(['normalizer' => new \stdClass()]);
    }

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

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame(Ip::V4, $aConstraint->version);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame(Ip::V6, $bConstraint->version);
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame('trim', $bConstraint->normalizer);
        self::assertSame(['Default', 'IpDummy']$bConstraint->groups);

        [
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class GreaterThanTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(GreaterThanDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame(2, $aConstraint->value);
        self::assertNull($aConstraint->propertyPath);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame(4711, $bConstraint->value);
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(['Default', 'GreaterThanDummy']$bConstraint->groups);

        [
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\LessThan;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class LessThanTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(LessThanDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame(2, $aConstraint->value);
        self::assertNull($aConstraint->propertyPath);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame(4711, $bConstraint->value);
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(['Default', 'LessThanDummy']$bConstraint->groups);

        [
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\NotIdenticalTo;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class NotIdenticalToTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(NotIdenticalToDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame(2, $aConstraint->value);
        self::assertNull($aConstraint->propertyPath);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame(4711, $bConstraint->value);
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(['Default', 'NotIdenticalToDummy']$bConstraint->groups);

        [
Home | Imprint | This part of the site doesn't use cookies.