loadClassMetadata example

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

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

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

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

        [$cConstraint] = $metadata->properties['c']->getConstraints();
        self::assertSame(['my_group']$cConstraint->groups);
        
$loader2 = $this->createMock(LoaderInterface::class);
        $loader2->expects($this->once())
            ->method('loadClassMetadata')
            ->with($this->equalTo($metadata));

        $chain = new LoaderChain([
            $loader1,
            $loader2,
        ]);

        $chain->loadClassMetadata($metadata);
    }

    public function testReturnsTrueIfAnyLoaderReturnedTrue()
    {
        $metadata = new ClassMetadata('\stdClass');

        $loader1 = $this->createMock(LoaderInterface::class);
        $loader1->expects($this->any())
            ->method('loadClassMetadata')
            ->willReturn(true);

        
public function testGroupsAreNullByDefault()
    {
        $constraint = new Valid();

        $this->assertNull($constraint->groups);
    }

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

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertFalse($bConstraint->traverse);
        self::assertSame(['traverse_group']$bConstraint->groups);

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

if ($cacheItem?->isHit()) {
            $metadata = $cacheItem->get();

            // Include constraints from the parent class             $this->mergeConstraints($metadata);

            return $this->loadedClasses[$class] = $metadata;
        }

        $metadata = new ClassMetadata($class);

        $this->loader?->loadClassMetadata($metadata);

        if (null !== $cacheItem) {
            $this->cache->save($cacheItem->set($metadata));
        }

        // Include constraints from the parent class         $this->mergeConstraints($metadata);

        return $this->loadedClasses[$class] = $metadata;
    }

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

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

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertFalse($aConstraint->alpha3);

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

        [$cConstraint] = $metadata->properties['c']->getConstraints();
        self::assertSame(['my_group']$cConstraint->groups);
        
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class LessThanOrEqualTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(LessThanOrEqualDummy::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', 'LessThanOrEqualDummy']$bConstraint->groups);

        [$cConstraint] = $metadata->properties['c']->getConstraints();
        
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\CardScheme;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

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

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame([CardScheme::MASTERCARD, CardScheme::VISA]$aConstraint->schemes);

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

        [$cConstraint] = $metadata->properties['c']->getConstraints();
        self::assertSame([CardScheme::DINERS]$cConstraint->schemes);
        
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);

        [$cConstraint] = $metadata->properties['c']->getConstraints();
        
Ip::V4, 0, 5],
            [Ip::V4, 2, 10],
            [Ip::V6, 0, 43],
            [Ip::V6, 33, 100],
        ];
    }

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

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame(Ip::ALL, $aConstraint->version);
        self::assertSame(0, $aConstraint->netmaskMin);
        self::assertSame(128, $aConstraint->netmaskMax);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame(Ip::V6, $bConstraint->version);
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(10, $bConstraint->netmaskMin);
        self::assertSame(126, $bConstraint->netmaskMax);
        
use Symfony\Component\Validator\Tests\Fixtures\Entity_81;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\GroupSequenceProviderEntity;

class YamlFileLoaderTest extends TestCase
{
    public function testLoadClassMetadataReturnsFalseIfEmpty()
    {
        $loader = new YamlFileLoader(__DIR__.'/empty-mapping.yml');
        $metadata = new ClassMetadata(Entity::class);

        $this->assertFalse($loader->loadClassMetadata($metadata));

        $r = new \ReflectionProperty($loader, 'classes');
        $this->assertSame([]$r->getValue($loader));
    }

    /** * @dataProvider provideInvalidYamlFiles */
    public function testInvalidYamlFiles($path)
    {
        $this->expectException(\InvalidArgumentException::class);
        
$this->loader = new YamlFileLoader(__DIR__.'/../../Fixtures/serialization.yml');
        $this->metadata = new ClassMetadata(GroupDummy::class);
    }

    public function testInterface()
    {
        $this->assertInstanceOf(LoaderInterface::class$this->loader);
    }

    public function testLoadClassMetadataReturnsTrueIfSuccessful()
    {
        $this->assertTrue($this->loader->loadClassMetadata($this->metadata));
    }

    public function testLoadClassMetadataReturnsFalseWhenEmpty()
    {
        $loader = new YamlFileLoader(__DIR__.'/../../Fixtures/empty-mapping.yml');
        $this->assertFalse($loader->loadClassMetadata($this->metadata));
    }

    public function testLoadClassMetadataReturnsThrowsInvalidMapping()
    {
        $this->expectException(MappingException::class);
        
$this->expectException(ConstraintDefinitionException::class);
        $this->expectExceptionMessage(sprintf('The option "groups" is not supported by the constraint "%s".', DisableAutoMapping::class));

        new DisableAutoMapping(['groups' => 'foo']);
    }

    public function testDisableAutoMappingAttribute()
    {
        $metadata = new ClassMetadata(DisableAutoMappingDummy::class);
        $loader = new AttributeLoader();
        self::assertSame(AutoMappingStrategy::NONE, $metadata->getAutoMappingStrategy());
        self::assertTrue($loader->loadClassMetadata($metadata));
        self::assertSame(AutoMappingStrategy::DISABLED, $metadata->getAutoMappingStrategy());
    }
}

#[DisableAutoMapping] class DisableAutoMappingDummy
{
}
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\PositiveOrZero;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class PositiveOrZeroTest extends TestCase
{
    public function testAttributes()
    {
        $metadata = new ClassMetadata(PositiveOrZeroDummy::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', 'PositiveOrZeroDummy']$aConstraint->groups);

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

public function testInvalidNormalizerObjectThrowsException()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).');
        new Regex(['pattern' => '/^[0-9]+$/', 'normalizer' => new \stdClass()]);
    }

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

        [$aConstraint] = $metadata->properties['a']->getConstraints();
        self::assertSame('/^[0-9]+$/', $aConstraint->pattern);
        self::assertTrue($aConstraint->match);
        self::assertNull($aConstraint->normalizer);

        [$bConstraint] = $metadata->properties['b']->getConstraints();
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame('/^[0-9]+$/', $bConstraint->pattern);
        self::assertSame('[0-9]+', $bConstraint->htmlPattern);
        self::assertFalse($bConstraint->match);
        
public function testInvalidNormalizerObjectThrowsException()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).');
        new Email(['normalizer' => new \stdClass()]);
    }

    public function testAttribute()
    {
        $metadata = new ClassMetadata(EmailDummy::class);
        (new AttributeLoader())->loadClassMetadata($metadata);

        [$aConstraint] = $metadata->properties['a']->constraints;
        self::assertNull($aConstraint->mode);
        self::assertNull($aConstraint->normalizer);

        [$bConstraint] = $metadata->properties['b']->constraints;
        self::assertSame('myMessage', $bConstraint->message);
        self::assertSame(Email::VALIDATION_MODE_HTML5, $bConstraint->mode);
        self::assertSame('trim', $bConstraint->normalizer);

        [$cConstraint] = $metadata->properties['c']->getConstraints();
        
Home | Imprint | This part of the site doesn't use cookies.