diff --git a/src/Symfony/Component/Form/ButtonBuilder.php b/src/Symfony/Component/Form/ButtonBuilder.php index 87adc69475dc5..bf020baf53787 100644 --- a/src/Symfony/Component/Form/ButtonBuilder.php +++ b/src/Symfony/Component/Form/ButtonBuilder.php @@ -471,7 +471,7 @@ public function getFormConfig() * * @throws BadMethodCallException */ - public function setIsEmptyCallback(?callable $isEmptyCallback) + public function setIsEmptyCallback(?callable $isEmptyCallback): static { throw new BadMethodCallException('Buttons do not support "is empty" callback.'); } diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 9a8dae4739d53..5b2c551b2b0cf 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -1,6 +1,20 @@ CHANGELOG ========= +6.0 +--- + + * Remove `PropertyPathMaper` + * Remove `Symfony\Component\Form\Extension\Validator\Util\ServiceParams` + * Remove `FormPass` configuration + * Add `FormConfigInterface::getIsEmptyCallback()` and `FormConfigBuilderInterface::setIsEmptyCallback()` + * Change `$forms` parameter type of the `DataMapper::mapDataToForms()` method from `iterable` to `\Traversable` + * Change `$forms` parameter type of the `DataMapper::mapFormsToData()` method from `iterable` to `\Traversable` + * Change `$checkboxes` parameter type of the `CheckboxListMapper::mapDataToForms()` method from `iterable` to `\Traversable` + * Change `$checkboxes` parameter type of the `CheckboxListMapper::mapFormsToData()` method from `iterable` to `\Traversable` + * Change `$radios` parameter type of the `RadioListMapper::mapDataToForms()` method from `iterable` to `\Traversable` + * Change `$radios` parameter type of the `RadioListMapper::mapFormsToData()` method from `iterable` to `\Traversable` + 5.3 --- diff --git a/src/Symfony/Component/Form/DependencyInjection/FormPass.php b/src/Symfony/Component/Form/DependencyInjection/FormPass.php index 0ba46117c5d42..cba1aeeb2cde6 100644 --- a/src/Symfony/Component/Form/DependencyInjection/FormPass.php +++ b/src/Symfony/Component/Form/DependencyInjection/FormPass.php @@ -30,32 +30,13 @@ class FormPass implements CompilerPassInterface { use PriorityTaggedServiceTrait; - private $formExtensionService; - private $formTypeTag; - private $formTypeExtensionTag; - private $formTypeGuesserTag; - private $formDebugCommandService; - - public function __construct(string $formExtensionService = 'form.extension', string $formTypeTag = 'form.type', string $formTypeExtensionTag = 'form.type_extension', string $formTypeGuesserTag = 'form.type_guesser', string $formDebugCommandService = 'console.command.form_debug') - { - if (0 < \func_num_args()) { - trigger_deprecation('symfony/http-kernel', '5.3', 'Configuring "%s" is deprecated.', __CLASS__); - } - - $this->formExtensionService = $formExtensionService; - $this->formTypeTag = $formTypeTag; - $this->formTypeExtensionTag = $formTypeExtensionTag; - $this->formTypeGuesserTag = $formTypeGuesserTag; - $this->formDebugCommandService = $formDebugCommandService; - } - public function process(ContainerBuilder $container) { - if (!$container->hasDefinition($this->formExtensionService)) { + if (!$container->hasDefinition('form.extension')) { return; } - $definition = $container->getDefinition($this->formExtensionService); + $definition = $container->getDefinition('form.extension'); $definition->replaceArgument(0, $this->processFormTypes($container)); $definition->replaceArgument(1, $this->processFormTypeExtensions($container)); $definition->replaceArgument(2, $this->processFormTypeGuessers($container)); @@ -68,15 +49,15 @@ private function processFormTypes(ContainerBuilder $container): Reference $namespaces = ['Symfony\Component\Form\Extension\Core\Type' => true]; // Builds an array with fully-qualified type class names as keys and service IDs as values - foreach ($container->findTaggedServiceIds($this->formTypeTag, true) as $serviceId => $tag) { + foreach ($container->findTaggedServiceIds('form.type', true) as $serviceId => $tag) { // Add form type service to the service locator $serviceDefinition = $container->getDefinition($serviceId); $servicesMap[$formType = $serviceDefinition->getClass()] = new Reference($serviceId); $namespaces[substr($formType, 0, strrpos($formType, '\\'))] = true; } - if ($container->hasDefinition($this->formDebugCommandService)) { - $commandDefinition = $container->getDefinition($this->formDebugCommandService); + if ($container->hasDefinition('console.command.form_debug')) { + $commandDefinition = $container->getDefinition('console.command.form_debug'); $commandDefinition->setArgument(1, array_keys($namespaces)); $commandDefinition->setArgument(2, array_keys($servicesMap)); } @@ -88,11 +69,11 @@ private function processFormTypeExtensions(ContainerBuilder $container): array { $typeExtensions = []; $typeExtensionsClasses = []; - foreach ($this->findAndSortTaggedServices($this->formTypeExtensionTag, $container) as $reference) { + foreach ($this->findAndSortTaggedServices('form.type_extension', $container) as $reference) { $serviceId = (string) $reference; $serviceDefinition = $container->getDefinition($serviceId); - $tag = $serviceDefinition->getTag($this->formTypeExtensionTag); + $tag = $serviceDefinition->getTag('form.type_extension'); $typeExtensionClass = $container->getParameterBag()->resolveValue($serviceDefinition->getClass()); if (isset($tag[0]['extended_type'])) { @@ -117,8 +98,8 @@ private function processFormTypeExtensions(ContainerBuilder $container): array $typeExtensions[$extendedType] = new IteratorArgument($extensions); } - if ($container->hasDefinition($this->formDebugCommandService)) { - $commandDefinition = $container->getDefinition($this->formDebugCommandService); + if ($container->hasDefinition('console.command.form_debug')) { + $commandDefinition = $container->getDefinition('console.command.form_debug'); $commandDefinition->setArgument(3, $typeExtensionsClasses); } @@ -129,15 +110,15 @@ private function processFormTypeGuessers(ContainerBuilder $container): ArgumentI { $guessers = []; $guessersClasses = []; - foreach ($container->findTaggedServiceIds($this->formTypeGuesserTag, true) as $serviceId => $tags) { + foreach ($container->findTaggedServiceIds('form.type_guesser', true) as $serviceId => $tags) { $guessers[] = new Reference($serviceId); $serviceDefinition = $container->getDefinition($serviceId); $guessersClasses[] = $serviceDefinition->getClass(); } - if ($container->hasDefinition($this->formDebugCommandService)) { - $commandDefinition = $container->getDefinition($this->formDebugCommandService); + if ($container->hasDefinition('console.command.form_debug')) { + $commandDefinition = $container->getDefinition('console.command.form_debug'); $commandDefinition->setArgument(4, $guessersClasses); } diff --git a/src/Symfony/Component/Form/Extension/Core/DataMapper/CheckboxListMapper.php b/src/Symfony/Component/Form/Extension/Core/DataMapper/CheckboxListMapper.php index ecfd83a066a8d..a6014d7076fc4 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataMapper/CheckboxListMapper.php +++ b/src/Symfony/Component/Form/Extension/Core/DataMapper/CheckboxListMapper.php @@ -28,12 +28,8 @@ class CheckboxListMapper implements DataMapperInterface /** * {@inheritdoc} */ - public function mapDataToForms($choices, iterable $checkboxes) + public function mapDataToForms($choices, \Traversable $checkboxes) { - if (\is_array($checkboxes)) { - trigger_deprecation('symfony/form', '5.3', 'Passing an array as the second argument of the "%s()" method is deprecated, pass "\Traversable" instead.', __METHOD__); - } - if (null === $choices) { $choices = []; } @@ -51,12 +47,8 @@ public function mapDataToForms($choices, iterable $checkboxes) /** * {@inheritdoc} */ - public function mapFormsToData(iterable $checkboxes, &$choices) + public function mapFormsToData(\Traversable $checkboxes, &$choices) { - if (\is_array($checkboxes)) { - trigger_deprecation('symfony/form', '5.3', 'Passing an array as the first argument of the "%s()" method is deprecated, pass "\Traversable" instead.', __METHOD__); - } - if (!\is_array($choices)) { throw new UnexpectedTypeException($choices, 'array'); } diff --git a/src/Symfony/Component/Form/Extension/Core/DataMapper/DataMapper.php b/src/Symfony/Component/Form/Extension/Core/DataMapper/DataMapper.php index 5f4c498a33526..1e98740d2eac6 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataMapper/DataMapper.php +++ b/src/Symfony/Component/Form/Extension/Core/DataMapper/DataMapper.php @@ -38,12 +38,8 @@ public function __construct(DataAccessorInterface $dataAccessor = null) /** * {@inheritdoc} */ - public function mapDataToForms($data, iterable $forms): void + public function mapDataToForms($data, \Traversable $forms): void { - if (\is_array($forms)) { - trigger_deprecation('symfony/form', '5.3', 'Passing an array as the second argument of the "%s()" method is deprecated, pass "\Traversable" instead.', __METHOD__); - } - $empty = null === $data || [] === $data; if (!$empty && !\is_array($data) && !\is_object($data)) { @@ -64,12 +60,8 @@ public function mapDataToForms($data, iterable $forms): void /** * {@inheritdoc} */ - public function mapFormsToData(iterable $forms, &$data): void + public function mapFormsToData(\Traversable $forms, &$data): void { - if (\is_array($forms)) { - trigger_deprecation('symfony/form', '5.3', 'Passing an array as the first argument of the "%s()" method is deprecated, pass "\Traversable" instead.', __METHOD__); - } - if (null === $data) { return; } diff --git a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php deleted file mode 100644 index 7dbc214ca677c..0000000000000 --- a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php +++ /dev/null @@ -1,113 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Form\Extension\Core\DataMapper; - -use Symfony\Component\Form\DataMapperInterface; -use Symfony\Component\Form\Exception\UnexpectedTypeException; -use Symfony\Component\PropertyAccess\Exception\AccessException; -use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException; -use Symfony\Component\PropertyAccess\PropertyAccess; -use Symfony\Component\PropertyAccess\PropertyAccessorInterface; - -trigger_deprecation('symfony/form', '5.2', 'The "%s" class is deprecated. Use "%s" instead.', PropertyPathMapper::class, DataMapper::class); - -/** - * Maps arrays/objects to/from forms using property paths. - * - * @author Bernhard Schussek - * - * @deprecated since symfony/form 5.2. Use {@see DataMapper} instead. - */ -class PropertyPathMapper implements DataMapperInterface -{ - private $propertyAccessor; - - public function __construct(PropertyAccessorInterface $propertyAccessor = null) - { - $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); - } - - /** - * {@inheritdoc} - */ - public function mapDataToForms($data, iterable $forms) - { - $empty = null === $data || [] === $data; - - if (!$empty && !\is_array($data) && !\is_object($data)) { - throw new UnexpectedTypeException($data, 'object, array or empty'); - } - - foreach ($forms as $form) { - $propertyPath = $form->getPropertyPath(); - $config = $form->getConfig(); - - if (!$empty && null !== $propertyPath && $config->getMapped()) { - $form->setData($this->getPropertyValue($data, $propertyPath)); - } else { - $form->setData($config->getData()); - } - } - } - - /** - * {@inheritdoc} - */ - public function mapFormsToData(iterable $forms, &$data) - { - if (null === $data) { - return; - } - - if (!\is_array($data) && !\is_object($data)) { - throw new UnexpectedTypeException($data, 'object, array or empty'); - } - - foreach ($forms as $form) { - $propertyPath = $form->getPropertyPath(); - $config = $form->getConfig(); - - // Write-back is disabled if the form is not synchronized (transformation failed), - // if the form was not submitted and if the form is disabled (modification not allowed) - if (null !== $propertyPath && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) { - $propertyValue = $form->getData(); - // If the field is of type DateTimeInterface and the data is the same skip the update to - // keep the original object hash - if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data, $propertyPath)) { - continue; - } - - // If the data is identical to the value in $data, we are - // dealing with a reference - if (!\is_object($data) || !$config->getByReference() || $propertyValue !== $this->getPropertyValue($data, $propertyPath)) { - $this->propertyAccessor->setValue($data, $propertyPath, $propertyValue); - } - } - } - } - - private function getPropertyValue($data, $propertyPath) - { - try { - return $this->propertyAccessor->getValue($data, $propertyPath); - } catch (AccessException $e) { - if (!$e instanceof UninitializedPropertyException - // For versions without UninitializedPropertyException check the exception message - && (class_exists(UninitializedPropertyException::class) || false === strpos($e->getMessage(), 'You should initialize it')) - ) { - throw $e; - } - - return null; - } - } -} diff --git a/src/Symfony/Component/Form/Extension/Core/DataMapper/RadioListMapper.php b/src/Symfony/Component/Form/Extension/Core/DataMapper/RadioListMapper.php index b54adfa5d1ba1..16f02a5cab77d 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataMapper/RadioListMapper.php +++ b/src/Symfony/Component/Form/Extension/Core/DataMapper/RadioListMapper.php @@ -28,7 +28,7 @@ class RadioListMapper implements DataMapperInterface /** * {@inheritdoc} */ - public function mapDataToForms($choice, iterable $radios) + public function mapDataToForms($choice, \Traversable $radios) { if (\is_array($radios)) { trigger_deprecation('symfony/form', '5.3', 'Passing an array as the second argument of the "%s()" method is deprecated, pass "\Traversable" instead.', __METHOD__); @@ -47,12 +47,8 @@ public function mapDataToForms($choice, iterable $radios) /** * {@inheritdoc} */ - public function mapFormsToData(iterable $radios, &$choice) + public function mapFormsToData(\Traversable $radios, &$choice) { - if (\is_array($radios)) { - trigger_deprecation('symfony/form', '5.3', 'Passing an array as the first argument of the "%s()" method is deprecated, pass "\Traversable" instead.', __METHOD__); - } - if (null !== $choice && !\is_string($choice)) { throw new UnexpectedTypeException($choice, 'null or string'); } diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php index 7fa7b60fc1791..c7973980b1c83 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php @@ -18,7 +18,6 @@ use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper; use Symfony\Component\Form\Extension\Core\EventListener\TrimListener; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\FormConfigBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\Options; @@ -66,12 +65,6 @@ public function buildForm(FormBuilderInterface $builder, array $options) $builder->addEventSubscriber(new TrimListener()); } - if (!method_exists($builder, 'setIsEmptyCallback')) { - trigger_deprecation('symfony/form', '5.1', 'Not implementing the "%s::setIsEmptyCallback()" method in "%s" is deprecated.', FormConfigBuilderInterface::class, get_debug_type($builder)); - - return; - } - $builder->setIsEmptyCallback($options['is_empty_callback']); } diff --git a/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php b/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php deleted file mode 100644 index 98e9f0c98a87f..0000000000000 --- a/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Form\Extension\Validator\Util; - -use Symfony\Component\Form\Util\ServerParams as BaseServerParams; - -trigger_deprecation('symfony/form', '5.1', 'The "%s" class is deprecated. Use "%s" instead.', ServerParams::class, BaseServerParams::class); - -/** - * @author Bernhard Schussek - * - * @deprecated since Symfony 5.1. Use {@see BaseServerParams} instead. - */ -class ServerParams extends BaseServerParams -{ -} diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 1901524562c6a..82bd8a660b9ee 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -724,15 +724,7 @@ public function isEmpty() } } - if (!method_exists($this->config, 'getIsEmptyCallback')) { - trigger_deprecation('symfony/form', '5.1', 'Not implementing the "%s::getIsEmptyCallback()" method in "%s" is deprecated.', FormConfigInterface::class, \get_class($this->config)); - - $isEmptyCallback = null; - } else { - $isEmptyCallback = $this->config->getIsEmptyCallback(); - } - - if (null !== $isEmptyCallback) { + if (null !== $isEmptyCallback = $this->config->getIsEmptyCallback()) { return $isEmptyCallback($this->modelData); } diff --git a/src/Symfony/Component/Form/FormConfigBuilder.php b/src/Symfony/Component/Form/FormConfigBuilder.php index 7e7b40edacabb..69acee4d9900a 100644 --- a/src/Symfony/Component/Form/FormConfigBuilder.php +++ b/src/Symfony/Component/Form/FormConfigBuilder.php @@ -773,7 +773,7 @@ public function getFormConfig() /** * {@inheritdoc} */ - public function setIsEmptyCallback(?callable $isEmptyCallback) + public function setIsEmptyCallback(?callable $isEmptyCallback): static { $this->isEmptyCallback = $isEmptyCallback; diff --git a/src/Symfony/Component/Form/FormConfigBuilderInterface.php b/src/Symfony/Component/Form/FormConfigBuilderInterface.php index d9064c1434a00..fb4a835ba6b9d 100644 --- a/src/Symfony/Component/Form/FormConfigBuilderInterface.php +++ b/src/Symfony/Component/Form/FormConfigBuilderInterface.php @@ -16,8 +16,6 @@ /** * @author Bernhard Schussek - * - * @method $this setIsEmptyCallback(callable|null $isEmptyCallback) Sets the callback that will be called to determine if the model data of the form is empty or not - not implementing it is deprecated since Symfony 5.1 */ interface FormConfigBuilderInterface extends FormConfigInterface { @@ -251,4 +249,12 @@ public function setAutoInitialize(bool $initialize); * @return FormConfigInterface */ public function getFormConfig(); + + /** + * Sets the callback that will be called to determine if the model + * data of the form is empty or not. + * + * @return $this + */ + public function setIsEmptyCallback(?callable $isEmptyCallback): static; } diff --git a/src/Symfony/Component/Form/FormConfigInterface.php b/src/Symfony/Component/Form/FormConfigInterface.php index e76986c4fb6b7..c73eb5d74a2da 100644 --- a/src/Symfony/Component/Form/FormConfigInterface.php +++ b/src/Symfony/Component/Form/FormConfigInterface.php @@ -18,8 +18,6 @@ * The configuration of a {@link Form} object. * * @author Bernhard Schussek - * - * @method callable|null getIsEmptyCallback() Returns a callable that takes the model data as argument and that returns if it is empty or not - not implementing it is deprecated since Symfony 5.1 */ interface FormConfigInterface { @@ -247,4 +245,9 @@ public function hasOption(string $name); * @return mixed The option value */ public function getOption(string $name, $default = null); + + /** + * Returns a callable that takes the model data as argument and that returns if it is empty or not. + */ + public function getIsEmptyCallback(): ?callable; } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php deleted file mode 100644 index d29c86e4b7879..0000000000000 --- a/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php +++ /dev/null @@ -1,362 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Form\Tests\Extension\Core\DataMapper; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\EventDispatcher\EventDispatcher; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; -use Symfony\Component\Form\Form; -use Symfony\Component\Form\FormConfigBuilder; -use Symfony\Component\Form\Tests\Fixtures\TypehintedPropertiesCar; -use Symfony\Component\PropertyAccess\PropertyAccess; -use Symfony\Component\PropertyAccess\PropertyAccessorInterface; -use Symfony\Component\PropertyAccess\PropertyPath; - -/** - * @group legacy - */ -class PropertyPathMapperTest extends TestCase -{ - /** - * @var PropertyPathMapper - */ - private $mapper; - - /** - * @var EventDispatcherInterface - */ - private $dispatcher; - - /** - * @var PropertyAccessorInterface - */ - private $propertyAccessor; - - protected function setUp(): void - { - $this->dispatcher = new EventDispatcher(); - $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); - $this->mapper = new PropertyPathMapper($this->propertyAccessor); - } - - public function testMapDataToFormsPassesObjectRefIfByReference() - { - $car = new \stdClass(); - $engine = new \stdClass(); - $car->engine = $engine; - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(true); - $config->setPropertyPath($propertyPath); - $form = new Form($config); - - $this->mapper->mapDataToForms($car, [$form]); - - $this->assertSame($engine, $form->getData()); - } - - public function testMapDataToFormsPassesObjectCloneIfNotByReference() - { - $car = new \stdClass(); - $engine = new \stdClass(); - $engine->brand = 'Rolls-Royce'; - $car->engine = $engine; - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(false); - $config->setPropertyPath($propertyPath); - $form = new Form($config); - - $this->mapper->mapDataToForms($car, [$form]); - - $this->assertNotSame($engine, $form->getData()); - $this->assertEquals($engine, $form->getData()); - } - - public function testMapDataToFormsIgnoresEmptyPropertyPath() - { - $car = new \stdClass(); - - $config = new FormConfigBuilder(null, '\stdClass', $this->dispatcher); - $config->setByReference(true); - $form = new Form($config); - - $this->assertNull($form->getPropertyPath()); - - $this->mapper->mapDataToForms($car, [$form]); - - $this->assertNull($form->getData()); - } - - public function testMapDataToFormsIgnoresUnmapped() - { - $car = new \stdClass(); - $car->engine = new \stdClass(); - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(true); - $config->setMapped(false); - $config->setPropertyPath($propertyPath); - $form = new Form($config); - - $this->mapper->mapDataToForms($car, [$form]); - - $this->assertNull($form->getData()); - } - - public function testMapDataToFormsIgnoresUninitializedProperties() - { - $engineForm = new Form(new FormConfigBuilder('engine', null, $this->dispatcher)); - $colorForm = new Form(new FormConfigBuilder('color', null, $this->dispatcher)); - - $car = new TypehintedPropertiesCar(); - $car->engine = 'BMW'; - - $this->mapper->mapDataToForms($car, [$engineForm, $colorForm]); - - $this->assertSame($car->engine, $engineForm->getData()); - $this->assertNull($colorForm->getData()); - } - - public function testMapDataToFormsSetsDefaultDataIfPassedDataIsNull() - { - $default = new \stdClass(); - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(true); - $config->setPropertyPath($propertyPath); - $config->setData($default); - - $form = new Form($config); - - $this->mapper->mapDataToForms(null, [$form]); - - $this->assertSame($default, $form->getData()); - } - - public function testMapDataToFormsSetsDefaultDataIfPassedDataIsEmptyArray() - { - $default = new \stdClass(); - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(true); - $config->setPropertyPath($propertyPath); - $config->setData($default); - - $form = new Form($config); - - $this->mapper->mapDataToForms([], [$form]); - - $this->assertSame($default, $form->getData()); - } - - public function testMapFormsToDataWritesBackIfNotByReference() - { - $car = new \stdClass(); - $car->engine = new \stdClass(); - $engine = new \stdClass(); - $engine->brand = 'Rolls-Royce'; - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(false); - $config->setPropertyPath($propertyPath); - $config->setData($engine); - $form = new SubmittedForm($config); - - $this->mapper->mapFormsToData([$form], $car); - - $this->assertEquals($engine, $car->engine); - $this->assertNotSame($engine, $car->engine); - } - - public function testMapFormsToDataWritesBackIfByReferenceButNoReference() - { - $car = new \stdClass(); - $car->engine = new \stdClass(); - $engine = new \stdClass(); - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(true); - $config->setPropertyPath($propertyPath); - $config->setData($engine); - $form = new SubmittedForm($config); - - $this->mapper->mapFormsToData([$form], $car); - - $this->assertSame($engine, $car->engine); - } - - public function testMapFormsToDataWritesBackIfByReferenceAndReference() - { - $car = new \stdClass(); - $car->engine = 'BMW'; - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('engine', null, $this->dispatcher); - $config->setByReference(true); - $config->setPropertyPath($propertyPath); - $config->setData('Rolls-Royce'); - $form = new SubmittedForm($config); - - $car->engine = 'Rolls-Royce'; - - $this->mapper->mapFormsToData([$form], $car); - - $this->assertSame('Rolls-Royce', $car->engine); - } - - public function testMapFormsToDataIgnoresUnmapped() - { - $initialEngine = new \stdClass(); - $car = new \stdClass(); - $car->engine = $initialEngine; - $engine = new \stdClass(); - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(true); - $config->setPropertyPath($propertyPath); - $config->setData($engine); - $config->setMapped(false); - $form = new SubmittedForm($config); - - $this->mapper->mapFormsToData([$form], $car); - - $this->assertSame($initialEngine, $car->engine); - } - - public function testMapFormsToDataIgnoresUnsubmittedForms() - { - $initialEngine = new \stdClass(); - $car = new \stdClass(); - $car->engine = $initialEngine; - $engine = new \stdClass(); - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(true); - $config->setPropertyPath($propertyPath); - $config->setData($engine); - $form = new Form($config); - - $this->mapper->mapFormsToData([$form], $car); - - $this->assertSame($initialEngine, $car->engine); - } - - public function testMapFormsToDataIgnoresEmptyData() - { - $initialEngine = new \stdClass(); - $car = new \stdClass(); - $car->engine = $initialEngine; - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(true); - $config->setPropertyPath($propertyPath); - $config->setData(null); - $form = new Form($config); - - $this->mapper->mapFormsToData([$form], $car); - - $this->assertSame($initialEngine, $car->engine); - } - - public function testMapFormsToDataIgnoresUnsynchronized() - { - $initialEngine = new \stdClass(); - $car = new \stdClass(); - $car->engine = $initialEngine; - $engine = new \stdClass(); - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(true); - $config->setPropertyPath($propertyPath); - $config->setData($engine); - $form = new NotSynchronizedForm($config); - - $this->mapper->mapFormsToData([$form], $car); - - $this->assertSame($initialEngine, $car->engine); - } - - public function testMapFormsToDataIgnoresDisabled() - { - $initialEngine = new \stdClass(); - $car = new \stdClass(); - $car->engine = $initialEngine; - $engine = new \stdClass(); - $propertyPath = new PropertyPath('engine'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); - $config->setByReference(true); - $config->setPropertyPath($propertyPath); - $config->setData($engine); - $config->setDisabled(true); - $form = new SubmittedForm($config); - - $this->mapper->mapFormsToData([$form], $car); - - $this->assertSame($initialEngine, $car->engine); - } - - public function testMapFormsToUninitializedProperties() - { - $car = new TypehintedPropertiesCar(); - $config = new FormConfigBuilder('engine', null, $this->dispatcher); - $config->setData('BMW'); - $form = new SubmittedForm($config); - - $this->mapper->mapFormsToData([$form], $car); - - $this->assertSame('BMW', $car->engine); - } - - /** - * @dataProvider provideDate - */ - public function testMapFormsToDataDoesNotChangeEqualDateTimeInstance($date) - { - $article = []; - $publishedAt = $date; - $publishedAtValue = clone $publishedAt; - $article['publishedAt'] = $publishedAtValue; - $propertyPath = new PropertyPath('[publishedAt]'); - - $config = new FormConfigBuilder('publishedAt', \get_class($publishedAt), $this->dispatcher); - $config->setByReference(false); - $config->setPropertyPath($propertyPath); - $config->setData($publishedAt); - $form = new SubmittedForm($config); - - $this->mapper->mapFormsToData([$form], $article); - - $this->assertSame($publishedAtValue, $article['publishedAt']); - } - - public function provideDate() - { - return [ - [new \DateTime()], - [new \DateTimeImmutable()], - ]; - } -} diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Util/LegacyServerParamsTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Util/LegacyServerParamsTest.php deleted file mode 100644 index 8b0b0d7ecf1fb..0000000000000 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Util/LegacyServerParamsTest.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Form\Tests\Extension\Validator\Util; - -use PHPUnit\Framework\TestCase; -use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; -use Symfony\Component\Form\Extension\Validator\Util\ServerParams; - -class LegacyServerParamsTest extends TestCase -{ - use ExpectDeprecationTrait; - - /** - * @group legacy - */ - public function testClassIsDeprecated() - { - $this->expectDeprecation('Since symfony/form 5.1: The "Symfony\Component\Form\Extension\Validator\Util\ServerParams" class is deprecated. Use "Symfony\Component\Form\Util\ServerParams" instead.'); - - new ServerParams(); - } -}