getTypeGuesser example

$builder = $type->createBuilder($this$name$options);

        // Explicitly call buildForm() in order to be able to override either         // createBuilder() or buildForm() in the resolved form type         $type->buildForm($builder$builder->getOptions());

        return $builder;
    }

    public function createBuilderForProperty(string $class, string $property, mixed $data = null, array $options = []): FormBuilderInterface
    {
        if (null === $guesser = $this->registry->getTypeGuesser()) {
            return $this->createNamedBuilder($property, TextType::class$data$options);
        }

        $typeGuess = $guesser->guessType($class$property);
        $maxLengthGuess = $guesser->guessMaxLength($class$property);
        $requiredGuess = $guesser->guessRequired($class$property);
        $patternGuess = $guesser->guessPattern($class$property);

        $type = $typeGuess ? $typeGuess->getType() : TextType::class;

        $maxLength = $maxLengthGuess?->getValue();
        


        $extension = new DependencyInjectionExtension(new ContainerBuilder()$extensions[]);

        $extension->getTypeExtensions('unmatched');
    }

    public function testGetTypeGuesser()
    {
        $extension = new DependencyInjectionExtension(new ContainerBuilder()[][new FormTypeGuesserChain([])]);

        $this->assertInstanceOf(FormTypeGuesserChain::class$extension->getTypeGuesser());
    }

    public function testGetTypeGuesserReturnsNullWhenNoTypeGuessersHaveBeenConfigured()
    {
        $extension = new DependencyInjectionExtension(new ContainerBuilder()[][]);

        $this->assertNull($extension->getTypeGuesser());
    }
}

class TestTypeExtension extends AbstractTypeExtension
{
public function testAddType()
    {
        $factoryBuilder = new FormFactoryBuilder();
        $factoryBuilder->addType($this->type);

        $factory = $factoryBuilder->getFormFactory();
        $registry = $this->registry->getValue($factory);
        $extensions = $registry->getExtensions();

        $this->assertCount(1, $extensions);
        $this->assertTrue($extensions[0]->hasType($this->type::class));
        $this->assertNull($extensions[0]->getTypeGuesser());
    }

    public function testAddTypeGuesser()
    {
        $factoryBuilder = new FormFactoryBuilder();
        $factoryBuilder->addTypeGuesser(new NullFormTypeGuesser());

        $factory = $factoryBuilder->getFormFactory();
        $registry = $this->registry->getValue($factory);
        $extensions = $registry->getExtensions();

        


        return true;
    }

    public function getTypeGuesser(): ?FormTypeGuesserInterface
    {
        if (false === $this->guesser) {
            $guessers = [];

            foreach ($this->extensions as $extension) {
                $guesser = $extension->getTypeGuesser();

                if ($guesser) {
                    $guessers[] = $guesser;
                }
            }

            $this->guesser = $guessers ? new FormTypeGuesserChain($guessers) : null;
        }

        return $this->guesser;
    }

    


    public function testDoesNotHaveTypeIfNoFormType()
    {
        $this->assertFalse($this->registry->hasType('stdClass'));
    }

    public function testGetTypeGuesser()
    {
        $expectedGuesser = new FormTypeGuesserChain([new NullFormTypeGuesser()new NullFormTypeGuesser()]);

        $this->assertEquals($expectedGuesser$this->registry->getTypeGuesser());

        $registry = new FormRegistry([new PreloadedExtension([][])]new ResolvedFormTypeFactory());

        $this->assertNull($registry->getTypeGuesser());
    }

    public function testGetExtensions()
    {
        $expectedExtensions = [$this->extension1, $this->extension2];

        $this->assertEquals($expectedExtensions$this->registry->getExtensions());
    }
Home | Imprint | This part of the site doesn't use cookies.