FakeMetadataFactory example

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\TraversalStrategy;
use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory;
use Symfony\Component\Validator\Validation;

class ValidatorExtensionTest extends TestCase
{
    public function test2Dot5ValidationApi()
    {
        $metadata = new ClassMetadata(Form::class);

        $metadataFactory = new FakeMetadataFactory();
        $metadataFactory->addMetadata($metadata);

        $validator = Validation::createValidatorBuilder()
            ->setMetadataFactory($metadataFactory)
            ->getValidator();

        $extension = new ValidatorExtension($validator, false);

        $this->assertInstanceOf(ValidatorTypeGuesser::class$extension->loadTypeGuesser());

        $this->assertCount(1, $metadata->getConstraints());
        

    private const ENTITY_CLASS = Entity::class;
    private const REFERENCE_CLASS = Reference::class;

    private FakeMetadataFactory $metadataFactory;
    private ClassMetadata $metadata;
    private ClassMetadata $referenceMetadata;
    private ValidatorInterface $validator;

    protected function setUp(): void
    {
        $this->metadataFactory = new FakeMetadataFactory();
        $this->metadata = new ClassMetadata(self::ENTITY_CLASS);
        $this->referenceMetadata = new ClassMetadata(self::REFERENCE_CLASS);
        $this->metadataFactory->addMetadata($this->metadata);
        $this->metadataFactory->addMetadata($this->referenceMetadata);
        $this->metadataFactory->addMetadata(new ClassMetadata(LazyProperty::class));

        $this->validator = $this->createValidator($this->metadataFactory);
    }

    protected function validate($value$constraints = null, $groups = null)
    {
        
private ValidatorTypeGuesser $guesser;
    private ClassMetadata $metadata;

    /** * @var MetadataFactoryInterface */
    private \Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory $metadataFactory;

    protected function setUp(): void
    {
        $this->metadata = new ClassMetadata(self::TEST_CLASS);
        $this->metadataFactory = new FakeMetadataFactory();
        $this->metadataFactory->addMetadata($this->metadata);
        $this->guesser = new ValidatorTypeGuesser($this->metadataFactory);
    }

    /** * @dataProvider guessTypeProvider */
    public function testGuessType(Constraint $constraint, TypeGuess $guess)
    {
        $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, $constraint);

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