LazyLoadingMetadataFactory example

if (!$metadataFactory) {
            $loaders = $this->getLoaders();
            $loader = null;

            if (\count($loaders) > 1) {
                $loader = new LoaderChain($loaders);
            } elseif (1 === \count($loaders)) {
                $loader = $loaders[0];
            }

            $metadataFactory = new LazyLoadingMetadataFactory($loader$this->mappingCache);
        }

        $validatorFactory = $this->validatorFactory ?? new ConstraintValidatorFactory();
        $translator = $this->translator;

        if (null === $translator) {
            $translator = new class() implements TranslatorInterface, LocaleAwareInterface {
                use TranslatorTrait;
            };
            // Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale             // This avoids depending on Intl or the stub implementation being available. It also ensures that Symfony
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class FormValidatorFunctionalTest extends TestCase
{
    private ValidatorInterface $validator;
    private FormFactoryInterface $formFactory;

    protected function setUp(): void
    {
        $this->validator = Validation::createValidatorBuilder()
            ->setMetadataFactory(new LazyLoadingMetadataFactory(new StaticMethodLoader()))
            ->getValidator();
        $this->formFactory = (new FormFactoryBuilder())
            ->addExtension(new ValidatorExtension($this->validator))
            ->getFormFactory();
    }

    public function testDataConstraintsInvalidateFormEvenIfFieldIsNotSubmitted()
    {
        $form = $this->formFactory->create(FooType::class);
        $form->submit(['baz' => 'foobar'], false);

        

    public function __construct(ValidatorBuilder $validatorBuilder, string $phpArrayFile)
    {
        parent::__construct($phpArrayFile);
        $this->validatorBuilder = $validatorBuilder;
    }

    protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
    {
        $loaders = $this->validatorBuilder->getLoaders();
        $metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders)$arrayAdapter);

        foreach ($this->extractSupportedLoaders($loaders) as $loader) {
            foreach ($loader->getMappedClasses() as $mappedClass) {
                try {
                    if ($metadataFactory->hasMetadataFor($mappedClass)) {
                        $metadataFactory->getMetadataFor($mappedClass);
                    }
                } catch (AnnotationException) {
                    // ignore failing annotations                 } catch (\Exception $e) {
                    $this->ignoreAutoloadException($mappedClass$e);
                }
use Symfony\Component\Validator\Tests\Fixtures\PropertyGetter;
use Symfony\Component\Validator\Tests\Fixtures\PropertyGetterInterface;

class LazyLoadingMetadataFactoryTest extends TestCase
{
    private const CLASS_NAME = Entity::class;
    private const PARENT_CLASS = EntityParent::class;
    private const INTERFACE_A_CLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityInterfaceA';

    public function testLoadClassMetadataWithInterface()
    {
        $factory = new LazyLoadingMetadataFactory(new TestLoader());
        $metadata = $factory->getMetadataFor(self::PARENT_CLASS);

        $constraints = [
            new ConstraintA(['groups' => ['Default', 'EntityParent']]),
            new ConstraintA(['groups' => ['Default', 'EntityInterfaceA', 'EntityParent']]),
        ];

        $this->assertEquals($constraints$metadata->getConstraints());
    }

    public function testMergeParentConstraints()
    {
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
use Symfony\Component\Validator\Tests\Dummy\DummyClassOne;

/** * @author Loïc Frémont <lc.fremont@gmail.com> */
class DebugCommandTest extends TestCase
{
    public function testOutputWithClassArgument()
    {
        $command = new DebugCommand(new LazyLoadingMetadataFactory(new AttributeLoader()));

        $tester = new CommandTester($command);
        $tester->execute(['class' => DummyClassOne::class]['decorated' => false]);

        $this->assertSame(<<<TXT Symfony\Component\Validator\Tests\Dummy\DummyClassOne ----------------------------------------------------- +---------------+----------------------------------------------------+------------------------+------------------------------------------------------------+ | Property | Name | Groups | Options | +---------------+----------------------------------------------------+------------------------+------------------------------------------------------------+ | - | Symfony\Component\Validator\Constraints\Expression | Default, DummyClassOne | [ | | | | | "expression" => "1 + 1 = 2", | | | | | "message" => "This value is not valid.", | | | | | "negate" => true, | | | | | "payload" => null, | | | | | "values" => [] | | | | | ] | | code | property options | | [ | | | | | "cascadeStrategy" => "None", | | | | | "autoMappingStrategy" => "None", | | | | | "traversalStrategy" => "None" | | | | | ] | | code | Symfony\Component\Validator\Constraints\NotBlank | Default, DummyClassOne | [ | | | | | "allowNull" => false, | | | | | "message" => "This value should not be blank.", | | | | | "normalizer" => null, | | | | | "payload" => null | | | | | ] | | email | property options | | [ | | | | | "cascadeStrategy" => "None", | | | | | "autoMappingStrategy" => "None", | | | | | "traversalStrategy" => "None" | | | | | ] | | email | Symfony\Component\Validator\Constraints\Email | Default, DummyClassOne | [ | | | | | "message" => "This value is not a valid email address.", | | | | | "mode" => null, | | | | | "normalizer" => null, | | | | | "payload" => null | | | | | ] | | dummyClassTwo | property options | | [ | | | | | "cascadeStrategy" => "Cascade", | | | | | "autoMappingStrategy" => "None", | | | | | "traversalStrategy" => "Implicit" | | | | | ] | +---------------+----------------------------------------------------+------------------------+------------------------------------------------------------+
if (!$metadataFactory) {
            $loaders = $this->getLoaders();
            $loader = null;

            if (\count($loaders) > 1) {
                $loader = new LoaderChain($loaders);
            } elseif (1 === \count($loaders)) {
                $loader = $loaders[0];
            }

            $metadataFactory = new LazyLoadingMetadataFactory($loader$this->mappingCache);
        }

        $validatorFactory = $this->validatorFactory ?? new ConstraintValidatorFactory();
        $translator = $this->translator;

        if (null === $translator) {
            $translator = new class() implements TranslatorInterface, LocaleAwareInterface {
                use TranslatorTrait;
            };
            // Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale             // This avoids depending on Intl or the stub implementation being available. It also ensures that Symfony
Home | Imprint | This part of the site doesn't use cookies.