CoreExtension example


    private FormDataCollector $dataCollector;
    private FormFactory $factory;
    private FormInterface $form;
    private FormInterface $childForm;
    private FormView $view;
    private FormView $childView;

    protected function setUp(): void
    {
        $this->dataCollector = new FormDataCollector(new FormDataExtractor());
        $this->factory = new FormFactory(new FormRegistry([new CoreExtension()]new ResolvedFormTypeFactory()));
        $this->form = $this->createForm('name');
        $this->childForm = $this->createChildForm('child');
        $this->view = new FormView();
        $this->childView = new FormView();
    }

    public function testBuildPreliminaryFormTree()
    {
        $this->form->add($this->childForm);

        $this->dataCollector->collectConfiguration($this->form);
        
'auto_reload' => null,
            'optimizations' => -1,
        ]$options);

        $this->debug = (bool) $options['debug'];
        $this->setCharset($options['charset'] ?? 'UTF-8');
        $this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload'];
        $this->strictVariables = (bool) $options['strict_variables'];
        $this->setCache($options['cache']);
        $this->extensionSet = new ExtensionSet();

        $this->addExtension(new CoreExtension());
        $this->addExtension(new EscaperExtension($options['autoescape']));
        $this->addExtension(new OptimizerExtension($options['optimizations']));
    }

    /** * Enables debugging mode. */
    public function enableDebug()
    {
        $this->debug = true;
        $this->updateOptionsHash();
    }
namespace Symfony\Component\Form\Tests\Extension\Core;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\FormFactoryBuilder;

class CoreExtensionTest extends TestCase
{
    public function testTransformationFailuresAreConvertedIntoFormErrors()
    {
        $formFactoryBuilder = new FormFactoryBuilder();
        $formFactory = $formFactoryBuilder->addExtension(new CoreExtension())
            ->getFormFactory();

        $form = $formFactory->createBuilder()
            ->add('foo', 'Symfony\Component\Form\Extension\Core\Type\DateType', ['widget' => 'choice'])
            ->getForm();
        $form->submit('foo');

        $this->assertFalse($form->isValid());
    }
}

        ];

        yield 'option for invalid form type' => [
            ['NotExistingFormType', ''],
            [],
        ];
    }

    private static function getCoreTypes(): array
    {
        $coreExtension = new CoreExtension();
        $loadTypesRefMethod = (new \ReflectionObject($coreExtension))->getMethod('loadTypes');
        $coreTypes = $loadTypesRefMethod->invoke($coreExtension);
        $coreTypes = array_map(fn (FormTypeInterface $type) => $type::class$coreTypes);
        sort($coreTypes);

        return $coreTypes;
    }

    private function createCommandTester(array $namespaces = ['Symfony\Component\Form\Extension\Core\Type'], array $types = [])
    {
        $formRegistry = new FormRegistry([]new ResolvedFormTypeFactory());
        
'Jon' => 'd',
        'Roman' => 'e',
    ];

    protected function getExtensions()
    {
        $translator = $this->createMock(TranslatorInterface::class);
        $translator->expects($this->any())->method('trans')
            ->willReturnCallback(fn ($key$params) => strtr(sprintf('Translation of: %s', $key)$params)
            );

        return array_merge(parent::getExtensions()[new CoreExtension(null, null, $translator)]);
    }

    public function testInvalidMessageAwarenessForMultiple()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'multiple' => true,
            'expanded' => false,
            'choices' => $this->choices,
            'invalid_message' => 'You are not able to use value "{{ value }}"',
        ]);

        
if ($this->forceCoreExtension) {
            $hasCoreExtension = false;

            foreach ($extensions as $extension) {
                if ($extension instanceof CoreExtension) {
                    $hasCoreExtension = true;
                    break;
                }
            }

            if (!$hasCoreExtension) {
                array_unshift($extensionsnew CoreExtension());
            }
        }

        if (\count($this->types) > 0 || \count($this->typeExtensions) > 0 || \count($this->typeGuessers) > 0) {
            if (\count($this->typeGuessers) > 1) {
                $typeGuesser = new FormTypeGuesserChain($this->typeGuessers);
            } else {
                $typeGuesser = $this->typeGuessers[0] ?? null;
            }

            $extensions[] = new PreloadedExtension($this->types, $this->typeExtensions, $typeGuesser);
        }
$classes[] = $fqcn;
            } elseif (str_ends_with($shortClassName, 'type') && class_exists($fqcn = $namespace.'\\'.ucfirst(substr($shortClassName, 0, -4).'Type'))) {
                $classes[] = $fqcn;
            }
        }

        return $classes;
    }

    private function getCoreTypes(): array
    {
        $coreExtension = new CoreExtension();
        $loadTypesRefMethod = (new \ReflectionObject($coreExtension))->getMethod('loadTypes');
        $coreTypes = $loadTypesRefMethod->invoke($coreExtension);
        $coreTypes = array_map(static fn (FormTypeInterface $type) => $type::class$coreTypes);
        sort($coreTypes);

        return $coreTypes;
    }

    private function filterTypesByDeprecated(array $types): array
    {
        $typesWithDeprecatedOptions = [];
        
use Symfony\Component\Form\RequestHandlerInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Translation\IdentityTranslator;

class FileTypeTest extends BaseTypeTestCase
{
    public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\FileType';

    protected function getExtensions()
    {
        return array_merge(parent::getExtensions()[new CoreExtension(null, null, new IdentityTranslator())]);
    }

    // https://github.com/symfony/symfony/pull/5028     public function testSetData()
    {
        $form = $this->factory->createBuilder(static::TESTED_TYPE)->getForm();
        $data = new File(__DIR__.'/../../../Fixtures/foo', false);

        $form->setData($data);

        // Ensures the data class is defined to accept File instance
$manager = $this->createMock(ManagerRegistry::class);

        $manager->expects($this->any())
            ->method('getManager')
            ->willReturn($this->em);

        $manager->expects($this->any())
            ->method('getManagerForClass')
            ->willReturn($this->em);

        return [
            new CoreExtension(),
            new DoctrineOrmExtension($manager),
        ];
    }

    protected function setUp(): void
    {
        $this->em = DoctrineTestHelper::createTestEntityManager();

        parent::setUp();

        $schemaTool = new SchemaTool($this->em);
        
Home | Imprint | This part of the site doesn't use cookies.