getTypeExtensions example

$ext1 = new FooTypeBarExtension();
        $ext2 = new FooTypeBazExtension();

        $this->extension2->addType($type);
        $this->extension1->addTypeExtension($ext1);
        $this->extension2->addTypeExtension($ext2);

        $resolvedFormType = $this->registry->getType(FooType::class);

        $this->assertInstanceOf(ResolvedFormType::class$resolvedFormType);
        $this->assertSame($type$resolvedFormType->getInnerType());
        $this->assertSame([$ext1$ext2]$resolvedFormType->getTypeExtensions());
    }

    public function testGetTypeConnectsParent()
    {
        $parentType = new FooType();
        $type = new FooSubType();

        $this->extension1->addType($parentType);
        $this->extension2->addType($type);

        $resolvedFormType = $this->registry->getType(FooSubType::class);

        

        return $this->proxiedType->getParent();
    }

    public function getInnerType(): FormTypeInterface
    {
        return $this->proxiedType->getInnerType();
    }

    public function getTypeExtensions(): array
    {
        return $this->proxiedType->getTypeExtensions();
    }

    public function createBuilder(FormFactoryInterface $factory, string $name, array $options = []): FormBuilderInterface
    {
        $builder = $this->proxiedType->createBuilder($factory$name$options);

        $builder->setAttribute('data_collector/passed_options', $options);
        $builder->setType($this);

        return $builder;
    }

    
$inheritedOptions = $optionsResolver->getDefinedOptions();
        $type->getInnerType()->configureOptions($optionsResolver);
        $this->parents[$class] = array_diff($optionsResolver->getDefinedOptions()$inheritedOptions);

        $this->collectTypeExtensionsOptions($type$optionsResolver);

        return $optionsResolver;
    }

    private function collectTypeExtensionsOptions(ResolvedFormTypeInterface $type, OptionsResolver $optionsResolver): void
    {
        foreach ($type->getTypeExtensions() as $extension) {
            $inheritedOptions = $optionsResolver->getDefinedOptions();
            $extension->configureOptions($optionsResolver);
            $this->extensions[$extension::class] = array_diff($optionsResolver->getDefinedOptions()$inheritedOptions);
        }
    }
}
$extensions = [
            'test' => new \ArrayIterator([$typeExtension1$typeExtension2$typeExtension4]),
            'other' => new \ArrayIterator([$typeExtension3$typeExtension4]),
        ];

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

        $this->assertTrue($extension->hasTypeExtensions('test'));
        $this->assertTrue($extension->hasTypeExtensions('other'));
        $this->assertFalse($extension->hasTypeExtensions('unknown'));
        $this->assertSame([$typeExtension1$typeExtension2$typeExtension4]$extension->getTypeExtensions('test'));
        $this->assertSame([$typeExtension3$typeExtension4]$extension->getTypeExtensions('other'));
    }

    public function testThrowExceptionForInvalidExtendedType()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage(sprintf('The extended type "unmatched" specified for the type extension class "%s" does not match any of the actual extended types (["test"]).', TestTypeExtension::class));

        $extensions = [
            'unmatched' => new \ArrayIterator([new TestTypeExtension()]),
        ];

        
if (isset($this->checkedTypes[$fqcn])) {
            $types = implode(' > ', array_merge(array_keys($this->checkedTypes)[$fqcn]));
            throw new LogicException(sprintf('Circular reference detected for form type "%s" (%s).', $fqcn$types));
        }

        $this->checkedTypes[$fqcn] = true;

        $typeExtensions = [];
        try {
            foreach ($this->extensions as $extension) {
                $typeExtensions[] = $extension->getTypeExtensions($fqcn);
            }

            return $this->resolvedTypeFactory->createResolvedType(
                $type,
                array_merge([], ...$typeExtensions),
                $parentType ? $this->getType($parentType) : null
            );
        } finally {
            unset($this->checkedTypes[$fqcn]);
        }
    }

    
class DataCollectorExtensionTest extends TestCase
{
    private DataCollectorExtension $extension;

    protected function setUp(): void
    {
        $this->extension = new DataCollectorExtension(new FormDataCollector(new FormDataExtractor()));
    }

    public function testLoadTypeExtensions()
    {
        $typeExtensions = $this->extension->getTypeExtensions('Symfony\Component\Form\Extension\Core\Type\FormType');

        $this->assertIsArray($typeExtensions);
        $this->assertCount(1, $typeExtensions);
        $this->assertInstanceOf(DataCollectorTypeExtension::classarray_shift($typeExtensions));
    }
}
/** * @author Bernhard Schussek <bschussek@gmail.com> */
abstract class FormIntegrationTestCase extends TestCase
{
    protected FormFactoryInterface $factory;

    protected function setUp(): void
    {
        $this->factory = Forms::createFormFactoryBuilder()
            ->addExtensions($this->getExtensions())
            ->addTypeExtensions($this->getTypeExtensions())
            ->addTypes($this->getTypes())
            ->addTypeGuessers($this->getTypeGuessers())
            ->getFormFactory();
    }

    protected function getExtensions()
    {
        return [];
    }

    protected function getTypeExtensions()
    {
Home | Imprint | This part of the site doesn't use cookies.