From 0c07d99445653def1e76a17374d255177491ca06 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 24 Apr 2024 12:12:14 +0200 Subject: [PATCH] deprecate the use of option arrays to configure validation constraints --- .../Validator/Constraints/UniqueEntity.php | 2 + .../Doctrine/Validator/DoctrineLoader.php | 4 +- .../Constraints/AbstractComparison.php | 4 + .../Component/Validator/Constraints/Bic.php | 6 + .../Component/Validator/Constraints/Blank.php | 6 + .../Validator/Constraints/Callback.php | 6 + .../Validator/Constraints/CardScheme.php | 4 + .../Validator/Constraints/Cascade.php | 4 + .../Validator/Constraints/Choice.php | 5 + .../Component/Validator/Constraints/Cidr.php | 4 + .../Validator/Constraints/Collection.php | 4 + .../Component/Validator/Constraints/Count.php | 4 + .../Validator/Constraints/CountValidator.php | 8 +- .../Validator/Constraints/Country.php | 6 + .../Validator/Constraints/CssColor.php | 4 + .../Validator/Constraints/Currency.php | 6 + .../Component/Validator/Constraints/Date.php | 6 + .../Validator/Constraints/DateTime.php | 4 + .../Component/Validator/Constraints/Email.php | 10 + .../Validator/Constraints/Expression.php | 2 + .../Constraints/ExpressionSyntax.php | 6 + .../Component/Validator/Constraints/File.php | 6 + .../Validator/Constraints/Hostname.php | 6 + .../Component/Validator/Constraints/Iban.php | 6 + .../Component/Validator/Constraints/Ip.php | 6 + .../Validator/Constraints/IsFalse.php | 6 + .../Validator/Constraints/IsNull.php | 6 + .../Validator/Constraints/IsTrue.php | 6 + .../Component/Validator/Constraints/Isbn.php | 4 + .../Component/Validator/Constraints/Isin.php | 6 + .../Component/Validator/Constraints/Issn.php | 6 + .../Component/Validator/Constraints/Json.php | 6 + .../Validator/Constraints/Language.php | 6 + .../Validator/Constraints/Length.php | 4 + .../Validator/Constraints/Locale.php | 6 + .../Component/Validator/Constraints/Luhn.php | 6 + .../Constraints/NoSuspiciousCharacters.php | 6 + .../Validator/Constraints/NotBlank.php | 6 + .../Constraints/NotCompromisedPassword.php | 6 + .../Validator/Constraints/NotNull.php | 6 + .../Constraints/PasswordStrength.php | 6 + .../Component/Validator/Constraints/Range.php | 6 + .../Component/Validator/Constraints/Regex.php | 4 + .../Component/Validator/Constraints/Time.php | 6 + .../Validator/Constraints/Timezone.php | 4 + .../Validator/Constraints/Traverse.php | 6 + .../Component/Validator/Constraints/Type.php | 4 + .../Component/Validator/Constraints/Ulid.php | 6 + .../Validator/Constraints/Unique.php | 6 + .../Component/Validator/Constraints/Url.php | 6 + .../Component/Validator/Constraints/Uuid.php | 6 + .../Component/Validator/Constraints/Valid.php | 6 + .../Component/Validator/Constraints/When.php | 4 + .../ZeroComparisonConstraintTrait.php | 4 + .../Context/ExecutionContextInterface.php | 2 +- .../Mapping/Loader/AbstractLoader.php | 16 + .../Mapping/Loader/PropertyInfoLoader.php | 6 +- .../Mapping/Loader/XmlFileLoader.php | 4 +- .../Mapping/Loader/YamlFileLoader.php | 6 + .../Tests/Constraints/AllValidatorTest.php | 8 +- .../Constraints/AtLeastOneOfValidatorTest.php | 106 ++-- .../Tests/Constraints/BicValidatorTest.php | 28 +- .../Tests/Constraints/BlankValidatorTest.php | 6 +- .../Constraints/CallbackValidatorTest.php | 36 +- .../Constraints/CardSchemeValidatorTest.php | 14 +- .../Tests/Constraints/ChoiceValidatorTest.php | 264 ++++---- .../Validator/Tests/Constraints/CidrTest.php | 26 +- .../Tests/Constraints/CidrValidatorTest.php | 14 +- .../Tests/Constraints/CollectionTest.php | 56 +- .../CollectionValidatorTestCase.php | 110 ++-- .../Tests/Constraints/CompositeTest.php | 18 +- .../Tests/Constraints/CompoundTest.php | 5 +- .../Constraints/CompoundValidatorTest.php | 2 +- .../Constraints/CountValidatorTestCase.php | 36 +- .../Constraints/CountryValidatorTest.php | 16 +- .../Constraints/CurrencyValidatorTest.php | 4 +- .../Constraints/DateTimeValidatorTest.php | 16 +- .../Tests/Constraints/DateValidatorTest.php | 4 +- .../Constraints/DisableAutoMappingTest.php | 3 + .../Constraints/DivisibleByValidatorTest.php | 6 +- .../Validator/Tests/Constraints/EmailTest.php | 14 +- .../Tests/Constraints/EmailValidatorTest.php | 38 +- .../Constraints/EnableAutoMappingTest.php | 3 + .../Constraints/EqualToValidatorTest.php | 6 +- .../Constraints/ExpressionSyntaxTest.php | 12 +- .../ExpressionSyntaxValidatorTest.php | 44 +- .../Constraints/ExpressionValidatorTest.php | 88 +-- .../Validator/Tests/Constraints/FileTest.php | 12 +- .../Constraints/FileValidatorPathTest.php | 6 +- .../Constraints/FileValidatorTestCase.php | 115 ++-- .../GreaterThanOrEqualValidatorTest.php | 6 +- ...idatorWithPositiveOrZeroConstraintTest.php | 6 + .../Constraints/GreaterThanValidatorTest.php | 6 +- ...hanValidatorWithPositiveConstraintTest.php | 6 + .../Constraints/HostnameValidatorTest.php | 34 +- .../Tests/Constraints/IbanValidatorTest.php | 4 +- .../Constraints/IdenticalToValidatorTest.php | 6 +- .../Tests/Constraints/ImageValidatorTest.php | 400 ++++++------ .../Validator/Tests/Constraints/IpTest.php | 8 +- .../Tests/Constraints/IpValidatorTest.php | 152 +++-- .../Constraints/IsFalseValidatorTest.php | 24 +- .../Tests/Constraints/IsNullValidatorTest.php | 4 +- .../Tests/Constraints/IsTrueValidatorTest.php | 22 +- .../Tests/Constraints/IsbnValidatorTest.php | 33 +- .../Tests/Constraints/IsinValidatorTest.php | 4 +- .../Tests/Constraints/IssnValidatorTest.php | 20 +- .../Tests/Constraints/JsonValidatorTest.php | 4 +- .../Constraints/LanguageValidatorTest.php | 20 +- .../Tests/Constraints/LengthTest.php | 20 +- .../Tests/Constraints/LengthValidatorTest.php | 66 +- .../LessThanOrEqualValidatorTest.php | 6 +- ...idatorWithNegativeOrZeroConstraintTest.php | 6 + .../Constraints/LessThanValidatorTest.php | 6 +- ...hanValidatorWithNegativeConstraintTest.php | 6 + .../Tests/Constraints/LocaleValidatorTest.php | 24 +- .../Tests/Constraints/LuhnValidatorTest.php | 4 +- .../NoSuspiciousCharactersValidatorTest.php | 4 +- .../Tests/Constraints/NotBlankTest.php | 8 +- .../Constraints/NotBlankValidatorTest.php | 40 +- .../NotCompromisedPasswordValidatorTest.php | 35 +- .../Constraints/NotEqualToValidatorTest.php | 6 +- .../NotIdenticalToValidatorTest.php | 6 +- .../Constraints/NotNullValidatorTest.php | 22 +- .../Validator/Tests/Constraints/RangeTest.php | 6 + .../Tests/Constraints/RangeValidatorTest.php | 242 ++++---- .../Validator/Tests/Constraints/RegexTest.php | 24 +- .../Tests/Constraints/RegexValidatorTest.php | 10 +- .../Constraints/SequentiallyValidatorTest.php | 30 +- .../Tests/Constraints/TimeValidatorTest.php | 8 +- .../Tests/Constraints/TimezoneTest.php | 22 +- .../Constraints/TimezoneValidatorTest.php | 56 +- .../Tests/Constraints/TypeValidatorTest.php | 47 +- .../Tests/Constraints/UlidValidatorTest.php | 4 +- .../Tests/Constraints/UniqueTest.php | 6 + .../Tests/Constraints/UniqueValidatorTest.php | 47 +- .../Validator/Tests/Constraints/UrlTest.php | 8 +- .../Tests/Constraints/UrlValidatorTest.php | 46 +- .../Validator/Tests/Constraints/UuidTest.php | 8 +- .../Tests/Constraints/UuidValidatorTest.php | 22 +- .../Validator/Tests/Constraints/ValidTest.php | 2 +- .../Validator/Tests/Constraints/WhenTest.php | 41 +- .../Tests/Constraints/WhenValidatorTest.php | 88 ++- .../Tests/Fixtures/EntityStaticCar.php | 2 +- .../Tests/Fixtures/EntityStaticCarTurbo.php | 2 +- .../Tests/Fixtures/EntityStaticVehicle.php | 2 +- .../LazyLoadingMetadataFactoryTest.php | 28 +- .../Mapping/Loader/AttributeLoaderTest.php | 48 +- .../Mapping/Loader/XmlFileLoaderTest.php | 22 +- .../Mapping/Loader/YamlFileLoaderTest.php | 22 +- .../Tests/Mapping/MemberMetadataTest.php | 4 +- .../Validator/RecursiveValidatorTest.php | 583 +++++++++--------- 151 files changed, 2180 insertions(+), 1707 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php index 02e853016d202..81b15d65b024f 100644 --- a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php +++ b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php @@ -59,6 +59,8 @@ public function __construct( array $options = [], ) { if (\is_array($fields) && \is_string(key($fields))) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($fields, $options); } elseif (null !== $fields) { $options['fields'] = $fields; diff --git a/src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php b/src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php index 93413c4f8e6d8..7cffa1461b48e 100644 --- a/src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php +++ b/src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php @@ -90,7 +90,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool } if (true === (self::getFieldMappingValue($mapping, 'unique') ?? false) && !isset($existingUniqueFields[self::getFieldMappingValue($mapping, 'fieldName')])) { - $metadata->addConstraint(new UniqueEntity(['fields' => self::getFieldMappingValue($mapping, 'fieldName')])); + $metadata->addConstraint(new UniqueEntity(fields: self::getFieldMappingValue($mapping, 'fieldName'))); $loaded = true; } @@ -103,7 +103,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool $metadata->addPropertyConstraint(self::getFieldMappingValue($mapping, 'declaredField'), new Valid()); $loaded = true; } elseif (property_exists($className, self::getFieldMappingValue($mapping, 'fieldName')) && (!$doctrineMetadata->isMappedSuperclass || $metadata->getReflectionClass()->getProperty(self::getFieldMappingValue($mapping, 'fieldName'))->isPrivate())) { - $metadata->addPropertyConstraint(self::getFieldMappingValue($mapping, 'fieldName'), new Length(['max' => self::getFieldMappingValue($mapping, 'length')])); + $metadata->addPropertyConstraint(self::getFieldMappingValue($mapping, 'fieldName'), new Length(max: self::getFieldMappingValue($mapping, 'length'))); $loaded = true; } } elseif (null === $lengthConstraint->max) { diff --git a/src/Symfony/Component/Validator/Constraints/AbstractComparison.php b/src/Symfony/Component/Validator/Constraints/AbstractComparison.php index 5eb8c4696340f..5009d3075bfba 100644 --- a/src/Symfony/Component/Validator/Constraints/AbstractComparison.php +++ b/src/Symfony/Component/Validator/Constraints/AbstractComparison.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\PropertyAccess\PropertyAccess; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\Exception\LogicException; @@ -28,9 +29,12 @@ abstract class AbstractComparison extends Constraint public mixed $value = null; public ?string $propertyPath = null; + #[HasNamedArguments] public function __construct(mixed $value = null, ?string $propertyPath = null, ?string $message = null, ?array $groups = null, mixed $payload = null, array $options = []) { if (\is_array($value)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($value, $options); } elseif (null !== $value) { $options['value'] = $value; diff --git a/src/Symfony/Component/Validator/Constraints/Bic.php b/src/Symfony/Component/Validator/Constraints/Bic.php index 625fb72132418..b1c62dfda8273 100644 --- a/src/Symfony/Component/Validator/Constraints/Bic.php +++ b/src/Symfony/Component/Validator/Constraints/Bic.php @@ -13,6 +13,7 @@ use Symfony\Component\Intl\Countries; use Symfony\Component\PropertyAccess\PropertyAccess; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\Exception\LogicException; @@ -56,12 +57,17 @@ class Bic extends Constraint * @param string|null $ibanPropertyPath Property path to the IBAN value when validating objects * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?string $iban = null, ?string $ibanPropertyPath = null, ?string $ibanMessage = null, ?array $groups = null, mixed $payload = null) { if (!class_exists(Countries::class)) { throw new LogicException('The Intl component is required to use the Bic constraint. Try running "composer require symfony/intl".'); } + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Blank.php b/src/Symfony/Component/Validator/Constraints/Blank.php index 283164fc17136..2ae6bdc2a5883 100644 --- a/src/Symfony/Component/Validator/Constraints/Blank.php +++ b/src/Symfony/Component/Validator/Constraints/Blank.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -33,8 +34,13 @@ class Blank extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options ?? [], $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Callback.php b/src/Symfony/Component/Validator/Constraints/Callback.php index 5ef48d88022be..d1f9c1f978905 100644 --- a/src/Symfony/Component/Validator/Constraints/Callback.php +++ b/src/Symfony/Component/Validator/Constraints/Callback.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -30,16 +31,21 @@ class Callback extends Constraint * @param string|string[]|callable|array|null $callback The callback definition * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(array|string|callable|null $callback = null, ?array $groups = null, mixed $payload = null, array $options = []) { // Invocation through attributes with an array parameter only if (\is_array($callback) && 1 === \count($callback) && isset($callback['value'])) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $callback = $callback['value']; } if (!\is_array($callback) || (!isset($callback['callback']) && !isset($callback['groups']) && !isset($callback['payload']))) { $options['callback'] = $callback; } else { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($callback, $options); } diff --git a/src/Symfony/Component/Validator/Constraints/CardScheme.php b/src/Symfony/Component/Validator/Constraints/CardScheme.php index 965ec1c01819b..f609aab50c32c 100644 --- a/src/Symfony/Component/Validator/Constraints/CardScheme.php +++ b/src/Symfony/Component/Validator/Constraints/CardScheme.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -51,9 +52,12 @@ class CardScheme extends Constraint * @param string[]|null $groups * @param array $options */ + #[HasNamedArguments] public function __construct(array|string|null $schemes, ?string $message = null, ?array $groups = null, mixed $payload = null, array $options = []) { if (\is_array($schemes) && \is_string(key($schemes))) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($schemes, $options); } else { $options['value'] = $schemes; diff --git a/src/Symfony/Component/Validator/Constraints/Cascade.php b/src/Symfony/Component/Validator/Constraints/Cascade.php index a1e7ccfa56379..795ba2a29281a 100644 --- a/src/Symfony/Component/Validator/Constraints/Cascade.php +++ b/src/Symfony/Component/Validator/Constraints/Cascade.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; @@ -28,9 +29,12 @@ class Cascade extends Constraint * @param string[]|string|array|null $exclude Properties excluded from validation * @param array|null $options */ + #[HasNamedArguments] public function __construct(array|string|null $exclude = null, ?array $options = null) { if (\is_array($exclude) && !array_is_list($exclude)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($exclude, $options ?? []); } else { $this->exclude = array_flip((array) $exclude); diff --git a/src/Symfony/Component/Validator/Constraints/Choice.php b/src/Symfony/Component/Validator/Constraints/Choice.php index 743abaf9795a2..22d966e458911 100644 --- a/src/Symfony/Component/Validator/Constraints/Choice.php +++ b/src/Symfony/Component/Validator/Constraints/Choice.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -59,6 +60,7 @@ public function getDefaultOption(): ?string * @param string[]|null $groups * @param bool|null $match Whether to validate the values are part of the choices or not (defaults to true) */ + #[HasNamedArguments] public function __construct( string|array $options = [], ?array $choices = null, @@ -78,7 +80,10 @@ public function __construct( if (\is_array($options) && $options && array_is_list($options)) { $choices ??= $options; $options = []; + } elseif (\is_array($options) && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); } + if (null !== $choices) { $options['value'] = $choices; } diff --git a/src/Symfony/Component/Validator/Constraints/Cidr.php b/src/Symfony/Component/Validator/Constraints/Cidr.php index 98617c5578f29..e711ab89081c6 100644 --- a/src/Symfony/Component/Validator/Constraints/Cidr.php +++ b/src/Symfony/Component/Validator/Constraints/Cidr.php @@ -84,6 +84,10 @@ public function __construct( $payload = null, ?callable $normalizer = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + $this->version = $version ?? $options['version'] ?? $this->version; if (!\array_key_exists($this->version, self::NET_MAXES)) { diff --git a/src/Symfony/Component/Validator/Constraints/Collection.php b/src/Symfony/Component/Validator/Constraints/Collection.php index 3ffd0a6fc6768..5ce38b92fa797 100644 --- a/src/Symfony/Component/Validator/Constraints/Collection.php +++ b/src/Symfony/Component/Validator/Constraints/Collection.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -41,10 +42,13 @@ class Collection extends Composite * @param bool|null $allowExtraFields Whether to allow additional keys not declared in the configured fields (defaults to false) * @param bool|null $allowMissingFields Whether to allow the collection to lack some fields declared in the configured fields (defaults to false) */ + #[HasNamedArguments] public function __construct(mixed $fields = null, ?array $groups = null, mixed $payload = null, ?bool $allowExtraFields = null, ?bool $allowMissingFields = null, ?string $extraFieldsMessage = null, ?string $missingFieldsMessage = null) { if (self::isFieldsOption($fields)) { $fields = ['fields' => $fields]; + } else { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); } parent::__construct($fields, $groups, $payload); diff --git a/src/Symfony/Component/Validator/Constraints/Count.php b/src/Symfony/Component/Validator/Constraints/Count.php index 3e1bf1e6b531f..fedd4d80f1551 100644 --- a/src/Symfony/Component/Validator/Constraints/Count.php +++ b/src/Symfony/Component/Validator/Constraints/Count.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\MissingOptionsException; @@ -50,6 +51,7 @@ class Count extends Constraint * @param string[]|null $groups * @param array $options */ + #[HasNamedArguments] public function __construct( int|array|null $exactly = null, ?int $min = null, @@ -64,6 +66,8 @@ public function __construct( array $options = [], ) { if (\is_array($exactly)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($exactly, $options); $exactly = $options['value'] ?? null; } diff --git a/src/Symfony/Component/Validator/Constraints/CountValidator.php b/src/Symfony/Component/Validator/Constraints/CountValidator.php index 6aa425bdfc17b..06ccec6fe822a 100644 --- a/src/Symfony/Component/Validator/Constraints/CountValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CountValidator.php @@ -70,10 +70,10 @@ public function validate(mixed $value, Constraint $constraint): void ->getValidator() ->inContext($this->context) ->validate($count, [ - new DivisibleBy([ - 'value' => $constraint->divisibleBy, - 'message' => $constraint->divisibleByMessage, - ]), + new DivisibleBy( + value: $constraint->divisibleBy, + message: $constraint->divisibleByMessage, + ), ], $this->context->getGroup()); } } diff --git a/src/Symfony/Component/Validator/Constraints/Country.php b/src/Symfony/Component/Validator/Constraints/Country.php index cb00162123691..4a14211b44686 100644 --- a/src/Symfony/Component/Validator/Constraints/Country.php +++ b/src/Symfony/Component/Validator/Constraints/Country.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Intl\Countries; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\LogicException; @@ -41,6 +42,7 @@ class Country extends Constraint * * @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3#Current_codes */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -52,6 +54,10 @@ public function __construct( throw new LogicException('The Intl component is required to use the Country constraint. Try running "composer require symfony/intl".'); } + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/CssColor.php b/src/Symfony/Component/Validator/Constraints/CssColor.php index 5e67d157ba4dc..f8d112955abae 100644 --- a/src/Symfony/Component/Validator/Constraints/CssColor.php +++ b/src/Symfony/Component/Validator/Constraints/CssColor.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\InvalidArgumentException; @@ -66,6 +67,7 @@ class CssColor extends Constraint * @param string[]|null $groups * @param array|null $options */ + #[HasNamedArguments] public function __construct(array|string $formats = [], ?string $message = null, ?array $groups = null, $payload = null, ?array $options = null) { $validationModesAsString = implode(', ', self::$validationModes); @@ -73,6 +75,8 @@ public function __construct(array|string $formats = [], ?string $message = null, if (!$formats) { $options['value'] = self::$validationModes; } elseif (\is_array($formats) && \is_string(key($formats))) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($formats, $options ?? []); } elseif (\is_array($formats)) { if ([] === array_intersect(self::$validationModes, $formats)) { diff --git a/src/Symfony/Component/Validator/Constraints/Currency.php b/src/Symfony/Component/Validator/Constraints/Currency.php index 337481543a3af..94535732a2be8 100644 --- a/src/Symfony/Component/Validator/Constraints/Currency.php +++ b/src/Symfony/Component/Validator/Constraints/Currency.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Intl\Currencies; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\LogicException; @@ -38,12 +39,17 @@ class Currency extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) { if (!class_exists(Currencies::class)) { throw new LogicException('The Intl component is required to use the Currency constraint. Try running "composer require symfony/intl".'); } + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Date.php b/src/Symfony/Component/Validator/Constraints/Date.php index add1080798026..11cda6e24ca88 100644 --- a/src/Symfony/Component/Validator/Constraints/Date.php +++ b/src/Symfony/Component/Validator/Constraints/Date.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -37,8 +38,13 @@ class Date extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/DateTime.php b/src/Symfony/Component/Validator/Constraints/DateTime.php index da2a2d6ba7a51..d1a202f1b86c6 100644 --- a/src/Symfony/Component/Validator/Constraints/DateTime.php +++ b/src/Symfony/Component/Validator/Constraints/DateTime.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -41,9 +42,12 @@ class DateTime extends Constraint * @param string[]|null $groups * @param array $options */ + #[HasNamedArguments] public function __construct(string|array|null $format = null, ?string $message = null, ?array $groups = null, mixed $payload = null, array $options = []) { if (\is_array($format)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($format, $options); } elseif (null !== $format) { $options['value'] = $format; diff --git a/src/Symfony/Component/Validator/Constraints/Email.php b/src/Symfony/Component/Validator/Constraints/Email.php index 01a91e50b5dc3..8144f6f4fdd8f 100644 --- a/src/Symfony/Component/Validator/Constraints/Email.php +++ b/src/Symfony/Component/Validator/Constraints/Email.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Egulias\EmailValidator\EmailValidator as StrictEmailValidator; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\InvalidArgumentException; use Symfony\Component\Validator\Exception\LogicException; @@ -50,6 +51,7 @@ class Email extends Constraint * @param self::VALIDATION_MODE_*|null $mode The pattern used to validate the email address; pass null to use the default mode configured for the EmailValidator * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -62,6 +64,14 @@ public function __construct( throw new InvalidArgumentException('The "mode" parameter value is not valid.'); } + if (null !== $mode && !\in_array($mode, self::VALIDATION_MODES, true)) { + throw new InvalidArgumentException('The "mode" parameter value is not valid.'); + } + + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Expression.php b/src/Symfony/Component/Validator/Constraints/Expression.php index aa4ee77852a88..00396b55b7c5e 100644 --- a/src/Symfony/Component/Validator/Constraints/Expression.php +++ b/src/Symfony/Component/Validator/Constraints/Expression.php @@ -59,6 +59,8 @@ public function __construct( } if (\is_array($expression)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($expression, $options); } else { $options['value'] = $expression; diff --git a/src/Symfony/Component/Validator/Constraints/ExpressionSyntax.php b/src/Symfony/Component/Validator/Constraints/ExpressionSyntax.php index a03053f0242d3..c1e3ebd6a2fdc 100644 --- a/src/Symfony/Component/Validator/Constraints/ExpressionSyntax.php +++ b/src/Symfony/Component/Validator/Constraints/ExpressionSyntax.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -37,8 +38,13 @@ class ExpressionSyntax extends Constraint * @param string[]|null $allowedVariables Restrict the available variables in the expression to these values (defaults to null that allows any variable) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?string $service = null, ?array $allowedVariables = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/File.php b/src/Symfony/Component/Validator/Constraints/File.php index 86b3b2802f613..45edcfdcf22c9 100644 --- a/src/Symfony/Component/Validator/Constraints/File.php +++ b/src/Symfony/Component/Validator/Constraints/File.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; @@ -89,6 +90,7 @@ class File extends Constraint * * @see https://www.iana.org/assignments/media-types/media-types.xhtml Existing media types */ + #[HasNamedArguments] public function __construct( ?array $options = null, int|string|null $maxSize = null, @@ -116,6 +118,10 @@ public function __construct( array|string|null $extensions = null, ?string $extensionsMessage = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->maxSize = $maxSize ?? $this->maxSize; diff --git a/src/Symfony/Component/Validator/Constraints/Hostname.php b/src/Symfony/Component/Validator/Constraints/Hostname.php index 3090f1ecc54aa..219ef796c7c39 100644 --- a/src/Symfony/Component/Validator/Constraints/Hostname.php +++ b/src/Symfony/Component/Validator/Constraints/Hostname.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -35,6 +36,7 @@ class Hostname extends Constraint * @param bool|null $requireTld Whether to require the hostname to include its top-level domain (defaults to true) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -42,6 +44,10 @@ public function __construct( ?array $groups = null, mixed $payload = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Iban.php b/src/Symfony/Component/Validator/Constraints/Iban.php index 71b6d18c723df..62db3ce5f9760 100644 --- a/src/Symfony/Component/Validator/Constraints/Iban.php +++ b/src/Symfony/Component/Validator/Constraints/Iban.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -45,8 +46,13 @@ class Iban extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Ip.php b/src/Symfony/Component/Validator/Constraints/Ip.php index 4a8b980216a6d..0e25a06a1563d 100644 --- a/src/Symfony/Component/Validator/Constraints/Ip.php +++ b/src/Symfony/Component/Validator/Constraints/Ip.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\Exception\InvalidArgumentException; @@ -111,6 +112,7 @@ class Ip extends Constraint * @param self::V4*|self::V6*|self::ALL*|null $version The IP version to validate (defaults to {@see self::V4}) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $version = null, @@ -119,6 +121,10 @@ public function __construct( ?array $groups = null, mixed $payload = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->version = $version ?? $this->version; diff --git a/src/Symfony/Component/Validator/Constraints/IsFalse.php b/src/Symfony/Component/Validator/Constraints/IsFalse.php index a46b071c99950..9c4e378eb2278 100644 --- a/src/Symfony/Component/Validator/Constraints/IsFalse.php +++ b/src/Symfony/Component/Validator/Constraints/IsFalse.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -33,8 +34,13 @@ class IsFalse extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options ?? [], $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/IsNull.php b/src/Symfony/Component/Validator/Constraints/IsNull.php index 9f86e856030b5..14e1da812512f 100644 --- a/src/Symfony/Component/Validator/Constraints/IsNull.php +++ b/src/Symfony/Component/Validator/Constraints/IsNull.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -33,8 +34,13 @@ class IsNull extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options ?? [], $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/IsTrue.php b/src/Symfony/Component/Validator/Constraints/IsTrue.php index c8418a2803dec..dcfede79892a6 100644 --- a/src/Symfony/Component/Validator/Constraints/IsTrue.php +++ b/src/Symfony/Component/Validator/Constraints/IsTrue.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -33,8 +34,13 @@ class IsTrue extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options ?? [], $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Isbn.php b/src/Symfony/Component/Validator/Constraints/Isbn.php index c1bc83a344021..fe28e60087375 100644 --- a/src/Symfony/Component/Validator/Constraints/Isbn.php +++ b/src/Symfony/Component/Validator/Constraints/Isbn.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -54,6 +55,7 @@ class Isbn extends Constraint * @param string[]|null $groups * @param array $options */ + #[HasNamedArguments] public function __construct( string|array|null $type = null, ?string $message = null, @@ -65,6 +67,8 @@ public function __construct( array $options = [], ) { if (\is_array($type)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($type, $options); } elseif (null !== $type) { $options['value'] = $type; diff --git a/src/Symfony/Component/Validator/Constraints/Isin.php b/src/Symfony/Component/Validator/Constraints/Isin.php index 3f722d21af0cb..9e68cbc46a35a 100644 --- a/src/Symfony/Component/Validator/Constraints/Isin.php +++ b/src/Symfony/Component/Validator/Constraints/Isin.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -42,8 +43,13 @@ class Isin extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Issn.php b/src/Symfony/Component/Validator/Constraints/Issn.php index 7d0e5b51580bd..6e29c944f9c58 100644 --- a/src/Symfony/Component/Validator/Constraints/Issn.php +++ b/src/Symfony/Component/Validator/Constraints/Issn.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -50,6 +51,7 @@ class Issn extends Constraint * @param bool|null $requireHyphen Whether to require a hyphenated ISSN value (defaults to false) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -58,6 +60,10 @@ public function __construct( ?array $groups = null, mixed $payload = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Json.php b/src/Symfony/Component/Validator/Constraints/Json.php index 3b85a347c5c64..b97bbdd798444 100644 --- a/src/Symfony/Component/Validator/Constraints/Json.php +++ b/src/Symfony/Component/Validator/Constraints/Json.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -33,8 +34,13 @@ class Json extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Language.php b/src/Symfony/Component/Validator/Constraints/Language.php index 67c228448c11b..b36a204aae143 100644 --- a/src/Symfony/Component/Validator/Constraints/Language.php +++ b/src/Symfony/Component/Validator/Constraints/Language.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Intl\Languages; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\LogicException; @@ -39,6 +40,7 @@ class Language extends Constraint * @param bool|null $alpha3 Pass true to validate the language with three-letter code (ISO 639-2 (2T)) or false with two-letter code (ISO 639-1) (defaults to false) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -50,6 +52,10 @@ public function __construct( throw new LogicException('The Intl component is required to use the Language constraint. Try running "composer require symfony/intl".'); } + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Length.php b/src/Symfony/Component/Validator/Constraints/Length.php index ebbe4750f06c0..3fc235a35faad 100644 --- a/src/Symfony/Component/Validator/Constraints/Length.php +++ b/src/Symfony/Component/Validator/Constraints/Length.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\InvalidArgumentException; use Symfony\Component\Validator\Exception\MissingOptionsException; @@ -67,6 +68,7 @@ class Length extends Constraint * @param string[]|null $groups * @param array $options */ + #[HasNamedArguments] public function __construct( int|array|null $exactly = null, ?int $min = null, @@ -83,6 +85,8 @@ public function __construct( array $options = [], ) { if (\is_array($exactly)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($exactly, $options); $exactly = $options['value'] ?? null; } diff --git a/src/Symfony/Component/Validator/Constraints/Locale.php b/src/Symfony/Component/Validator/Constraints/Locale.php index fa31fbac41b10..81e29982def3b 100644 --- a/src/Symfony/Component/Validator/Constraints/Locale.php +++ b/src/Symfony/Component/Validator/Constraints/Locale.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Intl\Locales; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\LogicException; @@ -39,6 +40,7 @@ class Locale extends Constraint * @param bool|null $canonicalize Whether to canonicalize the value before validation (defaults to true) (see {@see https://www.php.net/manual/en/locale.canonicalize.php}) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -50,6 +52,10 @@ public function __construct( throw new LogicException('The Intl component is required to use the Locale constraint. Try running "composer require symfony/intl".'); } + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Luhn.php b/src/Symfony/Component/Validator/Constraints/Luhn.php index df26b283e6696..3b372cb58f575 100644 --- a/src/Symfony/Component/Validator/Constraints/Luhn.php +++ b/src/Symfony/Component/Validator/Constraints/Luhn.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -39,12 +40,17 @@ class Luhn extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/NoSuspiciousCharacters.php b/src/Symfony/Component/Validator/Constraints/NoSuspiciousCharacters.php index 2dd6fb8f5a025..23277564f2b01 100644 --- a/src/Symfony/Component/Validator/Constraints/NoSuspiciousCharacters.php +++ b/src/Symfony/Component/Validator/Constraints/NoSuspiciousCharacters.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\LogicException; @@ -89,6 +90,7 @@ class NoSuspiciousCharacters extends Constraint * @param string[]|null $locales Restrict the string's characters to those normally used with these locales. Pass null to use the default locales configured for the NoSuspiciousCharactersValidator. (defaults to null) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $restrictionLevelMessage = null, @@ -105,6 +107,10 @@ public function __construct( throw new LogicException('The intl extension is required to use the NoSuspiciousCharacters constraint.'); } + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->restrictionLevelMessage = $restrictionLevelMessage ?? $this->restrictionLevelMessage; diff --git a/src/Symfony/Component/Validator/Constraints/NotBlank.php b/src/Symfony/Component/Validator/Constraints/NotBlank.php index 96b49a302a592..58ebce774d5c5 100644 --- a/src/Symfony/Component/Validator/Constraints/NotBlank.php +++ b/src/Symfony/Component/Validator/Constraints/NotBlank.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\InvalidArgumentException; @@ -39,8 +40,13 @@ class NotBlank extends Constraint * @param bool|null $allowNull Whether to allow null values (defaults to false) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?bool $allowNull = null, ?callable $normalizer = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options ?? [], $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/NotCompromisedPassword.php b/src/Symfony/Component/Validator/Constraints/NotCompromisedPassword.php index a122089af97b0..fbaf689bfeb86 100644 --- a/src/Symfony/Component/Validator/Constraints/NotCompromisedPassword.php +++ b/src/Symfony/Component/Validator/Constraints/NotCompromisedPassword.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -37,6 +38,7 @@ class NotCompromisedPassword extends Constraint * @param bool|null $skipOnError Whether to ignore HTTP errors while requesting the API and thus consider the password valid (defaults to false) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -45,6 +47,10 @@ public function __construct( ?array $groups = null, mixed $payload = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/NotNull.php b/src/Symfony/Component/Validator/Constraints/NotNull.php index 32a327a57c491..325fb1dc96e9e 100644 --- a/src/Symfony/Component/Validator/Constraints/NotNull.php +++ b/src/Symfony/Component/Validator/Constraints/NotNull.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -33,8 +34,13 @@ class NotNull extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options ?? [], $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/PasswordStrength.php b/src/Symfony/Component/Validator/Constraints/PasswordStrength.php index 1827c5fb4f70f..ac13c84a20463 100644 --- a/src/Symfony/Component/Validator/Constraints/PasswordStrength.php +++ b/src/Symfony/Component/Validator/Constraints/PasswordStrength.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; @@ -43,8 +44,13 @@ final class PasswordStrength extends Constraint * @param self::STRENGTH_*|null $minScore The minimum required strength of the password (defaults to {@see PasswordStrength::STRENGTH_MEDIUM}) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct(?array $options = null, ?int $minScore = null, ?array $groups = null, mixed $payload = null, ?string $message = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + $options['minScore'] ??= self::STRENGTH_MEDIUM; parent::__construct($options, $groups, $payload); diff --git a/src/Symfony/Component/Validator/Constraints/Range.php b/src/Symfony/Component/Validator/Constraints/Range.php index e91aaf98f1066..b9363127b7c4e 100644 --- a/src/Symfony/Component/Validator/Constraints/Range.php +++ b/src/Symfony/Component/Validator/Constraints/Range.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\PropertyAccess\PropertyAccess; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\Exception\LogicException; @@ -57,6 +58,7 @@ class Range extends Constraint * @param string|null $maxPropertyPath Property path to the max value * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $notInRangeMessage = null, @@ -71,6 +73,10 @@ public function __construct( ?array $groups = null, mixed $payload = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->notInRangeMessage = $notInRangeMessage ?? $this->notInRangeMessage; diff --git a/src/Symfony/Component/Validator/Constraints/Regex.php b/src/Symfony/Component/Validator/Constraints/Regex.php index 1ce86eec302c0..0dbcd3957330e 100644 --- a/src/Symfony/Component/Validator/Constraints/Regex.php +++ b/src/Symfony/Component/Validator/Constraints/Regex.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\InvalidArgumentException; @@ -42,6 +43,7 @@ class Regex extends Constraint * @param string[]|null $groups * @param array $options */ + #[HasNamedArguments] public function __construct( string|array|null $pattern, ?string $message = null, @@ -53,6 +55,8 @@ public function __construct( array $options = [], ) { if (\is_array($pattern)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($pattern, $options); } elseif (null !== $pattern) { $options['value'] = $pattern; diff --git a/src/Symfony/Component/Validator/Constraints/Time.php b/src/Symfony/Component/Validator/Constraints/Time.php index dca9537cf68d9..4615824ef1184 100644 --- a/src/Symfony/Component/Validator/Constraints/Time.php +++ b/src/Symfony/Component/Validator/Constraints/Time.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -37,6 +38,7 @@ class Time extends Constraint * @param string[]|null $groups * @param bool|null $withSeconds Whether to allow seconds in the given value (defaults to true) */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -44,6 +46,10 @@ public function __construct( mixed $payload = null, ?bool $withSeconds = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->withSeconds = $withSeconds ?? $this->withSeconds; diff --git a/src/Symfony/Component/Validator/Constraints/Timezone.php b/src/Symfony/Component/Validator/Constraints/Timezone.php index de10e280353a5..c97bac3b1b985 100644 --- a/src/Symfony/Component/Validator/Constraints/Timezone.php +++ b/src/Symfony/Component/Validator/Constraints/Timezone.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; @@ -49,6 +50,7 @@ class Timezone extends Constraint * * @see \DateTimeZone */ + #[HasNamedArguments] public function __construct( int|array|null $zone = null, ?string $message = null, @@ -59,6 +61,8 @@ public function __construct( array $options = [], ) { if (\is_array($zone)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($zone, $options); } elseif (null !== $zone) { $options['value'] = $zone; diff --git a/src/Symfony/Component/Validator/Constraints/Traverse.php b/src/Symfony/Component/Validator/Constraints/Traverse.php index 8f999adee16a7..62055d9979fe7 100644 --- a/src/Symfony/Component/Validator/Constraints/Traverse.php +++ b/src/Symfony/Component/Validator/Constraints/Traverse.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; @@ -27,12 +28,17 @@ class Traverse extends Constraint /** * @param bool|array|null $traverse Whether to traverse the given object or not (defaults to true). Pass an associative array to configure the constraint's options (e.g. payload). */ + #[HasNamedArguments] public function __construct(bool|array|null $traverse = null) { if (\is_array($traverse) && \array_key_exists('groups', $traverse)) { throw new ConstraintDefinitionException(sprintf('The option "groups" is not supported by the constraint "%s".', __CLASS__)); } + if (\is_array($traverse)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($traverse); } diff --git a/src/Symfony/Component/Validator/Constraints/Type.php b/src/Symfony/Component/Validator/Constraints/Type.php index 0482ff253d423..fccdb11ca660c 100644 --- a/src/Symfony/Component/Validator/Constraints/Type.php +++ b/src/Symfony/Component/Validator/Constraints/Type.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -35,9 +36,12 @@ class Type extends Constraint * @param string[]|null $groups * @param array $options */ + #[HasNamedArguments] public function __construct(string|array|null $type, ?string $message = null, ?array $groups = null, mixed $payload = null, array $options = []) { if (\is_array($type) && \is_string(key($type))) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($type, $options); } elseif (null !== $type) { $options['value'] = $type; diff --git a/src/Symfony/Component/Validator/Constraints/Ulid.php b/src/Symfony/Component/Validator/Constraints/Ulid.php index a5e2a47b9face..d4aba653420c5 100644 --- a/src/Symfony/Component/Validator/Constraints/Ulid.php +++ b/src/Symfony/Component/Validator/Constraints/Ulid.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -41,12 +42,17 @@ class Ulid extends Constraint * @param array|null $options * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Unique.php b/src/Symfony/Component/Validator/Constraints/Unique.php index c759fac634349..04ae84cfad5da 100644 --- a/src/Symfony/Component/Validator/Constraints/Unique.php +++ b/src/Symfony/Component/Validator/Constraints/Unique.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\InvalidArgumentException; @@ -39,6 +40,7 @@ class Unique extends Constraint * @param string[]|null $groups * @param string[]|string|null $fields Defines the key or keys in the collection that should be checked for uniqueness (defaults to null, which ensure uniqueness for all keys) */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -47,6 +49,10 @@ public function __construct( mixed $payload = null, array|string|null $fields = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Url.php b/src/Symfony/Component/Validator/Constraints/Url.php index 9481f93956350..a60b83cb9abea 100644 --- a/src/Symfony/Component/Validator/Constraints/Url.php +++ b/src/Symfony/Component/Validator/Constraints/Url.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\InvalidArgumentException; @@ -45,6 +46,7 @@ class Url extends Constraint * @param string[]|null $groups * @param bool|null $requireTld Whether to require the URL to include a top-level domain (defaults to false) */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -55,6 +57,10 @@ public function __construct( mixed $payload = null, ?bool $requireTld = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); if (null === ($options['requireTld'] ?? $requireTld)) { diff --git a/src/Symfony/Component/Validator/Constraints/Uuid.php b/src/Symfony/Component/Validator/Constraints/Uuid.php index 1b028f1a9ddfa..b01ea302fce16 100644 --- a/src/Symfony/Component/Validator/Constraints/Uuid.php +++ b/src/Symfony/Component/Validator/Constraints/Uuid.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\InvalidArgumentException; @@ -100,6 +101,7 @@ class Uuid extends Constraint * @param bool|null $strict Whether to force the value to follow the RFC's input format rules; pass false to allow alternate formats (defaults to true) * @param string[]|null $groups */ + #[HasNamedArguments] public function __construct( ?array $options = null, ?string $message = null, @@ -109,6 +111,10 @@ public function __construct( ?array $groups = null, mixed $payload = null, ) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/src/Symfony/Component/Validator/Constraints/Valid.php b/src/Symfony/Component/Validator/Constraints/Valid.php index f94d959a3ce26..22a8618fd704a 100644 --- a/src/Symfony/Component/Validator/Constraints/Valid.php +++ b/src/Symfony/Component/Validator/Constraints/Valid.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; /** @@ -28,8 +29,13 @@ class Valid extends Constraint * @param string[]|null $groups * @param bool|null $traverse Whether to validate {@see \Traversable} objects (defaults to true) */ + #[HasNamedArguments] public function __construct(?array $options = null, ?array $groups = null, $payload = null, ?bool $traverse = null) { + if (null !== $options && [] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + parent::__construct($options ?? [], $groups, $payload); $this->traverse = $traverse ?? $this->traverse; diff --git a/src/Symfony/Component/Validator/Constraints/When.php b/src/Symfony/Component/Validator/Constraints/When.php index 52121c41c03ce..2907704e0ead3 100644 --- a/src/Symfony/Component/Validator/Constraints/When.php +++ b/src/Symfony/Component/Validator/Constraints/When.php @@ -13,6 +13,7 @@ use Symfony\Component\ExpressionLanguage\Expression; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; +use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\LogicException; @@ -35,6 +36,7 @@ class When extends Composite * @param string[]|null $groups * @param array $options */ + #[HasNamedArguments] public function __construct(string|Expression|array $expression, array|Constraint|null $constraints = null, ?array $values = null, ?array $groups = null, $payload = null, array $options = []) { if (!class_exists(ExpressionLanguage::class)) { @@ -42,6 +44,8 @@ public function __construct(string|Expression|array $expression, array|Constrain } if (\is_array($expression)) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + $options = array_merge($expression, $options); } else { $options['expression'] = $expression; diff --git a/src/Symfony/Component/Validator/Constraints/ZeroComparisonConstraintTrait.php b/src/Symfony/Component/Validator/Constraints/ZeroComparisonConstraintTrait.php index 4c816474f9701..becda2c199009 100644 --- a/src/Symfony/Component/Validator/Constraints/ZeroComparisonConstraintTrait.php +++ b/src/Symfony/Component/Validator/Constraints/ZeroComparisonConstraintTrait.php @@ -25,6 +25,10 @@ public function __construct(?array $options = null, ?string $message = null, ?ar { $options ??= []; + if ([] !== $options) { + trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class); + } + if (isset($options['propertyPath'])) { throw new ConstraintDefinitionException(sprintf('The "propertyPath" option of the "%s" constraint cannot be set.', static::class)); } diff --git a/src/Symfony/Component/Validator/Context/ExecutionContextInterface.php b/src/Symfony/Component/Validator/Context/ExecutionContextInterface.php index bd7ec5f9641f7..56e39bd6ae4f6 100644 --- a/src/Symfony/Component/Validator/Context/ExecutionContextInterface.php +++ b/src/Symfony/Component/Validator/Context/ExecutionContextInterface.php @@ -95,7 +95,7 @@ public function buildViolation(string $message, array $parameters = []): Constra * { * $validator = $this->context->getValidator(); * - * $violations = $validator->validate($value, new Length(['min' => 3])); + * $violations = $validator->validate($value, new Length(min: 3)); * * if (count($violations) > 0) { * // ... diff --git a/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php index 1d6ffb7889489..d0d754c8a9edd 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php @@ -85,6 +85,22 @@ protected function newConstraint(string $name, mixed $options = null): Constrain } if ($this->namedArgumentsCache[$className] ??= (bool) (new \ReflectionMethod($className, '__construct'))->getAttributes(HasNamedArguments::class)) { + if (null === $options) { + return new $className(); + } + + if (\is_scalar($options) || is_object($options)) { + return new $className($options); + } + + if (\is_array($options) && 1 === \count($options) && isset($options['value'])) { + return new $className($options['value']); + } + + if (isset($options['groups']) && !\is_array($options['groups'])) { + $options['groups'] = (array) $options['groups']; + } + return new $className(...$options); } diff --git a/src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php index f878974ecc811..0146bb8bf49ff 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php @@ -198,17 +198,17 @@ private function getTypeConstraintLegacy(string $builtinType, PropertyInfoType $ private function getTypeConstraint(TypeInfoType $type): ?Type { if ($type instanceof UnionType || $type instanceof IntersectionType) { - return ($type->isA(TypeIdentifier::INT) || $type->isA(TypeIdentifier::FLOAT) || $type->isA(TypeIdentifier::STRING) || $type->isA(TypeIdentifier::BOOL)) ? new Type(['type' => 'scalar']) : null; + return ($type->isA(TypeIdentifier::INT) || $type->isA(TypeIdentifier::FLOAT) || $type->isA(TypeIdentifier::STRING) || $type->isA(TypeIdentifier::BOOL)) ? new Type('scalar') : null; } $baseType = $type->getBaseType(); if ($baseType instanceof ObjectType) { - return new Type(['type' => $baseType->getClassName()]); + return new Type($baseType->getClassName()); } if (TypeIdentifier::MIXED !== $baseType->getTypeIdentifier()) { - return new Type(['type' => $baseType->getTypeIdentifier()->value]); + return new Type($baseType->getTypeIdentifier()->value); } return null; diff --git a/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php index 1e070d609d337..5da89c28ffacd 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php @@ -80,7 +80,9 @@ protected function parseConstraints(\SimpleXMLElement $nodes): array foreach ($nodes as $node) { if (\count($node) > 0) { if (\count($node->value) > 0) { - $options = $this->parseValues($node->value); + $options = [ + 'value' => $this->parseValues($node->value), + ]; } elseif (\count($node->constraint) > 0) { $options = $this->parseConstraints($node->constraint); } elseif (\count($node->option) > 0) { diff --git a/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php index 34eaf4e5bb067..b117d7932be5e 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php @@ -86,6 +86,12 @@ protected function parseNodes(array $nodes): array $options = $this->parseNodes($options); } + if (null !== $options && (!\is_array($options) || array_is_list($options))) { + $options = [ + 'value' => $options, + ]; + } + $values[] = $this->newConstraint(key($childNodes), $options); } else { if (\is_array($childNodes)) { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php index 65dae62756d3a..ee6a291744a66 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php @@ -27,7 +27,7 @@ protected function createValidator(): AllValidator public function testNullIsValid() { - $this->validator->validate(null, new All(new Range(['min' => 4]))); + $this->validator->validate(null, new All(new Range(min: 4))); $this->assertNoViolation(); } @@ -35,7 +35,7 @@ public function testNullIsValid() public function testThrowsExceptionIfNotTraversable() { $this->expectException(UnexpectedValueException::class); - $this->validator->validate('foo.barbar', new All(new Range(['min' => 4]))); + $this->validator->validate('foo.barbar', new All(new Range(min: 4))); } /** @@ -43,7 +43,7 @@ public function testThrowsExceptionIfNotTraversable() */ public function testWalkSingleConstraint($array) { - $constraint = new Range(['min' => 4]); + $constraint = new Range(min: 4); $i = 0; @@ -61,7 +61,7 @@ public function testWalkSingleConstraint($array) */ public function testWalkMultipleConstraints($array) { - $constraint1 = new Range(['min' => 4]); + $constraint1 = new Range(min: 4); $constraint2 = new NotNull(); $constraints = [$constraint1, $constraint2]; diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php index 9e2da70f7f00f..6603dcc4d4d5b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php @@ -64,31 +64,31 @@ public static function getValidCombinations() { return [ ['symfony', [ - new Length(['min' => 10]), - new EqualTo(['value' => 'symfony']), + new Length(min: 10), + new EqualTo('symfony'), ]], [150, [ - new Range(['min' => 10, 'max' => 20]), - new GreaterThanOrEqual(['value' => 100]), + new Range(min: 10, max: 20), + new GreaterThanOrEqual(100), ]], [7, [ - new LessThan(['value' => 5]), - new IdenticalTo(['value' => 7]), + new LessThan(5), + new IdenticalTo(7), ]], [-3, [ - new DivisibleBy(['value' => 4]), + new DivisibleBy(4), new Negative(), ]], ['FOO', [ - new Choice(['choices' => ['bar', 'BAR']]), - new Regex(['pattern' => '/foo/i']), + new Choice(['bar', 'BAR']), + new Regex('/foo/i'), ]], ['fr', [ new Country(), new Language(), ]], [[1, 3, 5], [ - new Count(['min' => 5]), + new Count(min: 5), new Unique(), ]], ]; @@ -99,7 +99,7 @@ public static function getValidCombinations() */ public function testInvalidCombinationsWithDefaultMessage($value, $constraints) { - $atLeastOneOf = new AtLeastOneOf(['constraints' => $constraints]); + $atLeastOneOf = new AtLeastOneOf($constraints); $validator = Validation::createValidator(); $message = [$atLeastOneOf->message]; @@ -121,7 +121,11 @@ public function testInvalidCombinationsWithDefaultMessage($value, $constraints) */ public function testInvalidCombinationsWithCustomMessage($value, $constraints) { - $atLeastOneOf = new AtLeastOneOf(['constraints' => $constraints, 'message' => 'foo', 'includeInternalMessages' => false]); + $atLeastOneOf = new AtLeastOneOf( + constraints: $constraints, + message: 'foo', + includeInternalMessages: false, + ); $violations = Validation::createValidator()->validate($value, $atLeastOneOf); @@ -133,31 +137,31 @@ public static function getInvalidCombinations() { return [ ['symphony', [ - new Length(['min' => 10]), - new EqualTo(['value' => 'symfony']), + new Length(min: 10), + new EqualTo('symfony'), ]], [70, [ - new Range(['min' => 10, 'max' => 20]), - new GreaterThanOrEqual(['value' => 100]), + new Range(min: 10, max: 20), + new GreaterThanOrEqual(100), ]], [8, [ - new LessThan(['value' => 5]), - new IdenticalTo(['value' => 7]), + new LessThan(5), + new IdenticalTo(7), ]], [3, [ - new DivisibleBy(['value' => 4]), + new DivisibleBy(4), new Negative(), ]], ['F_O_O', [ - new Choice(['choices' => ['bar', 'BAR']]), - new Regex(['pattern' => '/foo/i']), + new Choice(['bar', 'BAR']), + new Regex('/foo/i'), ]], ['f_r', [ new Country(), new Language(), ]], [[1, 3, 3], [ - new Count(['min' => 5]), + new Count(min: 5), new Unique(), ]], ]; @@ -167,21 +171,21 @@ public function testGroupsArePropagatedToNestedConstraints() { $validator = Validation::createValidator(); - $violations = $validator->validate(50, new AtLeastOneOf([ - 'constraints' => [ - new Range([ - 'groups' => 'non_default_group', - 'min' => 10, - 'max' => 20, - ]), - new Range([ - 'groups' => 'non_default_group', - 'min' => 30, - 'max' => 40, - ]), + $violations = $validator->validate(50, new AtLeastOneOf( + constraints: [ + new Range( + groups: ['non_default_group'], + min: 10, + max: 20, + ), + new Range( + groups: ['non_default_group'], + min: 30, + max: 40, + ), ], - 'groups' => 'non_default_group', - ]), 'non_default_group'); + groups: ['non_default_group'], + ), ['non_default_group']); $this->assertCount(1, $violations); } @@ -219,9 +223,9 @@ public function testEmbeddedMessageTakenFromFailingConstraint() public function getMetadataFor($classOrObject): MetadataInterface { return (new ClassMetadata(Data::class)) - ->addPropertyConstraint('foo', new NotNull(['message' => 'custom message foo'])) + ->addPropertyConstraint('foo', new NotNull(message: 'custom message foo')) ->addPropertyConstraint('bar', new AtLeastOneOf([ - new NotNull(['message' => 'custom message bar']), + new NotNull(message: 'custom message bar'), ])) ; } @@ -245,20 +249,20 @@ public function testNestedConstraintsAreNotExecutedWhenGroupDoesNotMatch() { $validator = Validation::createValidator(); - $violations = $validator->validate(50, new AtLeastOneOf([ - 'constraints' => [ - new Range([ - 'groups' => 'adult', - 'min' => 18, - 'max' => 55, - ]), - new GreaterThan([ - 'groups' => 'senior', - 'value' => 55, - ]), + $violations = $validator->validate(50, new AtLeastOneOf( + constraints: [ + new Range( + groups: ['adult'], + min: 18, + max: 55, + ), + new GreaterThan( + groups: ['senior'], + value: 55, + ), ], - 'groups' => ['adult', 'senior'], - ]), 'senior'); + groups: ['adult', 'senior'], + ), 'senior'); $this->assertCount(1, $violations); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php index 8b3c815423a48..09be3cbf09245 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php @@ -42,7 +42,7 @@ public function testEmptyStringIsValid() public function testValidComparisonToPropertyPath() { - $constraint = new Bic(['ibanPropertyPath' => 'value']); + $constraint = new Bic(ibanPropertyPath: 'value'); $object = new BicComparisonTestClass('FR14 2004 1010 0505 0001 3M02 606'); @@ -55,7 +55,7 @@ public function testValidComparisonToPropertyPath() public function testInvalidComparisonToPropertyPath() { - $constraint = new Bic(['ibanPropertyPath' => 'value']); + $constraint = new Bic(ibanPropertyPath: 'value'); $constraint->ibanMessage = 'Constraint Message'; $object = new BicComparisonTestClass('FR14 2004 1010 0505 0001 3M02 606'); @@ -91,7 +91,7 @@ public function testInvalidComparisonToPropertyPathFromAttribute() public function testValidComparisonToValue() { - $constraint = new Bic(['iban' => 'FR14 2004 1010 0505 0001 3M02 606']); + $constraint = new Bic(iban: 'FR14 2004 1010 0505 0001 3M02 606'); $constraint->ibanMessage = 'Constraint Message'; $this->validator->validate('SOGEFRPP', $constraint); @@ -101,7 +101,7 @@ public function testValidComparisonToValue() public function testInvalidComparisonToValue() { - $constraint = new Bic(['iban' => 'FR14 2004 1010 0505 0001 3M02 606']); + $constraint = new Bic(iban: 'FR14 2004 1010 0505 0001 3M02 606'); $constraint->ibanMessage = 'Constraint Message'; $this->validator->validate('UNCRIT2B912', $constraint); @@ -131,7 +131,7 @@ public function testInvalidComparisonToValueFromAttribute() public function testNoViolationOnNullObjectWithPropertyPath() { - $constraint = new Bic(['ibanPropertyPath' => 'propertyPath']); + $constraint = new Bic(ibanPropertyPath: 'propertyPath'); $this->setObject(null); @@ -144,10 +144,10 @@ public function testThrowsConstraintExceptionIfBothValueAndPropertyPath() { $this->expectException(ConstraintDefinitionException::class); $this->expectExceptionMessage('The "iban" and "ibanPropertyPath" options of the Iban constraint cannot be used at the same time'); - new Bic([ - 'iban' => 'value', - 'ibanPropertyPath' => 'propertyPath', - ]); + new Bic( + iban: 'value', + ibanPropertyPath: 'propertyPath', + ); } public function testThrowsConstraintExceptionIfBothValueAndPropertyPathNamed() @@ -160,7 +160,7 @@ public function testThrowsConstraintExceptionIfBothValueAndPropertyPathNamed() public function testInvalidValuePath() { - $constraint = new Bic(['ibanPropertyPath' => 'foo']); + $constraint = new Bic(ibanPropertyPath: 'foo'); $this->expectException(ConstraintDefinitionException::class); $this->expectExceptionMessage(sprintf('Invalid property path "foo" provided to "%s" constraint', $constraint::class)); @@ -206,9 +206,9 @@ public static function getValidBics() */ public function testInvalidBics($bic, $code) { - $constraint = new Bic([ - 'message' => 'myMessage', - ]); + $constraint = new Bic( + message: 'myMessage', + ); $this->validator->validate($bic, $constraint); @@ -266,7 +266,7 @@ public static function getInvalidBics() */ public function testValidBicSpecialCases(string $bic, string $iban) { - $constraint = new Bic(['iban' => $iban]); + $constraint = new Bic(iban: $iban); $this->validator->validate($bic, $constraint); $this->assertNoViolation(); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php index 9643c6793c9cb..21d3fc83e96e7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php @@ -41,9 +41,9 @@ public function testBlankIsValid() */ public function testInvalidValues($value, $valueAsString) { - $constraint = new Blank([ - 'message' => 'myMessage', - ]); + $constraint = new Blank( + message: 'myMessage', + ); $this->validator->validate($value, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php index e888baa7a6596..08256bb4aedb1 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php @@ -74,7 +74,7 @@ public function testSingleMethod() public function testSingleMethodExplicitName() { $object = new CallbackValidatorTest_Object(); - $constraint = new Callback(['callback' => 'validate']); + $constraint = new Callback('validate'); $this->validator->validate($object, $constraint); @@ -129,13 +129,11 @@ public function testClosureNullObject() public function testClosureExplicitName() { $object = new CallbackValidatorTest_Object(); - $constraint = new Callback([ - 'callback' => function ($object, ExecutionContextInterface $context) { - $context->addViolation('My message', ['{{ value }}' => 'foobar']); + $constraint = new Callback(function ($object, ExecutionContextInterface $context) { + $context->addViolation('My message', ['{{ value }}' => 'foobar']); - return false; - }, - ]); + return false; + }); $this->validator->validate($object, $constraint); @@ -170,9 +168,7 @@ public function testArrayCallableNullObject() public function testArrayCallableExplicitName() { $object = new CallbackValidatorTest_Object(); - $constraint = new Callback([ - 'callback' => [__CLASS__.'_Class', 'validateCallback'], - ]); + $constraint = new Callback([__CLASS__.'_Class', 'validateCallback']); $this->validator->validate($object, $constraint); @@ -186,7 +182,7 @@ public function testExpectValidMethods() $this->expectException(ConstraintDefinitionException::class); $object = new CallbackValidatorTest_Object(); - $this->validator->validate($object, new Callback(['callback' => ['foobar']])); + $this->validator->validate($object, new Callback(['foobar'])); } public function testExpectValidCallbacks() @@ -194,7 +190,7 @@ public function testExpectValidCallbacks() $this->expectException(ConstraintDefinitionException::class); $object = new CallbackValidatorTest_Object(); - $this->validator->validate($object, new Callback(['callback' => ['foo', 'bar']])); + $this->validator->validate($object, new Callback(['foo', 'bar'])); } public function testConstraintGetTargets() @@ -215,14 +211,14 @@ public function testNoConstructorArguments() public function testAttributeInvocationSingleValued() { - $constraint = new Callback(['value' => 'validateStatic']); + $constraint = new Callback('validateStatic'); $this->assertEquals(new Callback('validateStatic'), $constraint); } public function testAttributeInvocationMultiValued() { - $constraint = new Callback(['value' => [__CLASS__.'_Class', 'validateCallback']]); + $constraint = new Callback([__CLASS__.'_Class', 'validateCallback']); $this->assertEquals(new Callback([__CLASS__.'_Class', 'validateCallback']), $constraint); } @@ -235,10 +231,10 @@ public function testPayloadIsPassedToCallback() $payloadCopy = $payload; }; - $constraint = new Callback([ - 'callback' => $callback, - 'payload' => 'Hello world!', - ]); + $constraint = new Callback( + callback: $callback, + payload: 'Hello world!', + ); $this->validator->validate($object, $constraint); $this->assertEquals('Hello world!', $payloadCopy); @@ -249,9 +245,7 @@ public function testPayloadIsPassedToCallback() $payloadCopy = 'Replace me!'; $payloadCopy = 'Replace me!'; - $constraint = new Callback([ - 'callback' => $callback, - ]); + $constraint = new Callback($callback); $this->validator->validate($object, $constraint); $this->assertNull($payloadCopy); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php index 7b28ca9dd15d2..93275d1bdb31c 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php @@ -24,14 +24,14 @@ protected function createValidator(): CardSchemeValidator public function testNullIsValid() { - $this->validator->validate(null, new CardScheme(['schemes' => []])); + $this->validator->validate(null, new CardScheme([])); $this->assertNoViolation(); } public function testEmptyStringIsValid() { - $this->validator->validate('', new CardScheme(['schemes' => []])); + $this->validator->validate('', new CardScheme([])); $this->assertNoViolation(); } @@ -41,7 +41,7 @@ public function testEmptyStringIsValid() */ public function testValidNumbers($scheme, $number) { - $this->validator->validate($number, new CardScheme(['schemes' => $scheme])); + $this->validator->validate($number, new CardScheme($scheme)); $this->assertNoViolation(); } @@ -61,10 +61,10 @@ public function testValidNumberWithOrderedArguments() */ public function testInvalidNumbers($scheme, $number, $code) { - $constraint = new CardScheme([ - 'schemes' => $scheme, - 'message' => 'myMessage', - ]); + $constraint = new CardScheme( + schemes: $scheme, + message: 'myMessage', + ); $this->validator->validate($number, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index 8b16e7e86997d..951686e5fbec2 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -42,22 +42,17 @@ public function objectMethodCallback() public function testExpectArrayIfMultipleIsTrue() { $this->expectException(UnexpectedValueException::class); - $constraint = new Choice([ - 'choices' => ['foo', 'bar'], - 'multiple' => true, - ]); + $constraint = new Choice( + choices: ['foo', 'bar'], + multiple: true, + ); $this->validator->validate('asdf', $constraint); } public function testNullIsValid() { - $this->validator->validate( - null, - new Choice([ - 'choices' => ['foo', 'bar'], - ]) - ); + $this->validator->validate(null, new Choice(choices: ['foo', 'bar'])); $this->assertNoViolation(); } @@ -71,7 +66,7 @@ public function testChoicesOrCallbackExpected() public function testValidCallbackExpected() { $this->expectException(ConstraintDefinitionException::class); - $this->validator->validate('foobar', new Choice(['callback' => 'abcd'])); + $this->validator->validate('foobar', new Choice(callback: 'abcd')); } /** @@ -86,12 +81,27 @@ public function testValidChoiceArray(Choice $constraint) public static function provideConstraintsWithChoicesArray(): iterable { - yield 'Doctrine style' => [new Choice(['choices' => ['foo', 'bar']])]; - yield 'Doctrine default option' => [new Choice(['value' => ['foo', 'bar']])]; yield 'first argument' => [new Choice(['foo', 'bar'])]; yield 'named arguments' => [new Choice(choices: ['foo', 'bar'])]; } + /** + * @group legacy + * @dataProvider provideLegacyConstraintsWithChoicesArrayDoctrineStyle + */ + public function testValidChoiceArrayDoctrineStyle(Choice $constraint) + { + $this->validator->validate('bar', $constraint); + + $this->assertNoViolation(); + } + + public static function provideLegacyConstraintsWithChoicesArrayDoctrineStyle(): iterable + { + yield 'Doctrine style' => [new Choice(['choices' => ['foo', 'bar']])]; + yield 'Doctrine default option' => [new Choice(['value' => ['foo', 'bar']])]; + } + /** * @dataProvider provideConstraintsWithCallbackFunction */ @@ -103,15 +113,30 @@ public function testValidChoiceCallbackFunction(Choice $constraint) } public static function provideConstraintsWithCallbackFunction(): iterable + { + yield 'named arguments, namespaced function' => [new Choice(callback: __NAMESPACE__.'\choice_callback')]; + yield 'named arguments, closure' => [new Choice(callback: fn () => ['foo', 'bar'])]; + yield 'named arguments, static method' => [new Choice(callback: [__CLASS__, 'staticCallback'])]; + } + + /** + * @group legacy + * @dataProvider provideLegacyConstraintsWithCallbackFunctionDoctrineStyle + */ + public function testValidChoiceCallbackFunctionDoctrineStyle(Choice $constraint) + { + $this->validator->validate('bar', $constraint); + + $this->assertNoViolation(); + } + + public static function provideLegacyConstraintsWithCallbackFunctionDoctrineStyle(): iterable { yield 'doctrine style, namespaced function' => [new Choice(['callback' => __NAMESPACE__.'\choice_callback'])]; yield 'doctrine style, closure' => [new Choice([ 'callback' => fn () => ['foo', 'bar'], ])]; yield 'doctrine style, static method' => [new Choice(['callback' => [__CLASS__, 'staticCallback']])]; - yield 'named arguments, namespaced function' => [new Choice(callback: __NAMESPACE__.'\choice_callback')]; - yield 'named arguments, closure' => [new Choice(callback: fn () => ['foo', 'bar'])]; - yield 'named arguments, static method' => [new Choice(callback: [__CLASS__, 'staticCallback'])]; } public function testValidChoiceCallbackContextMethod() @@ -119,7 +144,7 @@ public function testValidChoiceCallbackContextMethod() // search $this for "staticCallback" $this->setObject($this); - $constraint = new Choice(['callback' => 'staticCallback']); + $constraint = new Choice(callback: 'staticCallback'); $this->validator->validate('bar', $constraint); @@ -131,41 +156,39 @@ public function testValidChoiceCallbackContextObjectMethod() // search $this for "objectMethodCallback" $this->setObject($this); - $constraint = new Choice(['callback' => 'objectMethodCallback']); + $constraint = new Choice(callback: 'objectMethodCallback'); $this->validator->validate('bar', $constraint); $this->assertNoViolation(); } - /** - * @dataProvider provideConstraintsWithMultipleTrue - */ - public function testMultipleChoices(Choice $constraint) + public function testMultipleChoices() { - $this->validator->validate(['baz', 'bar'], $constraint); + $this->validator->validate(['baz', 'bar'], new Choice( + choices: ['foo', 'bar', 'baz'], + multiple: true, + )); $this->assertNoViolation(); } - public static function provideConstraintsWithMultipleTrue(): iterable + /** + * @group legacy + */ + public function testMultipleChoicesDoctrineStyle() { - yield 'Doctrine style' => [new Choice([ + $this->validator->validate(['baz', 'bar'], new Choice([ 'choices' => ['foo', 'bar', 'baz'], 'multiple' => true, - ])]; - yield 'named arguments' => [new Choice( - choices: ['foo', 'bar', 'baz'], - multiple: true, - )]; + ])); + + $this->assertNoViolation(); } - /** - * @dataProvider provideConstraintsWithMessage - */ - public function testInvalidChoice(Choice $constraint) + public function testInvalidChoice() { - $this->validator->validate('baz', $constraint); + $this->validator->validate('baz', new Choice(choices: ['foo', 'bar'], message: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"baz"') @@ -174,20 +197,28 @@ public function testInvalidChoice(Choice $constraint) ->assertRaised(); } - public static function provideConstraintsWithMessage(): iterable + /** + * @group legacy + */ + public function testInvalidChoiceDoctrineStyle() { - yield 'Doctrine style' => [new Choice(['choices' => ['foo', 'bar'], 'message' => 'myMessage'])]; - yield 'named arguments' => [new Choice(choices: ['foo', 'bar'], message: 'myMessage')]; + $this->validator->validate('baz', new Choice(['choices' => ['foo', 'bar'], 'message' => 'myMessage'])); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"baz"') + ->setParameter('{{ choices }}', '"foo", "bar"') + ->setCode(Choice::NO_SUCH_CHOICE_ERROR) + ->assertRaised(); } public function testInvalidChoiceEmptyChoices() { - $constraint = new Choice([ + $constraint = new Choice( // May happen when the choices are provided dynamically, e.g. from // the DB or the model - 'choices' => [], - 'message' => 'myMessage', - ]); + choices: [], + message: 'myMessage', + ); $this->validator->validate('baz', $constraint); @@ -198,12 +229,13 @@ public function testInvalidChoiceEmptyChoices() ->assertRaised(); } - /** - * @dataProvider provideConstraintsWithMultipleMessage - */ - public function testInvalidChoiceMultiple(Choice $constraint) + public function testInvalidChoiceMultiple() { - $this->validator->validate(['foo', 'baz'], $constraint); + $this->validator->validate(['foo', 'baz'], new Choice( + choices: ['foo', 'bar'], + multipleMessage: 'myMessage', + multiple: true, + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"baz"') @@ -212,31 +244,37 @@ public function testInvalidChoiceMultiple(Choice $constraint) ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->assertRaised(); } - - public static function provideConstraintsWithMultipleMessage(): iterable + /** + * @group legacy + */ + public function testInvalidChoiceMultipleDoctrineStyle() { - yield 'Doctrine style' => [new Choice([ + $this->validator->validate(['foo', 'baz'], new Choice([ 'choices' => ['foo', 'bar'], 'multipleMessage' => 'myMessage', 'multiple' => true, - ])]; - yield 'named arguments' => [new Choice( - choices: ['foo', 'bar'], - multipleMessage: 'myMessage', - multiple: true, - )]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"baz"') + ->setParameter('{{ choices }}', '"foo", "bar"') + ->setInvalidValue('baz') + ->setCode(Choice::NO_SUCH_CHOICE_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideConstraintsWithMin - */ - public function testTooFewChoices(Choice $constraint) + public function testTooFewChoices() { $value = ['foo']; $this->setValue($value); - $this->validator->validate($value, $constraint); + $this->validator->validate($value, new Choice( + choices: ['foo', 'bar', 'moo', 'maa'], + multiple: true, + min: 2, + minMessage: 'myMessage', + )); $this->buildViolation('myMessage') ->setParameter('{{ limit }}', 2) @@ -246,32 +284,42 @@ public function testTooFewChoices(Choice $constraint) ->assertRaised(); } - public static function provideConstraintsWithMin(): iterable + /** + * @group legacy + */ + public function testTooFewChoicesDoctrineStyle() { - yield 'Doctrine style' => [new Choice([ + $value = ['foo']; + + $this->setValue($value); + + $this->validator->validate($value, new Choice([ 'choices' => ['foo', 'bar', 'moo', 'maa'], 'multiple' => true, 'min' => 2, 'minMessage' => 'myMessage', - ])]; - yield 'named arguments' => [new Choice( - choices: ['foo', 'bar', 'moo', 'maa'], - multiple: true, - min: 2, - minMessage: 'myMessage', - )]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ limit }}', 2) + ->setInvalidValue($value) + ->setPlural(2) + ->setCode(Choice::TOO_FEW_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideConstraintsWithMax - */ - public function testTooManyChoices(Choice $constraint) + public function testTooManyChoices() { $value = ['foo', 'bar', 'moo']; $this->setValue($value); - $this->validator->validate($value, $constraint); + $this->validator->validate($value, new Choice( + choices: ['foo', 'bar', 'moo', 'maa'], + multiple: true, + max: 2, + maxMessage: 'myMessage', + )); $this->buildViolation('myMessage') ->setParameter('{{ limit }}', 2) @@ -281,27 +329,33 @@ public function testTooManyChoices(Choice $constraint) ->assertRaised(); } - public static function provideConstraintsWithMax(): iterable + /** + * @group legacy + */ + public function testTooManyChoicesDoctrineStyle() { - yield 'Doctrine style' => [new Choice([ + $value = ['foo', 'bar', 'moo']; + + $this->setValue($value); + + $this->validator->validate($value, new Choice([ 'choices' => ['foo', 'bar', 'moo', 'maa'], 'multiple' => true, 'max' => 2, 'maxMessage' => 'myMessage', - ])]; - yield 'named arguments' => [new Choice( - choices: ['foo', 'bar', 'moo', 'maa'], - multiple: true, - max: 2, - maxMessage: 'myMessage', - )]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ limit }}', 2) + ->setInvalidValue($value) + ->setPlural(2) + ->setCode(Choice::TOO_MANY_ERROR) + ->assertRaised(); } public function testStrictAllowsExactValue() { - $constraint = new Choice([ - 'choices' => [1, 2], - ]); + $constraint = new Choice(choices: [1, 2]); $this->validator->validate(2, $constraint); @@ -310,10 +364,10 @@ public function testStrictAllowsExactValue() public function testStrictDisallowsDifferentType() { - $constraint = new Choice([ - 'choices' => [1, 2], - 'message' => 'myMessage', - ]); + $constraint = new Choice( + choices: [1, 2], + message: 'myMessage', + ); $this->validator->validate('2', $constraint); @@ -326,11 +380,11 @@ public function testStrictDisallowsDifferentType() public function testStrictWithMultipleChoices() { - $constraint = new Choice([ - 'choices' => [1, 2, 3], - 'multiple' => true, - 'multipleMessage' => 'myMessage', - ]); + $constraint = new Choice( + choices: [1, 2, 3], + multiple: true, + multipleMessage: 'myMessage', + ); $this->validator->validate([2, '3'], $constraint); @@ -344,10 +398,10 @@ public function testStrictWithMultipleChoices() public function testMatchFalse() { - $this->validator->validate('foo', new Choice([ - 'choices' => ['foo', 'bar'], - 'match' => false, - ])); + $this->validator->validate('foo', new Choice( + choices: ['foo', 'bar'], + match: false, + )); $this->buildViolation('The value you selected is not a valid choice.') ->setParameter('{{ value }}', '"foo"') @@ -358,11 +412,11 @@ public function testMatchFalse() public function testMatchFalseWithMultiple() { - $this->validator->validate(['ccc', 'bar', 'zzz'], new Choice([ - 'choices' => ['foo', 'bar'], - 'multiple' => true, - 'match' => false, - ])); + $this->validator->validate(['ccc', 'bar', 'zzz'], new Choice( + choices: ['foo', 'bar'], + multiple: true, + match: false, + )); $this->buildViolation('One or more of the given values is invalid.') ->setParameter('{{ value }}', '"bar"') diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CidrTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CidrTest.php index c2536a7b5d418..f346c6e70e431 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CidrTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CidrTest.php @@ -31,7 +31,7 @@ public function testForAll() public function testForV4() { - $cidrConstraint = new Cidr(['version' => Ip::V4]); + $cidrConstraint = new Cidr(version: Ip::V4); self::assertEquals(Ip::V4, $cidrConstraint->version); self::assertEquals(0, $cidrConstraint->netmaskMin); @@ -40,7 +40,7 @@ public function testForV4() public function testForV6() { - $cidrConstraint = new Cidr(['version' => Ip::V6]); + $cidrConstraint = new Cidr(version: Ip::V6); self::assertEquals(Ip::V6, $cidrConstraint->version); self::assertEquals(0, $cidrConstraint->netmaskMin); @@ -62,7 +62,7 @@ public function testWithInvalidVersion() self::expectException(ConstraintDefinitionException::class); self::expectExceptionMessage(sprintf('The option "version" must be one of "%s".', implode('", "', $availableVersions))); - new Cidr(['version' => '8']); + new Cidr(version: '8'); } /** @@ -70,11 +70,11 @@ public function testWithInvalidVersion() */ public function testWithValidMinMaxValues(string $ipVersion, int $netmaskMin, int $netmaskMax) { - $cidrConstraint = new Cidr([ - 'version' => $ipVersion, - 'netmaskMin' => $netmaskMin, - 'netmaskMax' => $netmaskMax, - ]); + $cidrConstraint = new Cidr( + version: $ipVersion, + netmaskMin: $netmaskMin, + netmaskMax: $netmaskMax, + ); self::assertEquals($ipVersion, $cidrConstraint->version); self::assertEquals($netmaskMin, $cidrConstraint->netmaskMin); @@ -91,11 +91,11 @@ public function testWithInvalidMinMaxValues(string $ipVersion, int $netmaskMin, self::expectException(ConstraintDefinitionException::class); self::expectExceptionMessage(sprintf('The netmask range must be between 0 and %d.', $expectedMax)); - new Cidr([ - 'version' => $ipVersion, - 'netmaskMin' => $netmaskMin, - 'netmaskMax' => $netmaskMax, - ]); + new Cidr( + version: $ipVersion, + netmaskMin: $netmaskMin, + netmaskMax: $netmaskMax, + ); } public static function getInvalidMinMaxValues(): array diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CidrValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CidrValidatorTest.php index 9274d81dad0cc..28c563d9f7cd7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CidrValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CidrValidatorTest.php @@ -86,7 +86,7 @@ public function testInvalidIpValue(string $cidr) */ public function testValidCidr(string|\Stringable $cidr, string $version) { - $this->validator->validate($cidr, new Cidr(['version' => $version])); + $this->validator->validate($cidr, new Cidr(version: $version)); $this->assertNoViolation(); } @@ -108,11 +108,11 @@ public function testInvalidIpAddressAndNetmask(string|\Stringable $cidr) */ public function testOutOfRangeNetmask(string $cidr, ?string $version = null, ?int $min = null, ?int $max = null) { - $cidrConstraint = new Cidr([ - 'version' => $version, - 'netmaskMin' => $min, - 'netmaskMax' => $max, - ]); + $cidrConstraint = new Cidr( + version: $version, + netmaskMin: $min, + netmaskMax: $max, + ); $this->validator->validate($cidr, $cidrConstraint); $this @@ -128,7 +128,7 @@ public function testOutOfRangeNetmask(string $cidr, ?string $version = null, ?in */ public function testWrongVersion(string $cidr, string $version) { - $this->validator->validate($cidr, new Cidr(['version' => $version])); + $this->validator->validate($cidr, new Cidr(version: $version)); $this ->buildViolation('This value is not a valid CIDR notation.') diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php index a2c606154ca62..9157bbe3314af 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php @@ -25,6 +25,9 @@ */ class CollectionTest extends TestCase { + /** + * @group legacy + */ public function testRejectNonConstraints() { $this->expectException(InvalidOptionsException::class); @@ -60,17 +63,13 @@ public function testRejectValidConstraintWithinRequired() public function testAcceptOptionalConstraintAsOneElementArray() { $collection1 = new Collection([ - 'fields' => [ - 'alternate_email' => [ - new Optional(new Email()), - ], + 'alternate_email' => [ + new Optional(new Email()), ], ]); $collection2 = new Collection([ - 'fields' => [ - 'alternate_email' => new Optional(new Email()), - ], + 'alternate_email' => new Optional(new Email()), ]); $this->assertEquals($collection1, $collection2); @@ -79,17 +78,13 @@ public function testAcceptOptionalConstraintAsOneElementArray() public function testAcceptRequiredConstraintAsOneElementArray() { $collection1 = new Collection([ - 'fields' => [ - 'alternate_email' => [ - new Required(new Email()), - ], + 'alternate_email' => [ + new Required(new Email()), ], ]); $collection2 = new Collection([ - 'fields' => [ - 'alternate_email' => new Required(new Email()), - ], + 'alternate_email' => new Required(new Email()), ]); $this->assertEquals($collection1, $collection2); @@ -107,6 +102,9 @@ public function testConstraintHasDefaultGroupWithOptionalValues() $this->assertEquals(['Default'], $constraint->fields['bar']->groups); } + /** + * @group legacy + */ public function testOnlySomeKeysAreKnowOptions() { $constraint = new Collection([ @@ -125,15 +123,15 @@ public function testOnlySomeKeysAreKnowOptions() public function testAllKeysAreKnowOptions() { - $constraint = new Collection([ - 'fields' => [ + $constraint = new Collection( + fields: [ 'fields' => [new Required()], 'properties' => [new Required()], 'catalog' => [new Optional()], ], - 'allowExtraFields' => true, - 'extraFieldsMessage' => 'foo bar baz', - ]); + allowExtraFields: true, + extraFieldsMessage: 'foo bar baz', + ); $this->assertArrayHasKey('fields', $constraint->fields); $this->assertInstanceOf(Required::class, $constraint->fields['fields']); @@ -156,11 +154,11 @@ public function testEmptyFields() public function testEmptyFieldsInOptions() { - $constraint = new Collection([ - 'fields' => [], - 'allowExtraFields' => true, - 'extraFieldsMessage' => 'foo bar baz', - ]); + $constraint = new Collection( + fields: [], + allowExtraFields: true, + extraFieldsMessage: 'foo bar baz', + ); $this->assertSame([], $constraint->fields); $this->assertTrue($constraint->allowExtraFields); @@ -196,13 +194,13 @@ public function testEmptyConstraintListForField(?array $fieldConstraint) */ public function testEmptyConstraintListForFieldInOptions(?array $fieldConstraint) { - $constraint = new Collection([ - 'fields' => [ + $constraint = new Collection( + fields: [ 'foo' => $fieldConstraint, ], - 'allowExtraFields' => true, - 'extraFieldsMessage' => 'foo bar baz', - ]); + allowExtraFields: true, + extraFieldsMessage: 'foo bar baz', + ); $this->assertArrayHasKey('foo', $constraint->fields); $this->assertInstanceOf(Required::class, $constraint->fields['foo']); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTestCase.php index 92260e96693da..8e03a9add9e77 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTestCase.php @@ -31,16 +31,16 @@ abstract protected function prepareTestData(array $contents); public function testNullIsValid() { - $this->validator->validate(null, new Collection(['fields' => [ - 'foo' => new Range(['min' => 4]), - ]])); + $this->validator->validate(null, new Collection(fields: [ + 'foo' => new Range(min: 4), + ])); $this->assertNoViolation(); } public function testFieldsAsDefaultOption() { - $constraint = new Range(['min' => 4]); + $constraint = new Range(min: 4); $data = $this->prepareTestData(['foo' => 'foobar']); @@ -56,14 +56,14 @@ public function testFieldsAsDefaultOption() public function testThrowsExceptionIfNotTraversable() { $this->expectException(UnexpectedValueException::class); - $this->validator->validate('foobar', new Collection(['fields' => [ - 'foo' => new Range(['min' => 4]), - ]])); + $this->validator->validate('foobar', new Collection(fields: [ + 'foo' => new Range(min: 4), + ])); } public function testWalkSingleConstraint() { - $constraint = new Range(['min' => 4]); + $constraint = new Range(min: 4); $array = [ 'foo' => 3, @@ -78,12 +78,12 @@ public function testWalkSingleConstraint() $data = $this->prepareTestData($array); - $this->validator->validate($data, new Collection([ - 'fields' => [ + $this->validator->validate($data, new Collection( + fields: [ 'foo' => $constraint, 'bar' => $constraint, ], - ])); + )); $this->assertNoViolation(); } @@ -91,7 +91,7 @@ public function testWalkSingleConstraint() public function testWalkMultipleConstraints() { $constraints = [ - new Range(['min' => 4]), + new Range(min: 4), new NotNull(), ]; @@ -108,19 +108,19 @@ public function testWalkMultipleConstraints() $data = $this->prepareTestData($array); - $this->validator->validate($data, new Collection([ - 'fields' => [ + $this->validator->validate($data, new Collection( + fields: [ 'foo' => $constraints, 'bar' => $constraints, ], - ])); + )); $this->assertNoViolation(); } public function testExtraFieldsDisallowed() { - $constraint = new Range(['min' => 4]); + $constraint = new Range(min: 4); $data = $this->prepareTestData([ 'foo' => 5, @@ -129,12 +129,12 @@ public function testExtraFieldsDisallowed() $this->expectValidateValueAt(0, '[foo]', $data['foo'], [$constraint]); - $this->validator->validate($data, new Collection([ - 'fields' => [ + $this->validator->validate($data, new Collection( + fields: [ 'foo' => $constraint, ], - 'extraFieldsMessage' => 'myMessage', - ])); + extraFieldsMessage: 'myMessage', + )); $this->buildViolation('myMessage') ->setParameter('{{ field }}', '"baz"') @@ -152,12 +152,12 @@ public function testExtraFieldsDisallowedWithOptionalValues() 'baz' => 6, ]); - $this->validator->validate($data, new Collection([ - 'fields' => [ + $this->validator->validate($data, new Collection( + fields: [ 'foo' => $constraint, ], - 'extraFieldsMessage' => 'myMessage', - ])); + extraFieldsMessage: 'myMessage', + )); $this->buildViolation('myMessage') ->setParameter('{{ field }}', '"baz"') @@ -174,15 +174,15 @@ public function testNullNotConsideredExtraField() 'foo' => null, ]); - $constraint = new Range(['min' => 4]); + $constraint = new Range(min: 4); $this->expectValidateValueAt(0, '[foo]', $data['foo'], [$constraint]); - $this->validator->validate($data, new Collection([ - 'fields' => [ + $this->validator->validate($data, new Collection( + fields: [ 'foo' => $constraint, ], - ])); + )); $this->assertNoViolation(); } @@ -194,16 +194,16 @@ public function testExtraFieldsAllowed() 'bar' => 6, ]); - $constraint = new Range(['min' => 4]); + $constraint = new Range(min: 4); $this->expectValidateValueAt(0, '[foo]', $data['foo'], [$constraint]); - $this->validator->validate($data, new Collection([ - 'fields' => [ + $this->validator->validate($data, new Collection( + fields: [ 'foo' => $constraint, ], - 'allowExtraFields' => true, - ])); + allowExtraFields: true, + )); $this->assertNoViolation(); } @@ -212,14 +212,14 @@ public function testMissingFieldsDisallowed() { $data = $this->prepareTestData([]); - $constraint = new Range(['min' => 4]); + $constraint = new Range(min: 4); - $this->validator->validate($data, new Collection([ - 'fields' => [ + $this->validator->validate($data, new Collection( + fields: [ 'foo' => $constraint, ], - 'missingFieldsMessage' => 'myMessage', - ])); + missingFieldsMessage: 'myMessage', + )); $this->buildViolation('myMessage') ->setParameter('{{ field }}', '"foo"') @@ -233,14 +233,14 @@ public function testMissingFieldsAllowed() { $data = $this->prepareTestData([]); - $constraint = new Range(['min' => 4]); + $constraint = new Range(min: 4); - $this->validator->validate($data, new Collection([ - 'fields' => [ + $this->validator->validate($data, new Collection( + fields: [ 'foo' => $constraint, ], - 'allowMissingFields' => true, - ])); + allowMissingFields: true, + )); $this->assertNoViolation(); } @@ -275,7 +275,7 @@ public function testOptionalFieldSingleConstraint() 'foo' => 5, ]; - $constraint = new Range(['min' => 4]); + $constraint = new Range(min: 4); $this->expectValidateValueAt(0, '[foo]', $array['foo'], [$constraint]); @@ -296,7 +296,7 @@ public function testOptionalFieldMultipleConstraints() $constraints = [ new NotNull(), - new Range(['min' => 4]), + new Range(min: 4), ]; $this->expectValidateValueAt(0, '[foo]', $array['foo'], $constraints); @@ -327,12 +327,12 @@ public function testRequiredFieldNotPresent() { $data = $this->prepareTestData([]); - $this->validator->validate($data, new Collection([ - 'fields' => [ + $this->validator->validate($data, new Collection( + fields: [ 'foo' => new Required(), ], - 'missingFieldsMessage' => 'myMessage', - ])); + missingFieldsMessage: 'myMessage', + )); $this->buildViolation('myMessage') ->setParameter('{{ field }}', '"foo"') @@ -348,7 +348,7 @@ public function testRequiredFieldSingleConstraint() 'foo' => 5, ]; - $constraint = new Range(['min' => 4]); + $constraint = new Range(min: 4); $this->expectValidateValueAt(0, '[foo]', $array['foo'], [$constraint]); @@ -369,7 +369,7 @@ public function testRequiredFieldMultipleConstraints() $constraints = [ new NotNull(), - new Range(['min' => 4]), + new Range(min: 4), ]; $this->expectValidateValueAt(0, '[foo]', $array['foo'], $constraints); @@ -389,15 +389,15 @@ public function testObjectShouldBeLeftUnchanged() 'foo' => 3, ]); - $constraint = new Range(['min' => 2]); + $constraint = new Range(min: 2); $this->expectValidateValueAt(0, '[foo]', $value['foo'], [$constraint]); - $this->validator->validate($value, new Collection([ - 'fields' => [ + $this->validator->validate($value, new Collection( + fields: [ 'foo' => $constraint, ], - ])); + )); $this->assertEquals([ 'foo' => 3, diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CompositeTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CompositeTest.php index 127ad21dd224d..a769a68e40809 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CompositeTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CompositeTest.php @@ -66,8 +66,8 @@ public function testNestedCompositeConstraintHasDefaultGroup() public function testMergeNestedGroupsIfNoExplicitParentGroup() { $constraint = new ConcreteComposite([ - new NotNull(['groups' => 'Default']), - new NotBlank(['groups' => ['Default', 'Strict']]), + new NotNull(groups: ['Default']), + new NotBlank(groups: ['Default', 'Strict']), ]); $this->assertEquals(['Default', 'Strict'], $constraint->groups); @@ -94,8 +94,8 @@ public function testExplicitNestedGroupsMustBeSubsetOfExplicitParentGroups() { $constraint = new ConcreteComposite([ 'constraints' => [ - new NotNull(['groups' => 'Default']), - new NotBlank(['groups' => 'Strict']), + new NotNull(groups: ['Default']), + new NotBlank(groups: ['Strict']), ], 'groups' => ['Default', 'Strict'], ]); @@ -110,7 +110,7 @@ public function testFailIfExplicitNestedGroupsNotSubsetOfExplicitParentGroups() $this->expectException(ConstraintDefinitionException::class); new ConcreteComposite([ 'constraints' => [ - new NotNull(['groups' => ['Default', 'Foobar']]), + new NotNull(groups: ['Default', 'Foobar']), ], 'groups' => ['Default', 'Strict'], ]); @@ -119,8 +119,8 @@ public function testFailIfExplicitNestedGroupsNotSubsetOfExplicitParentGroups() public function testImplicitGroupNamesAreForwarded() { $constraint = new ConcreteComposite([ - new NotNull(['groups' => 'Default']), - new NotBlank(['groups' => 'Strict']), + new NotNull(groups: ['Default']), + new NotBlank(groups: ['Strict']), ]); $constraint->addImplicitGroupName('ImplicitGroup'); @@ -142,7 +142,7 @@ public function testFailIfNoConstraint() { $this->expectException(ConstraintDefinitionException::class); new ConcreteComposite([ - new NotNull(['groups' => 'Default']), + new NotNull(groups: ['Default']), 'NotBlank', ]); } @@ -151,7 +151,7 @@ public function testFailIfNoConstraintObject() { $this->expectException(ConstraintDefinitionException::class); new ConcreteComposite([ - new NotNull(['groups' => 'Default']), + new NotNull(groups: ['Default']), new \ArrayObject(), ]); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php index d5cf9e9ceecbb..cc5a822f4879b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php @@ -19,6 +19,9 @@ class CompoundTest extends TestCase { + /** + * @group legacy + */ public function testItCannotRedefineConstraintsOption() { $this->expectException(ConstraintDefinitionException::class); @@ -54,7 +57,7 @@ public function getDefaultOption(): ?string protected function getConstraints(array $options): array { return [ - new Length(['min' => $options['min'] ?? null]), + new Length(min: $options['min'] ?? null), ]; } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CompoundValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CompoundValidatorTest.php index 2f48657b21fc5..e48ba9c3c9e8d 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CompoundValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CompoundValidatorTest.php @@ -50,7 +50,7 @@ protected function getConstraints(array $options): array { return [ new NotBlank(), - new Length(['max' => 3]), + new Length(max: 3), ]; } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php index c52cd4e69d394..f60199027396d 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php @@ -70,6 +70,7 @@ public static function getFiveOrMoreElements() } /** + * @group legacy * @dataProvider getThreeOrLessElements */ public function testValidValuesMax($value) @@ -92,6 +93,7 @@ public function testValidValuesMaxNamed($value) } /** + * @group legacy * @dataProvider getFiveOrMoreElements */ public function testValidValuesMin($value) @@ -114,6 +116,7 @@ public function testValidValuesMinNamed($value) } /** + * @group legacy * @dataProvider getFourElements */ public function testValidValuesExact($value) @@ -136,6 +139,7 @@ public function testValidValuesExactNamed($value) } /** + * @group legacy * @dataProvider getFiveOrMoreElements */ public function testTooManyValues($value) @@ -175,6 +179,7 @@ public function testTooManyValuesNamed($value) } /** + * @group legacy * @dataProvider getThreeOrLessElements */ public function testTooFewValues($value) @@ -214,6 +219,7 @@ public function testTooFewValuesNamed($value) } /** + * @group legacy * @dataProvider getFiveOrMoreElements */ public function testTooManyValuesExact($value) @@ -258,11 +264,11 @@ public function testTooManyValuesExactNamed($value) */ public function testTooFewValuesExact($value) { - $constraint = new Count([ - 'min' => 4, - 'max' => 4, - 'exactMessage' => 'myMessage', - ]); + $constraint = new Count( + min: 4, + max: 4, + exactMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -285,7 +291,7 @@ public function testDefaultOption() public function testConstraintAttributeDefaultOption() { - $constraint = new Count(['value' => 5, 'exactMessage' => 'message']); + $constraint = new Count(exactly: 5, exactMessage: 'message'); $this->assertEquals(5, $constraint->min); $this->assertEquals(5, $constraint->max); @@ -296,15 +302,15 @@ public function testConstraintAttributeDefaultOption() // is called with the right DivisibleBy constraint. public function testDivisibleBy() { - $constraint = new Count([ - 'divisibleBy' => 123, - 'divisibleByMessage' => 'foo {{ compared_value }}', - ]); - - $this->expectValidateValue(0, 3, [new DivisibleBy([ - 'value' => 123, - 'message' => 'foo {{ compared_value }}', - ])], $this->group); + $constraint = new Count( + divisibleBy: 123, + divisibleByMessage: 'foo {{ compared_value }}', + ); + + $this->expectValidateValue(0, 3, [new DivisibleBy( + value: 123, + message: 'foo {{ compared_value }}', + )], $this->group); $this->validator->validate(['foo', 'bar', 'ccc'], $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php index 524d0bc540d2b..e535ce4f506a5 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php @@ -84,9 +84,7 @@ public static function getValidCountries() */ public function testInvalidCountries($country) { - $constraint = new Country([ - 'message' => 'myMessage', - ]); + $constraint = new Country(message: 'myMessage'); $this->validator->validate($country, $constraint); @@ -109,9 +107,7 @@ public static function getInvalidCountries() */ public function testValidAlpha3Countries($country) { - $this->validator->validate($country, new Country([ - 'alpha3' => true, - ])); + $this->validator->validate($country, new Country(alpha3: true)); $this->assertNoViolation(); } @@ -130,10 +126,10 @@ public static function getValidAlpha3Countries() */ public function testInvalidAlpha3Countries($country) { - $constraint = new Country([ - 'alpha3' => true, - 'message' => 'myMessage', - ]); + $constraint = new Country( + alpha3: true, + message: 'myMessage', + ); $this->validator->validate($country, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php index a0e16ec145fb4..51def4a2aec91 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php @@ -100,9 +100,7 @@ public static function getValidCurrencies() */ public function testInvalidCurrencies($currency) { - $constraint = new Currency([ - 'message' => 'myMessage', - ]); + $constraint = new Currency(message: 'myMessage'); $this->validator->validate($currency, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php index 8da07c424f40e..f5d0f62229360 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php @@ -62,9 +62,7 @@ public function testDateTimeWithDefaultFormat() */ public function testValidDateTimes($format, $dateTime) { - $constraint = new DateTime([ - 'format' => $format, - ]); + $constraint = new DateTime(format: $format); $this->validator->validate($dateTime, $constraint); @@ -87,10 +85,10 @@ public static function getValidDateTimes() */ public function testInvalidDateTimes($format, $dateTime, $code) { - $constraint = new DateTime([ - 'message' => 'myMessage', - 'format' => $format, - ]); + $constraint = new DateTime( + message: 'myMessage', + format: $format, + ); $this->validator->validate($dateTime, $constraint); @@ -130,9 +128,7 @@ public function testInvalidDateTimeNamed() public function testDateTimeWithTrailingData() { - $this->validator->validate('1995-05-10 00:00:00', new DateTime([ - 'format' => 'Y-m-d+', - ])); + $this->validator->validate('1995-05-10 00:00:00', new DateTime(format: 'Y-m-d+')); $this->assertNoViolation(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php index e8642f04a8ec1..464d2771f0fe1 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php @@ -67,9 +67,7 @@ public static function getValidDates() */ public function testInvalidDates($date, $code) { - $constraint = new Date([ - 'message' => 'myMessage', - ]); + $constraint = new Date(message: 'myMessage'); $this->validator->validate($date, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DisableAutoMappingTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DisableAutoMappingTest.php index e47bc6aeb839f..135250ac6adfe 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DisableAutoMappingTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DisableAutoMappingTest.php @@ -23,6 +23,9 @@ */ class DisableAutoMappingTest extends TestCase { + /** + * @group legacy + */ public function testGroups() { $this->expectException(ConstraintDefinitionException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php index 30950d94b6532..d29663fbdf756 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php @@ -28,7 +28,11 @@ protected function createValidator(): DivisibleByValidator protected static function createConstraint(?array $options = null): Constraint { - return new DivisibleBy($options); + if (null !== $options) { + return new DivisibleBy(...$options); + } + + return new DivisibleBy(); } protected function getErrorCode(): ?string diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EmailTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EmailTest.php index b6f710ffc7ad2..1fd5a6ca14e2b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EmailTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EmailTest.php @@ -21,14 +21,14 @@ class EmailTest extends TestCase { public function testConstructorStrict() { - $subject = new Email(['mode' => Email::VALIDATION_MODE_STRICT]); + $subject = new Email(mode: Email::VALIDATION_MODE_STRICT); $this->assertEquals(Email::VALIDATION_MODE_STRICT, $subject->mode); } public function testConstructorHtml5AllowNoTld() { - $subject = new Email(['mode' => Email::VALIDATION_MODE_HTML5_ALLOW_NO_TLD]); + $subject = new Email(mode: Email::VALIDATION_MODE_HTML5_ALLOW_NO_TLD); $this->assertEquals(Email::VALIDATION_MODE_HTML5_ALLOW_NO_TLD, $subject->mode); } @@ -37,16 +37,19 @@ public function testUnknownModesTriggerException() { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('The "mode" parameter value is not valid.'); - new Email(['mode' => 'Unknown Mode']); + new Email(mode: 'Unknown Mode'); } public function testNormalizerCanBeSet() { - $email = new Email(['normalizer' => 'trim']); + $email = new Email(normalizer: 'trim'); $this->assertEquals('trim', $email->normalizer); } + /** + * @group legacy + */ public function testInvalidNormalizerThrowsException() { $this->expectException(InvalidArgumentException::class); @@ -54,6 +57,9 @@ public function testInvalidNormalizerThrowsException() new Email(['normalizer' => 'Unknown Callable']); } + /** + * @group legacy + */ public function testInvalidNormalizerObjectThrowsException() { $this->expectException(InvalidArgumentException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php index ab424a82d25d6..cafaee391b77e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php @@ -84,7 +84,7 @@ public static function getValidEmails() */ public function testValidNormalizedEmails($email) { - $this->validator->validate($email, new Email(['normalizer' => 'trim'])); + $this->validator->validate($email, new Email(normalizer: 'trim')); $this->assertNoViolation(); } @@ -102,7 +102,7 @@ public static function getValidEmailsWithWhitespaces() */ public function testValidEmailsHtml5($email) { - $this->validator->validate($email, new Email(['mode' => Email::VALIDATION_MODE_HTML5])); + $this->validator->validate($email, new Email(mode: Email::VALIDATION_MODE_HTML5)); $this->assertNoViolation(); } @@ -122,9 +122,7 @@ public static function getValidEmailsHtml5() */ public function testInvalidEmails($email) { - $constraint = new Email([ - 'message' => 'myMessage', - ]); + $constraint = new Email(message: 'myMessage'); $this->validator->validate($email, $constraint); @@ -149,10 +147,10 @@ public static function getInvalidEmails() */ public function testInvalidHtml5Emails($email) { - $constraint = new Email([ - 'message' => 'myMessage', - 'mode' => Email::VALIDATION_MODE_HTML5, - ]); + $constraint = new Email( + message: 'myMessage', + mode: Email::VALIDATION_MODE_HTML5, + ); $this->validator->validate($email, $constraint); @@ -189,10 +187,10 @@ public static function getInvalidHtml5Emails() */ public function testInvalidAllowNoTldEmails($email) { - $constraint = new Email([ - 'message' => 'myMessage', - 'mode' => Email::VALIDATION_MODE_HTML5_ALLOW_NO_TLD, - ]); + $constraint = new Email( + message: 'myMessage', + mode: Email::VALIDATION_MODE_HTML5_ALLOW_NO_TLD, + ); $this->validator->validate($email, $constraint); @@ -215,7 +213,7 @@ public static function getInvalidAllowNoTldEmails() public function testModeStrict() { - $constraint = new Email(['mode' => Email::VALIDATION_MODE_STRICT]); + $constraint = new Email(mode: Email::VALIDATION_MODE_STRICT); $this->validator->validate('example@mywebsite.tld', $constraint); @@ -224,7 +222,7 @@ public function testModeStrict() public function testModeHtml5() { - $constraint = new Email(['mode' => Email::VALIDATION_MODE_HTML5]); + $constraint = new Email(mode: Email::VALIDATION_MODE_HTML5); $this->validator->validate('example@example..com', $constraint); @@ -236,7 +234,7 @@ public function testModeHtml5() public function testModeHtml5AllowNoTld() { - $constraint = new Email(['mode' => Email::VALIDATION_MODE_HTML5_ALLOW_NO_TLD]); + $constraint = new Email(mode: Email::VALIDATION_MODE_HTML5_ALLOW_NO_TLD); $this->validator->validate('example@example', $constraint); @@ -259,10 +257,10 @@ public function testUnknownModesOnValidateTriggerException() */ public function testStrictWithInvalidEmails($email) { - $constraint = new Email([ - 'message' => 'myMessage', - 'mode' => Email::VALIDATION_MODE_STRICT, - ]); + $constraint = new Email( + message: 'myMessage', + mode: Email::VALIDATION_MODE_STRICT, + ); $this->validator->validate($email, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EnableAutoMappingTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EnableAutoMappingTest.php index 2a5a944d31a6f..ce50f27397df4 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EnableAutoMappingTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EnableAutoMappingTest.php @@ -23,6 +23,9 @@ */ class EnableAutoMappingTest extends TestCase { + /** + * @group legacy + */ public function testGroups() { $this->expectException(ConstraintDefinitionException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php index 0a57fa8d34058..5ec31d690fbb4 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php @@ -30,7 +30,11 @@ protected function createValidator(): EqualToValidator protected static function createConstraint(?array $options = null): Constraint { - return new EqualTo($options); + if (null !== $options) { + return new EqualTo(...$options); + } + + return new EqualTo(); } protected function getErrorCode(): ?string diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionSyntaxTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionSyntaxTest.php index 3f77cace2c2ee..8731a5d850ec7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionSyntaxTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionSyntaxTest.php @@ -36,8 +36,6 @@ public function testValidatedByService(ExpressionSyntax $constraint) public static function provideServiceValidatedConstraints(): iterable { - yield 'Doctrine style' => [new ExpressionSyntax(['service' => 'my_service'])]; - yield 'named arguments' => [new ExpressionSyntax(service: 'my_service')]; $metadata = new ClassMetadata(ExpressionSyntaxDummy::class); @@ -46,6 +44,16 @@ public static function provideServiceValidatedConstraints(): iterable yield 'attribute' => [$metadata->properties['b']->constraints[0]]; } + /** + * @group legacy + */ + public function testValidatedByServiceDoctrineStyle() + { + $constraint = new ExpressionSyntax(['service' => 'my_service']); + + self::assertSame('my_service', $constraint->validatedBy()); + } + public function testAttributes() { $metadata = new ClassMetadata(ExpressionSyntaxDummy::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionSyntaxValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionSyntaxValidatorTest.php index 65be7fb2a85aa..3ca4e655b16e5 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionSyntaxValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionSyntaxValidatorTest.php @@ -40,49 +40,47 @@ public function testEmptyStringIsValid() public function testExpressionValid() { - $this->validator->validate('1 + 1', new ExpressionSyntax([ - 'message' => 'myMessage', - 'allowedVariables' => [], - ])); + $this->validator->validate('1 + 1', new ExpressionSyntax( + message: 'myMessage', + allowedVariables: [], + )); $this->assertNoViolation(); } public function testStringableExpressionValid() { - $this->validator->validate(new StringableValue('1 + 1'), new ExpressionSyntax([ - 'message' => 'myMessage', - 'allowedVariables' => [], - ])); + $this->validator->validate(new StringableValue('1 + 1'), new ExpressionSyntax( + message: 'myMessage', + allowedVariables: [], + )); $this->assertNoViolation(); } public function testExpressionWithoutNames() { - $this->validator->validate('1 + 1', new ExpressionSyntax([ - 'message' => 'myMessage', - ], null, null, [])); + $this->validator->validate('1 + 1', new ExpressionSyntax(null, 'myMessage', null, [])); $this->assertNoViolation(); } public function testExpressionWithAllowedVariableName() { - $this->validator->validate('a + 1', new ExpressionSyntax([ - 'message' => 'myMessage', - 'allowedVariables' => ['a'], - ])); + $this->validator->validate('a + 1', new ExpressionSyntax( + message: 'myMessage', + allowedVariables: ['a'], + )); $this->assertNoViolation(); } public function testExpressionIsNotValid() { - $this->validator->validate('a + 1', new ExpressionSyntax([ - 'message' => 'myMessage', - 'allowedVariables' => [], - ])); + $this->validator->validate('a + 1', new ExpressionSyntax( + message: 'myMessage', + allowedVariables: [], + )); $this->buildViolation('myMessage') ->setParameter('{{ syntax_error }}', '"Variable "a" is not valid around position 1 for expression `a + 1`."') @@ -93,10 +91,10 @@ public function testExpressionIsNotValid() public function testStringableExpressionIsNotValid() { - $this->validator->validate(new StringableValue('a + 1'), new ExpressionSyntax([ - 'message' => 'myMessage', - 'allowedVariables' => [], - ])); + $this->validator->validate(new StringableValue('a + 1'), new ExpressionSyntax( + message: 'myMessage', + allowedVariables: [], + )); $this->buildViolation('myMessage') ->setParameter('{{ syntax_error }}', '"Variable "a" is not valid around position 1 for expression `a + 1`."') diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php index c237c793f0cbc..21c9eb630bce3 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php @@ -31,10 +31,10 @@ protected function createValidator(): ExpressionValidator public function testExpressionIsEvaluatedWithNullValue() { - $constraint = new Expression([ - 'expression' => 'false', - 'message' => 'myMessage', - ]); + $constraint = new Expression( + expression: 'false', + message: 'myMessage', + ); $this->validator->validate(null, $constraint); @@ -46,10 +46,10 @@ public function testExpressionIsEvaluatedWithNullValue() public function testExpressionIsEvaluatedWithEmptyStringValue() { - $constraint = new Expression([ - 'expression' => 'false', - 'message' => 'myMessage', - ]); + $constraint = new Expression( + expression: 'false', + message: 'myMessage', + ); $this->validator->validate('', $constraint); @@ -75,10 +75,10 @@ public function testSucceedingExpressionAtObjectLevel() public function testFailingExpressionAtObjectLevel() { - $constraint = new Expression([ - 'expression' => 'this.data == 1', - 'message' => 'myMessage', - ]); + $constraint = new Expression( + expression: 'this.data == 1', + message: 'myMessage', + ); $object = new Entity(); $object->data = '2'; @@ -109,10 +109,10 @@ public function testSucceedingExpressionAtObjectLevelWithToString() public function testFailingExpressionAtObjectLevelWithToString() { - $constraint = new Expression([ - 'expression' => 'this.data == 1', - 'message' => 'myMessage', - ]); + $constraint = new Expression( + expression: 'this.data == 1', + message: 'myMessage', + ); $object = new ToString(); $object->data = '2'; @@ -145,10 +145,10 @@ public function testSucceedingExpressionAtPropertyLevel() public function testFailingExpressionAtPropertyLevel() { - $constraint = new Expression([ - 'expression' => 'value == this.data', - 'message' => 'myMessage', - ]); + $constraint = new Expression( + expression: 'value == this.data', + message: 'myMessage', + ); $object = new Entity(); $object->data = '1'; @@ -187,10 +187,10 @@ public function testSucceedingExpressionAtNestedPropertyLevel() public function testFailingExpressionAtNestedPropertyLevel() { - $constraint = new Expression([ - 'expression' => 'value == this.data', - 'message' => 'myMessage', - ]); + $constraint = new Expression( + expression: 'value == this.data', + message: 'myMessage', + ); $object = new Entity(); $object->data = '1'; @@ -234,10 +234,10 @@ public function testSucceedingExpressionAtPropertyLevelWithoutRoot() */ public function testFailingExpressionAtPropertyLevelWithoutRoot() { - $constraint = new Expression([ - 'expression' => 'value == "1"', - 'message' => 'myMessage', - ]); + $constraint = new Expression( + expression: 'value == "1"', + message: 'myMessage', + ); $this->setRoot('2'); $this->setPropertyPath(''); @@ -254,9 +254,7 @@ public function testFailingExpressionAtPropertyLevelWithoutRoot() public function testExpressionLanguageUsage() { - $constraint = new Expression([ - 'expression' => 'false', - ]); + $constraint = new Expression(expression: 'false'); $expressionLanguage = $this->createMock(ExpressionLanguage::class); @@ -278,12 +276,12 @@ public function testExpressionLanguageUsage() public function testPassingCustomValues() { - $constraint = new Expression([ - 'expression' => 'value + custom == 2', - 'values' => [ + $constraint = new Expression( + expression: 'value + custom == 2', + values: [ 'custom' => 1, ], - ]); + ); $this->validator->validate(1, $constraint); @@ -292,13 +290,13 @@ public function testPassingCustomValues() public function testViolationOnPass() { - $constraint = new Expression([ - 'expression' => 'value + custom != 2', - 'values' => [ + $constraint = new Expression( + expression: 'value + custom != 2', + values: [ 'custom' => 1, ], - 'negate' => false, - ]); + negate: false, + ); $this->validator->validate(2, $constraint); @@ -311,10 +309,11 @@ public function testViolationOnPass() public function testIsValidExpression() { - $constraints = [new NotNull(), new Range(['min' => 2])]; + $constraints = [new NotNull(), new Range(min: 2)]; $constraint = new Expression( - ['expression' => 'is_valid(this.data, a)', 'values' => ['a' => $constraints]] + expression: 'is_valid(this.data, a)', + values: ['a' => $constraints], ); $object = new Entity(); @@ -331,10 +330,11 @@ public function testIsValidExpression() public function testIsValidExpressionInvalid() { - $constraints = [new Range(['min' => 2, 'max' => 5])]; + $constraints = [new Range(min: 2, max: 5)]; $constraint = new Expression( - ['expression' => 'is_valid(this.data, a)', 'values' => ['a' => $constraints]] + expression: 'is_valid(this.data, a)', + values: ['a' => $constraints], ); $object = new Entity(); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileTest.php index e8c27b4b1f290..e4e30a5816446 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileTest.php @@ -24,7 +24,7 @@ class FileTest extends TestCase */ public function testMaxSize($maxSize, $bytes, $binaryFormat) { - $file = new File(['maxSize' => $maxSize]); + $file = new File(maxSize: $maxSize); $this->assertSame($bytes, $file->maxSize); $this->assertSame($binaryFormat, $file->binaryFormat); @@ -33,7 +33,7 @@ public function testMaxSize($maxSize, $bytes, $binaryFormat) public function testMagicIsset() { - $file = new File(['maxSize' => 1]); + $file = new File(maxSize: 1); $this->assertTrue($file->__isset('maxSize')); $this->assertTrue($file->__isset('groups')); @@ -57,7 +57,7 @@ public function testMaxSizeCanBeSetAfterInitialization($maxSize, $bytes, $binary */ public function testInvalidValueForMaxSizeThrowsExceptionAfterInitialization($maxSize) { - $file = new File(['maxSize' => 1000]); + $file = new File(maxSize: 1000); $this->expectException(ConstraintDefinitionException::class); @@ -69,7 +69,7 @@ public function testInvalidValueForMaxSizeThrowsExceptionAfterInitialization($ma */ public function testMaxSizeCannotBeSetToInvalidValueAfterInitialization($maxSize) { - $file = new File(['maxSize' => 1000]); + $file = new File(maxSize: 1000); try { $file->maxSize = $maxSize; @@ -85,7 +85,7 @@ public function testMaxSizeCannotBeSetToInvalidValueAfterInitialization($maxSize public function testInvalidMaxSize($maxSize) { $this->expectException(ConstraintDefinitionException::class); - new File(['maxSize' => $maxSize]); + new File(maxSize: $maxSize); } public static function provideValidSizes() @@ -125,7 +125,7 @@ public static function provideInvalidSizes() */ public function testBinaryFormat($maxSize, $guessedFormat, $binaryFormat) { - $file = new File(['maxSize' => $maxSize, 'binaryFormat' => $guessedFormat]); + $file = new File(maxSize: $maxSize, binaryFormat: $guessedFormat); $this->assertSame($binaryFormat, $file->binaryFormat); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php index 9a89688016de3..37557aa1aa8fa 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorPathTest.php @@ -22,9 +22,9 @@ protected function getFile($filename) public function testFileNotFound() { - $constraint = new File([ - 'notFoundMessage' => 'myMessage', - ]); + $constraint = new File( + notFoundMessage: 'myMessage', + ); $this->validator->validate('foobar', $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTestCase.php index b5d05801e53fe..81e833b275828 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTestCase.php @@ -168,10 +168,10 @@ public function testMaxSizeExceeded($bytesWritten, $limit, $sizeAsString, $limit fwrite($this->file, '0'); fclose($this->file); - $constraint = new File([ - 'maxSize' => $limit, - 'maxSizeMessage' => 'myMessage', - ]); + $constraint = new File( + maxSize: $limit, + maxSizeMessage: 'myMessage', + ); $this->validator->validate($this->getFile($this->path), $constraint); @@ -220,10 +220,10 @@ public function testMaxSizeNotExceeded($bytesWritten, $limit) fwrite($this->file, '0'); fclose($this->file); - $constraint = new File([ - 'maxSize' => $limit, - 'maxSizeMessage' => 'myMessage', - ]); + $constraint = new File( + maxSize: $limit, + maxSizeMessage: 'myMessage', + ); $this->validator->validate($this->getFile($this->path), $constraint); @@ -233,9 +233,7 @@ public function testMaxSizeNotExceeded($bytesWritten, $limit) public function testInvalidMaxSize() { $this->expectException(ConstraintDefinitionException::class); - new File([ - 'maxSize' => '1abc', - ]); + new File(maxSize: '1abc'); } public static function provideBinaryFormatTests() @@ -269,11 +267,11 @@ public function testBinaryFormat($bytesWritten, $limit, $binaryFormat, $sizeAsSt fwrite($this->file, '0'); fclose($this->file); - $constraint = new File([ - 'maxSize' => $limit, - 'binaryFormat' => $binaryFormat, - 'maxSizeMessage' => 'myMessage', - ]); + $constraint = new File( + maxSize: $limit, + binaryFormat: $binaryFormat, + maxSizeMessage: 'myMessage', + ); $this->validator->validate($this->getFile($this->path), $constraint); @@ -322,9 +320,7 @@ public function testValidMimeType() ->method('getMimeType') ->willReturn('image/jpg'); - $constraint = new File([ - 'mimeTypes' => ['image/png', 'image/jpg'], - ]); + $constraint = new File(mimeTypes: ['image/png', 'image/jpg']); $this->validator->validate($file, $constraint); @@ -346,19 +342,14 @@ public function testValidWildcardMimeType() ->method('getMimeType') ->willReturn('image/jpg'); - $constraint = new File([ - 'mimeTypes' => ['image/*'], - ]); + $constraint = new File(mimeTypes: ['image/*']); $this->validator->validate($file, $constraint); $this->assertNoViolation(); } - /** - * @dataProvider provideMimeTypeConstraints - */ - public function testInvalidMimeType(File $constraint) + public function testInvalidMimeType() { $file = $this ->getMockBuilder(\Symfony\Component\HttpFoundation\File\File::class) @@ -373,7 +364,7 @@ public function testInvalidMimeType(File $constraint) ->method('getMimeType') ->willReturn('application/pdf'); - $this->validator->validate($file, $constraint); + $this->validator->validate($file, new File(mimeTypes: ['image/png', 'image/jpg'], mimeTypesMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ type }}', '"application/pdf"') @@ -384,15 +375,36 @@ public function testInvalidMimeType(File $constraint) ->assertRaised(); } - public static function provideMimeTypeConstraints(): iterable + /** + * @group legacy + */ + public function testInvalidMimeTypeDoctrineStyle() { - yield 'Doctrine style' => [new File([ + $file = $this + ->getMockBuilder(\Symfony\Component\HttpFoundation\File\File::class) + ->setConstructorArgs([__DIR__.'/Fixtures/foo']) + ->getMock(); + $file + ->expects($this->once()) + ->method('getPathname') + ->willReturn($this->path); + $file + ->expects($this->once()) + ->method('getMimeType') + ->willReturn('application/pdf'); + + $this->validator->validate($file, new File([ 'mimeTypes' => ['image/png', 'image/jpg'], 'mimeTypesMessage' => 'myMessage', - ])]; - yield 'named arguments' => [ - new File(mimeTypes: ['image/png', 'image/jpg'], mimeTypesMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ type }}', '"application/pdf"') + ->setParameter('{{ types }}', '"image/png", "image/jpg"') + ->setParameter('{{ file }}', '"'.$this->path.'"') + ->setParameter('{{ name }}', '"'.basename($this->path).'"') + ->setCode(File::INVALID_MIME_TYPE_ERROR) + ->assertRaised(); } public function testInvalidWildcardMimeType() @@ -410,10 +422,10 @@ public function testInvalidWildcardMimeType() ->method('getMimeType') ->willReturn('application/pdf'); - $constraint = new File([ - 'mimeTypes' => ['image/*', 'image/jpg'], - 'mimeTypesMessage' => 'myMessage', - ]); + $constraint = new File( + mimeTypes: ['image/*', 'image/jpg'], + mimeTypesMessage: 'myMessage', + ); $this->validator->validate($file, $constraint); @@ -426,14 +438,11 @@ public function testInvalidWildcardMimeType() ->assertRaised(); } - /** - * @dataProvider provideDisallowEmptyConstraints - */ - public function testDisallowEmpty(File $constraint) + public function testDisallowEmpty() { ftruncate($this->file, 0); - $this->validator->validate($this->getFile($this->path), $constraint); + $this->validator->validate($this->getFile($this->path), new File(disallowEmptyMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ file }}', '"'.$this->path.'"') @@ -442,14 +451,22 @@ public function testDisallowEmpty(File $constraint) ->assertRaised(); } - public static function provideDisallowEmptyConstraints(): iterable + /** + * @group legacy + */ + public function testDisallowEmptyDoctrineStyle() { - yield 'Doctrine style' => [new File([ + ftruncate($this->file, 0); + + $this->validator->validate($this->getFile($this->path), new File([ 'disallowEmptyMessage' => 'myMessage', - ])]; - yield 'named arguments' => [ - new File(disallowEmptyMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ file }}', '"'.$this->path.'"') + ->setParameter('{{ name }}', '"'.basename($this->path).'"') + ->setCode(File::EMPTY_ERROR) + ->assertRaised(); } /** @@ -459,7 +476,7 @@ public function testUploadedFileError($error, $message, array $params = [], $max { $file = new UploadedFile(tempnam(sys_get_temp_dir(), 'file-validator-test-'), 'originalName', 'mime', $error); - $constraint = new File([ + $constraint = new File(...[ $message => 'myMessage', 'maxSize' => $maxSize, ]); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php index aeaf46f566d61..1bc2f76ca62a2 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php @@ -30,7 +30,11 @@ protected function createValidator(): GreaterThanOrEqualValidator protected static function createConstraint(?array $options = null): Constraint { - return new GreaterThanOrEqual($options); + if (null !== $options) { + return new GreaterThanOrEqual(...$options); + } + + return new GreaterThanOrEqual(); } protected function getErrorCode(): ?string diff --git a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest.php b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest.php index e21f6b8e909e3..f414d4a7b5168 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest.php @@ -48,6 +48,9 @@ public static function provideInvalidComparisons(): array ]; } + /** + * @group legacy + */ public function testThrowsConstraintExceptionIfPropertyPath() { $this->expectException(ConstraintDefinitionException::class); @@ -56,6 +59,9 @@ public function testThrowsConstraintExceptionIfPropertyPath() return new PositiveOrZero(['propertyPath' => 'field']); } + /** + * @group legacy + */ public function testThrowsConstraintExceptionIfValue() { $this->expectException(ConstraintDefinitionException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php index 709e01c26a59e..8ac96d8604787 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php @@ -30,7 +30,11 @@ protected function createValidator(): GreaterThanValidator protected static function createConstraint(?array $options = null): Constraint { - return new GreaterThan($options); + if (null !== $options) { + return new GreaterThan(...$options); + } + + return new GreaterThan(); } protected function getErrorCode(): ?string diff --git a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorWithPositiveConstraintTest.php b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorWithPositiveConstraintTest.php index 0b5f2e2228750..57caae5988971 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorWithPositiveConstraintTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorWithPositiveConstraintTest.php @@ -46,6 +46,9 @@ public static function provideInvalidComparisons(): array ]; } + /** + * @group legacy + */ public function testThrowsConstraintExceptionIfPropertyPath() { $this->expectException(ConstraintDefinitionException::class); @@ -54,6 +57,9 @@ public function testThrowsConstraintExceptionIfPropertyPath() return new Positive(['propertyPath' => 'field']); } + /** + * @group legacy + */ public function testThrowsConstraintExceptionIfValue() { $this->expectException(ConstraintDefinitionException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/HostnameValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/HostnameValidatorTest.php index 0af802b6318cf..552f09f6786ed 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/HostnameValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/HostnameValidatorTest.php @@ -57,7 +57,7 @@ public function testValidTldDomainsPassValidationIfTldRequired($domain) */ public function testValidTldDomainsPassValidationIfTldNotRequired($domain) { - $this->validator->validate($domain, new Hostname(['requireTld' => false])); + $this->validator->validate($domain, new Hostname(requireTld: false)); $this->assertNoViolation(); } @@ -81,9 +81,7 @@ public static function getValidMultilevelDomains() */ public function testInvalidDomainsRaiseViolationIfTldRequired($domain) { - $this->validator->validate($domain, new Hostname([ - 'message' => 'myMessage', - ])); + $this->validator->validate($domain, new Hostname(message: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"'.$domain.'"') @@ -96,10 +94,10 @@ public function testInvalidDomainsRaiseViolationIfTldRequired($domain) */ public function testInvalidDomainsRaiseViolationIfTldNotRequired($domain) { - $this->validator->validate($domain, new Hostname([ - 'message' => 'myMessage', - 'requireTld' => false, - ])); + $this->validator->validate($domain, new Hostname( + message: 'myMessage', + requireTld: false, + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"'.$domain.'"') @@ -123,7 +121,7 @@ public static function getInvalidDomains() */ public function testReservedDomainsPassValidationIfTldNotRequired($domain) { - $this->validator->validate($domain, new Hostname(['requireTld' => false])); + $this->validator->validate($domain, new Hostname(requireTld: false)); $this->assertNoViolation(); } @@ -133,10 +131,10 @@ public function testReservedDomainsPassValidationIfTldNotRequired($domain) */ public function testReservedDomainsRaiseViolationIfTldRequired($domain) { - $this->validator->validate($domain, new Hostname([ - 'message' => 'myMessage', - 'requireTld' => true, - ])); + $this->validator->validate($domain, new Hostname( + message: 'myMessage', + requireTld: true, + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"'.$domain.'"') @@ -176,7 +174,7 @@ public function testReservedDomainsRaiseViolationIfTldRequiredNamed() */ public function testTopLevelDomainsPassValidationIfTldNotRequired($domain) { - $this->validator->validate($domain, new Hostname(['requireTld' => false])); + $this->validator->validate($domain, new Hostname(requireTld: false)); $this->assertNoViolation(); } @@ -186,10 +184,10 @@ public function testTopLevelDomainsPassValidationIfTldNotRequired($domain) */ public function testTopLevelDomainsRaiseViolationIfTldRequired($domain) { - $this->validator->validate($domain, new Hostname([ - 'message' => 'myMessage', - 'requireTld' => true, - ])); + $this->validator->validate($domain, new Hostname( + message: 'myMessage', + requireTld: true, + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"'.$domain.'"') diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php index 212d5329f44af..ee8b09903bc29 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php @@ -462,9 +462,7 @@ public static function getIbansWithInvalidCountryCode() private function assertViolationRaised($iban, $code) { - $constraint = new Iban([ - 'message' => 'myMessage', - ]); + $constraint = new Iban(message: 'myMessage'); $this->validator->validate($iban, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php index 2855e83f0b50c..2d920e96ee68e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php @@ -30,7 +30,11 @@ protected function createValidator(): IdenticalToValidator protected static function createConstraint(?array $options = null): Constraint { - return new IdenticalTo($options); + if (null !== $options) { + return new IdenticalTo(...$options); + } + + return new IdenticalTo(); } protected function getErrorCode(): ?string diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php index 6523173a1fabc..b5ea43911c0ed 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php @@ -72,12 +72,10 @@ public function testValidImage() /** * Checks that the logic from FileValidator still works. - * - * @dataProvider provideConstraintsWithNotFoundMessage */ - public function testFileNotFound(Image $constraint) + public function testFileNotFound() { - $this->validator->validate('foobar', $constraint); + $this->validator->validate('foobar', new Image(notFoundMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ file }}', '"foobar"') @@ -85,36 +83,40 @@ public function testFileNotFound(Image $constraint) ->assertRaised(); } - public static function provideConstraintsWithNotFoundMessage(): iterable + /** + * Checks that the logic from FileValidator still works. + * + * @group legacy + */ + public function testFileNotFoundDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate('foobar', new Image([ 'notFoundMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(notFoundMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ file }}', '"foobar"') + ->setCode(Image::NOT_FOUND_ERROR) + ->assertRaised(); } public function testValidSize() { - $constraint = new Image([ - 'minWidth' => 1, - 'maxWidth' => 2, - 'minHeight' => 1, - 'maxHeight' => 2, - ]); + $constraint = new Image( + minWidth: 1, + maxWidth: 2, + minHeight: 1, + maxHeight: 2, + ); $this->validator->validate($this->image, $constraint); $this->assertNoViolation(); } - /** - * @dataProvider provideMinWidthConstraints - */ - public function testWidthTooSmall(Image $constraint) + public function testWidthTooSmall() { - $this->validator->validate($this->image, $constraint); + $this->validator->validate($this->image, new Image(minWidth: 3, minWidthMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ width }}', '2') @@ -123,23 +125,26 @@ public function testWidthTooSmall(Image $constraint) ->assertRaised(); } - public static function provideMinWidthConstraints(): iterable + /** + * @group legacy + */ + public function testWidthTooSmallDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->image, new Image([ 'minWidth' => 3, 'minWidthMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(minWidth: 3, minWidthMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ width }}', '2') + ->setParameter('{{ min_width }}', '3') + ->setCode(Image::TOO_NARROW_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideMaxWidthConstraints - */ - public function testWidthTooBig(Image $constraint) + public function testWidthTooBig() { - $this->validator->validate($this->image, $constraint); + $this->validator->validate($this->image, new Image(maxWidth: 1, maxWidthMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ width }}', '2') @@ -148,23 +153,26 @@ public function testWidthTooBig(Image $constraint) ->assertRaised(); } - public static function provideMaxWidthConstraints(): iterable + /** + * @group legacy + */ + public function testWidthTooBigDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->image, new Image([ 'maxWidth' => 1, 'maxWidthMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(maxWidth: 1, maxWidthMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ width }}', '2') + ->setParameter('{{ max_width }}', '1') + ->setCode(Image::TOO_WIDE_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideMinHeightConstraints - */ - public function testHeightTooSmall(Image $constraint) + public function testHeightTooSmall() { - $this->validator->validate($this->image, $constraint); + $this->validator->validate($this->image, new Image(minHeight: 3, minHeightMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ height }}', '2') @@ -173,23 +181,26 @@ public function testHeightTooSmall(Image $constraint) ->assertRaised(); } - public static function provideMinHeightConstraints(): iterable + /** + * @group legacy + */ + public function testHeightTooSmallDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->image, new Image([ 'minHeight' => 3, 'minHeightMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(minHeight: 3, minHeightMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ height }}', '2') + ->setParameter('{{ min_height }}', '3') + ->setCode(Image::TOO_LOW_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideMaxHeightConstraints - */ - public function testHeightTooBig(Image $constraint) + public function testHeightTooBig() { - $this->validator->validate($this->image, $constraint); + $this->validator->validate($this->image, new Image(maxHeight: 1, maxHeightMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ height }}', '2') @@ -198,23 +209,26 @@ public function testHeightTooBig(Image $constraint) ->assertRaised(); } - public static function provideMaxHeightConstraints(): iterable + /** + * @group legacy + */ + public function testHeightTooBigDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->image, new Image([ 'maxHeight' => 1, 'maxHeightMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(maxHeight: 1, maxHeightMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ height }}', '2') + ->setParameter('{{ max_height }}', '1') + ->setCode(Image::TOO_HIGH_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideMinPixelsConstraints - */ - public function testPixelsTooFew(Image $constraint) + public function testPixelsTooFew() { - $this->validator->validate($this->image, $constraint); + $this->validator->validate($this->image, new Image(minPixels: 5, minPixelsMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ pixels }}', '4') @@ -225,23 +239,28 @@ public function testPixelsTooFew(Image $constraint) ->assertRaised(); } - public static function provideMinPixelsConstraints(): iterable + /** + * @group legacy + */ + public function testPixelsTooFewDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->image, new Image([ 'minPixels' => 5, 'minPixelsMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(minPixels: 5, minPixelsMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ pixels }}', '4') + ->setParameter('{{ min_pixels }}', '5') + ->setParameter('{{ height }}', '2') + ->setParameter('{{ width }}', '2') + ->setCode(Image::TOO_FEW_PIXEL_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideMaxPixelsConstraints - */ - public function testPixelsTooMany(Image $constraint) + public function testPixelsTooMany() { - $this->validator->validate($this->image, $constraint); + $this->validator->validate($this->image, new Image(maxPixels: 3, maxPixelsMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ pixels }}', '4') @@ -252,23 +271,28 @@ public function testPixelsTooMany(Image $constraint) ->assertRaised(); } - public static function provideMaxPixelsConstraints(): iterable + /** + * @group legacy + */ + public function testPixelsTooManyDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->image, new Image([ 'maxPixels' => 3, 'maxPixelsMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(maxPixels: 3, maxPixelsMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ pixels }}', '4') + ->setParameter('{{ max_pixels }}', '3') + ->setParameter('{{ height }}', '2') + ->setParameter('{{ width }}', '2') + ->setCode(Image::TOO_MANY_PIXEL_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideMinRatioConstraints - */ - public function testRatioTooSmall(Image $constraint) + public function testRatioTooSmall() { - $this->validator->validate($this->image, $constraint); + $this->validator->validate($this->image, new Image(minRatio: 2, minRatioMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ ratio }}', 1) @@ -277,23 +301,26 @@ public function testRatioTooSmall(Image $constraint) ->assertRaised(); } - public static function provideMinRatioConstraints(): iterable + /** + * @group legacy + */ + public function testRatioTooSmallDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->image, new Image([ 'minRatio' => 2, 'minRatioMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(minRatio: 2, minRatioMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ ratio }}', 1) + ->setParameter('{{ min_ratio }}', 2) + ->setCode(Image::RATIO_TOO_SMALL_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideMaxRatioConstraints - */ - public function testRatioTooBig(Image $constraint) + public function testRatioTooBig() { - $this->validator->validate($this->image, $constraint); + $this->validator->validate($this->image, new Image(maxRatio: 0.5, maxRatioMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ ratio }}', 1) @@ -302,22 +329,26 @@ public function testRatioTooBig(Image $constraint) ->assertRaised(); } - public static function provideMaxRatioConstraints(): iterable + /** + * @group legacy + */ + public function testRatioTooBigDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->image, new Image([ 'maxRatio' => 0.5, 'maxRatioMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(maxRatio: 0.5, maxRatioMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ ratio }}', 1) + ->setParameter('{{ max_ratio }}', 0.5) + ->setCode(Image::RATIO_TOO_BIG_ERROR) + ->assertRaised(); } public function testMaxRatioUsesTwoDecimalsOnly() { - $constraint = new Image([ - 'maxRatio' => 1.33, - ]); + $constraint = new Image(maxRatio: 1.33); $this->validator->validate($this->image4By3, $constraint); @@ -326,9 +357,7 @@ public function testMaxRatioUsesTwoDecimalsOnly() public function testMinRatioUsesInputMoreDecimals() { - $constraint = new Image([ - 'minRatio' => 4 / 3, - ]); + $constraint = new Image(minRatio: 4 / 3); $this->validator->validate($this->image4By3, $constraint); @@ -337,21 +366,16 @@ public function testMinRatioUsesInputMoreDecimals() public function testMaxRatioUsesInputMoreDecimals() { - $constraint = new Image([ - 'maxRatio' => 16 / 9, - ]); + $constraint = new Image(maxRatio: 16 / 9); $this->validator->validate($this->image16By9, $constraint); $this->assertNoViolation(); } - /** - * @dataProvider provideAllowSquareConstraints - */ - public function testSquareNotAllowed(Image $constraint) + public function testSquareNotAllowed() { - $this->validator->validate($this->image, $constraint); + $this->validator->validate($this->image, new Image(allowSquare: false, allowSquareMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ width }}', 2) @@ -360,23 +384,26 @@ public function testSquareNotAllowed(Image $constraint) ->assertRaised(); } - public static function provideAllowSquareConstraints(): iterable + /** + * @group legacy + */ + public function testSquareNotAllowedDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->image, new Image([ 'allowSquare' => false, 'allowSquareMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(allowSquare: false, allowSquareMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ width }}', 2) + ->setParameter('{{ height }}', 2) + ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideAllowLandscapeConstraints - */ - public function testLandscapeNotAllowed(Image $constraint) + public function testLandscapeNotAllowed() { - $this->validator->validate($this->imageLandscape, $constraint); + $this->validator->validate($this->imageLandscape, new Image(allowLandscape: false, allowLandscapeMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ width }}', 2) @@ -385,23 +412,26 @@ public function testLandscapeNotAllowed(Image $constraint) ->assertRaised(); } - public static function provideAllowLandscapeConstraints(): iterable + /** + * @group legacy + */ + public function testLandscapeNotAllowedDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->imageLandscape, new Image([ 'allowLandscape' => false, 'allowLandscapeMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(allowLandscape: false, allowLandscapeMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ width }}', 2) + ->setParameter('{{ height }}', 1) + ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR) + ->assertRaised(); } - /** - * @dataProvider provideAllowPortraitConstraints - */ - public function testPortraitNotAllowed(Image $constraint) + public function testPortraitNotAllowed() { - $this->validator->validate($this->imagePortrait, $constraint); + $this->validator->validate($this->imagePortrait, new Image(allowPortrait: false, allowPortraitMessage: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ width }}', 1) @@ -410,26 +440,56 @@ public function testPortraitNotAllowed(Image $constraint) ->assertRaised(); } - public static function provideAllowPortraitConstraints(): iterable + /** + * @group legacy + */ + public function testPortraitNotAllowedDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->imagePortrait, new Image([ 'allowPortrait' => false, 'allowPortraitMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(allowPortrait: false, allowPortraitMessage: 'myMessage'), - ]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ width }}', 1) + ->setParameter('{{ height }}', 2) + ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR) + ->assertRaised(); + } + + public function testCorrupted() + { + if (!\function_exists('imagecreatefromstring')) { + $this->markTestSkipped('This test require GD extension'); + } + + $constraint = new Image(detectCorrupted: true, corruptedMessage: 'myMessage'); + + $this->validator->validate($this->image, $constraint); + + $this->assertNoViolation(); + + $this->validator->validate($this->imageCorrupted, $constraint); + + $this->buildViolation('myMessage') + ->setCode(Image::CORRUPTED_IMAGE_ERROR) + ->assertRaised(); } /** - * @dataProvider provideDetectCorruptedConstraints + * @group legacy */ - public function testCorrupted(Image $constraint) + public function testCorruptedDoctrineStyle() { if (!\function_exists('imagecreatefromstring')) { $this->markTestSkipped('This test require GD extension'); } + $constraint = new Image([ + 'detectCorrupted' => true, + 'corruptedMessage' => 'myMessage', + ]); + $this->validator->validate($this->image, $constraint); $this->assertNoViolation(); @@ -456,23 +516,12 @@ public function testInvalidMimeType() ->assertRaised(); } - public static function provideDetectCorruptedConstraints(): iterable + public function testInvalidMimeTypeWithNarrowedSet() { - yield 'Doctrine style' => [new Image([ - 'detectCorrupted' => true, - 'corruptedMessage' => 'myMessage', - ])]; - yield 'Named arguments' => [ - new Image(detectCorrupted: true, corruptedMessage: 'myMessage'), - ]; - } - - /** - * @dataProvider provideInvalidMimeTypeWithNarrowedSet - */ - public function testInvalidMimeTypeWithNarrowedSet(Image $constraint) - { - $this->validator->validate($this->image, $constraint); + $this->validator->validate($this->image, new Image(mimeTypes: [ + 'image/jpeg', + 'image/png', + ])); $this->buildViolation('The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.') ->setParameter('{{ file }}', sprintf('"%s"', $this->image)) @@ -483,19 +532,24 @@ public function testInvalidMimeTypeWithNarrowedSet(Image $constraint) ->assertRaised(); } - public static function provideInvalidMimeTypeWithNarrowedSet() + /** + * @group legacy + */ + public function testInvalidMimeTypeWithNarrowedSetDoctrineStyle() { - yield 'Doctrine style' => [new Image([ + $this->validator->validate($this->image, new Image([ 'mimeTypes' => [ 'image/jpeg', 'image/png', ], - ])]; - yield 'Named arguments' => [ - new Image(mimeTypes: [ - 'image/jpeg', - 'image/png', - ]), - ]; + ])); + + $this->buildViolation('The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.') + ->setParameter('{{ file }}', sprintf('"%s"', $this->image)) + ->setParameter('{{ type }}', '"image/gif"') + ->setParameter('{{ types }}', '"image/jpeg", "image/png"') + ->setParameter('{{ name }}', '"test.gif"') + ->setCode(Image::INVALID_MIME_TYPE_ERROR) + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IpTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IpTest.php index 7f391153f3a69..2d740ae88a03a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IpTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IpTest.php @@ -24,11 +24,14 @@ class IpTest extends TestCase { public function testNormalizerCanBeSet() { - $ip = new Ip(['normalizer' => 'trim']); + $ip = new Ip(normalizer: 'trim'); $this->assertEquals('trim', $ip->normalizer); } + /** + * @group legacy + */ public function testInvalidNormalizerThrowsException() { $this->expectException(InvalidArgumentException::class); @@ -36,6 +39,9 @@ public function testInvalidNormalizerThrowsException() new Ip(['normalizer' => 'Unknown Callable']); } + /** + * @group legacy + */ public function testInvalidNormalizerObjectThrowsException() { $this->expectException(InvalidArgumentException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php index a2277a3d8ff86..e37d61bb61b7c 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php @@ -47,9 +47,7 @@ public function testExpectsStringCompatibleType() public function testInvalidValidatorVersion() { $this->expectException(ConstraintDefinitionException::class); - new Ip([ - 'version' => 666, - ]); + new Ip(version: 666); } /** @@ -57,9 +55,7 @@ public function testInvalidValidatorVersion() */ public function testValidIpsV4($ip) { - $this->validator->validate($ip, new Ip([ - 'version' => Ip::V4, - ])); + $this->validator->validate($ip, new Ip(version: Ip::V4)); $this->assertNoViolation(); } @@ -83,10 +79,10 @@ public static function getValidIpsV4() */ public function testValidIpsV4WithWhitespaces($ip) { - $this->validator->validate($ip, new Ip([ - 'version' => Ip::V4, - 'normalizer' => 'trim', - ])); + $this->validator->validate($ip, new Ip( + version: Ip::V4, + normalizer: 'trim', + )); $this->assertNoViolation(); } @@ -118,9 +114,7 @@ public static function getValidIpsV4WithWhitespaces() */ public function testValidIpsV6($ip) { - $this->validator->validate($ip, new Ip([ - 'version' => Ip::V6, - ])); + $this->validator->validate($ip, new Ip(version: Ip::V6)); $this->assertNoViolation(); } @@ -155,9 +149,7 @@ public static function getValidIpsV6() */ public function testValidIpsAll($ip) { - $this->validator->validate($ip, new Ip([ - 'version' => Ip::ALL, - ])); + $this->validator->validate($ip, new Ip(version: Ip::ALL)); $this->assertNoViolation(); } @@ -172,10 +164,10 @@ public static function getValidIpsAll() */ public function testInvalidIpsV4($ip) { - $constraint = new Ip([ - 'version' => Ip::V4, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V4, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -190,10 +182,10 @@ public function testInvalidIpsV4($ip) */ public function testInvalidNoPublicIpsV4($ip) { - $constraint = new Ip([ - 'version' => Ip::V4_NO_PUBLIC, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V4_NO_PUBLIC, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -232,9 +224,7 @@ public static function getInvalidIpsV4() */ public function testValidPrivateIpsV4($ip) { - $this->validator->validate($ip, new Ip([ - 'version' => Ip::V4_ONLY_PRIVATE, - ])); + $this->validator->validate($ip, new Ip(version: Ip::V4_ONLY_PRIVATE)); $this->assertNoViolation(); } @@ -244,10 +234,10 @@ public function testValidPrivateIpsV4($ip) */ public function testInvalidPrivateIpsV4($ip) { - $constraint = new Ip([ - 'version' => Ip::V4_NO_PRIVATE, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V4_NO_PRIVATE, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -262,10 +252,10 @@ public function testInvalidPrivateIpsV4($ip) */ public function testInvalidOnlyPrivateIpsV4($ip) { - $constraint = new Ip([ - 'version' => Ip::V4_ONLY_PRIVATE, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V4_ONLY_PRIVATE, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -294,9 +284,7 @@ public static function getInvalidPrivateIpsV4() */ public function testValidReservedIpsV4($ip) { - $this->validator->validate($ip, new Ip([ - 'version' => Ip::V4_ONLY_RESERVED, - ])); + $this->validator->validate($ip, new Ip(version: Ip::V4_ONLY_RESERVED)); $this->assertNoViolation(); } @@ -306,10 +294,10 @@ public function testValidReservedIpsV4($ip) */ public function testInvalidReservedIpsV4($ip) { - $constraint = new Ip([ - 'version' => Ip::V4_NO_RESERVED, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V4_NO_RESERVED, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -324,10 +312,10 @@ public function testInvalidReservedIpsV4($ip) */ public function testInvalidOnlyReservedIpsV4($ip) { - $constraint = new Ip([ - 'version' => Ip::V4_ONLY_RESERVED, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V4_ONLY_RESERVED, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -356,10 +344,10 @@ public static function getInvalidReservedIpsV4() */ public function testInvalidPublicIpsV4($ip) { - $constraint = new Ip([ - 'version' => Ip::V4_ONLY_PUBLIC, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V4_ONLY_PUBLIC, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -379,10 +367,10 @@ public static function getInvalidPublicIpsV4() */ public function testInvalidIpsV6($ip) { - $constraint = new Ip([ - 'version' => Ip::V6, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V6, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -416,10 +404,10 @@ public static function getInvalidIpsV6() */ public function testInvalidPrivateIpsV6($ip) { - $constraint = new Ip([ - 'version' => Ip::V6_NO_PRIVATE, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V6_NO_PRIVATE, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -443,10 +431,10 @@ public static function getInvalidPrivateIpsV6() */ public function testInvalidReservedIpsV6($ip) { - $constraint = new Ip([ - 'version' => Ip::V6_NO_RESERVED, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V6_NO_RESERVED, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -469,10 +457,10 @@ public static function getInvalidReservedIpsV6() */ public function testInvalidPublicIpsV6($ip) { - $constraint = new Ip([ - 'version' => Ip::V6_ONLY_PUBLIC, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::V6_ONLY_PUBLIC, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -492,10 +480,10 @@ public static function getInvalidPublicIpsV6() */ public function testInvalidIpsAll($ip) { - $constraint = new Ip([ - 'version' => Ip::ALL, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::ALL, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -515,10 +503,10 @@ public static function getInvalidIpsAll() */ public function testInvalidPrivateIpsAll($ip) { - $constraint = new Ip([ - 'version' => Ip::ALL_NO_PRIVATE, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::ALL_NO_PRIVATE, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -538,10 +526,10 @@ public static function getInvalidPrivateIpsAll() */ public function testInvalidReservedIpsAll($ip) { - $constraint = new Ip([ - 'version' => Ip::ALL_NO_RESERVED, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::ALL_NO_RESERVED, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); @@ -561,10 +549,10 @@ public static function getInvalidReservedIpsAll() */ public function testInvalidPublicIpsAll($ip) { - $constraint = new Ip([ - 'version' => Ip::ALL_ONLY_PUBLIC, - 'message' => 'myMessage', - ]); + $constraint = new Ip( + version: Ip::ALL_ONLY_PUBLIC, + message: 'myMessage', + ); $this->validator->validate($ip, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IsFalseValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsFalseValidatorTest.php index e597647640d5a..c6e2ccef6e8c3 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IsFalseValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsFalseValidatorTest.php @@ -36,12 +36,9 @@ public function testFalseIsValid() $this->assertNoViolation(); } - /** - * @dataProvider provideInvalidConstraints - */ - public function testTrueIsInvalid(IsFalse $constraint) + public function testTrueIsInvalid() { - $this->validator->validate(true, $constraint); + $this->validator->validate(true, new IsFalse(message: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ value }}', 'true') @@ -49,11 +46,20 @@ public function testTrueIsInvalid(IsFalse $constraint) ->assertRaised(); } - public static function provideInvalidConstraints(): iterable + /** + * @group legacy + */ + public function testTrueIsInvalidDoctrineStyle() { - yield 'Doctrine style' => [new IsFalse([ + $constraint = new IsFalse([ 'message' => 'myMessage', - ])]; - yield 'named parameters' => [new IsFalse(message: 'myMessage')]; + ]); + + $this->validator->validate(true, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 'true') + ->setCode(IsFalse::NOT_FALSE_ERROR) + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IsNullValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsNullValidatorTest.php index f0ff58f3420e3..ed6beffc4901e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IsNullValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsNullValidatorTest.php @@ -34,9 +34,7 @@ public function testNullIsValid() */ public function testInvalidValues($value, $valueAsString) { - $constraint = new IsNull([ - 'message' => 'myMessage', - ]); + $constraint = new IsNull(message: 'myMessage'); $this->validator->validate($value, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IsTrueValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsTrueValidatorTest.php index 1dc47f4b0f9b1..4a9eb7702b385 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IsTrueValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsTrueValidatorTest.php @@ -36,12 +36,9 @@ public function testTrueIsValid() $this->assertNoViolation(); } - /** - * @dataProvider provideInvalidConstraints - */ - public function testFalseIsInvalid(IsTrue $constraint) + public function testFalseIsInvalid() { - $this->validator->validate(false, $constraint); + $this->validator->validate(false, new IsTrue(message: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ value }}', 'false') @@ -49,11 +46,18 @@ public function testFalseIsInvalid(IsTrue $constraint) ->assertRaised(); } - public static function provideInvalidConstraints(): iterable + /** + * @group legacy + */ + public function testFalseIsInvalidDoctrineStyle() { - yield 'Doctrine style' => [new IsTrue([ + $this->validator->validate(false, new IsTrue([ 'message' => 'myMessage', - ])]; - yield 'named parameters' => [new IsTrue(message: 'myMessage')]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 'false') + ->setCode(IsTrue::NOT_TRUE_ERROR) + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php index df985137c7845..b0f1a983126ab 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php @@ -150,9 +150,7 @@ public function testExpectsStringCompatibleType() */ public function testValidIsbn10($isbn) { - $constraint = new Isbn([ - 'type' => 'isbn10', - ]); + $constraint = new Isbn('isbn10'); $this->validator->validate($isbn, $constraint); @@ -160,6 +158,7 @@ public function testValidIsbn10($isbn) } /** + * @group legacy * @dataProvider getInvalidIsbn10 */ public function testInvalidIsbn10($isbn, $code) @@ -195,7 +194,7 @@ public function testInvalidIsbn10Named() */ public function testValidIsbn13($isbn) { - $constraint = new Isbn(['type' => 'isbn13']); + $constraint = new Isbn('isbn13'); $this->validator->validate($isbn, $constraint); @@ -203,6 +202,7 @@ public function testValidIsbn13($isbn) } /** + * @group legacy * @dataProvider getInvalidIsbn13 */ public function testInvalidIsbn13($isbn, $code) @@ -220,16 +220,21 @@ public function testInvalidIsbn13($isbn, $code) ->assertRaised(); } - public function testInvalidIsbn13Named() + /** + * @dataProvider getInvalidIsbn13 + */ + public function testInvalidIsbn13Named($isbn, $code) { - $this->validator->validate( - '2723442284', - new Isbn(type: Isbn::ISBN_13, isbn13Message: 'myMessage') + $constraint = new Isbn( + type: Isbn::ISBN_13, + isbn13Message: 'myMessage', ); + $this->validator->validate($isbn, $constraint); + $this->buildViolation('myMessage') - ->setParameter('{{ value }}', '"2723442284"') - ->setCode(Isbn::TOO_SHORT_ERROR) + ->setParameter('{{ value }}', '"'.$isbn.'"') + ->setCode($code) ->assertRaised(); } @@ -250,9 +255,7 @@ public function testValidIsbnAny($isbn) */ public function testInvalidIsbnAnyIsbn10($isbn, $code) { - $constraint = new Isbn([ - 'bothIsbnMessage' => 'myMessage', - ]); + $constraint = new Isbn(bothIsbnMessage: 'myMessage'); $this->validator->validate($isbn, $constraint); @@ -272,9 +275,7 @@ public function testInvalidIsbnAnyIsbn10($isbn, $code) */ public function testInvalidIsbnAnyIsbn13($isbn, $code) { - $constraint = new Isbn([ - 'bothIsbnMessage' => 'myMessage', - ]); + $constraint = new Isbn(bothIsbnMessage: 'myMessage'); $this->validator->validate($isbn, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IsinValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsinValidatorTest.php index dca4a423f8a47..b1ac3be20ddd1 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IsinValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsinValidatorTest.php @@ -130,9 +130,7 @@ public static function getIsinWithValidFormatButIncorrectChecksum() private function assertViolationRaised($isin, $code) { - $constraint = new Isin([ - 'message' => 'myMessage', - ]); + $constraint = new Isin(message: 'myMessage'); $this->validator->validate($isin, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php index 9eece3eb9ce21..6351ab6209381 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php @@ -119,10 +119,10 @@ public function testExpectsStringCompatibleType() */ public function testCaseSensitiveIssns($issn) { - $constraint = new Issn([ - 'caseSensitive' => true, - 'message' => 'myMessage', - ]); + $constraint = new Issn( + caseSensitive: true, + message: 'myMessage', + ); $this->validator->validate($issn, $constraint); @@ -137,10 +137,10 @@ public function testCaseSensitiveIssns($issn) */ public function testRequireHyphenIssns($issn) { - $constraint = new Issn([ - 'requireHyphen' => true, - 'message' => 'myMessage', - ]); + $constraint = new Issn( + requireHyphen: true, + message: 'myMessage', + ); $this->validator->validate($issn, $constraint); @@ -167,9 +167,7 @@ public function testValidIssn($issn) */ public function testInvalidIssn($issn, $code) { - $constraint = new Issn([ - 'message' => 'myMessage', - ]); + $constraint = new Issn(message: 'myMessage'); $this->validator->validate($issn, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/JsonValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/JsonValidatorTest.php index 92d8a20a79e28..123cb95fe67cc 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/JsonValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/JsonValidatorTest.php @@ -37,9 +37,7 @@ public function testJsonIsValid($value) */ public function testInvalidValues($value) { - $constraint = new Json([ - 'message' => 'myMessageTest', - ]); + $constraint = new Json(message: 'myMessageTest'); $this->validator->validate($value, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php index 9abb9cfc4ecc7..d7c01a4788734 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php @@ -83,9 +83,7 @@ public static function getValidLanguages() */ public function testInvalidLanguages($language) { - $constraint = new Language([ - 'message' => 'myMessage', - ]); + $constraint = new Language(message: 'myMessage'); $this->validator->validate($language, $constraint); @@ -108,9 +106,7 @@ public static function getInvalidLanguages() */ public function testValidAlpha3Languages($language) { - $this->validator->validate($language, new Language([ - 'alpha3' => true, - ])); + $this->validator->validate($language, new Language(alpha3: true)); $this->assertNoViolation(); } @@ -129,10 +125,10 @@ public static function getValidAlpha3Languages() */ public function testInvalidAlpha3Languages($language) { - $constraint = new Language([ - 'alpha3' => true, - 'message' => 'myMessage', - ]); + $constraint = new Language( + alpha3: true, + message: 'myMessage', + ); $this->validator->validate($language, $constraint); @@ -172,9 +168,7 @@ public function testValidateUsingCountrySpecificLocale() \Locale::setDefault('fr_FR'); $existingLanguage = 'en'; - $this->validator->validate($existingLanguage, new Language([ - 'message' => 'aMessage', - ])); + $this->validator->validate($existingLanguage, new Language(message: 'aMessage')); $this->assertNoViolation(); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LengthTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LengthTest.php index 03bd37674922e..93943e2b9721f 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LengthTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthTest.php @@ -24,11 +24,18 @@ class LengthTest extends TestCase { public function testNormalizerCanBeSet() { - $length = new Length(['min' => 0, 'max' => 10, 'normalizer' => 'trim']); + $length = new Length( + min: 0, + max: 10, + normalizer: 'trim', + ); $this->assertEquals('trim', $length->normalizer); } + /** + * @group legacy + */ public function testInvalidNormalizerThrowsException() { $this->expectException(InvalidArgumentException::class); @@ -36,6 +43,9 @@ public function testInvalidNormalizerThrowsException() new Length(['min' => 0, 'max' => 10, 'normalizer' => 'Unknown Callable']); } + /** + * @group legacy + */ public function testInvalidNormalizerObjectThrowsException() { $this->expectException(InvalidArgumentException::class); @@ -45,13 +55,13 @@ public function testInvalidNormalizerObjectThrowsException() public function testDefaultCountUnitIsUsed() { - $length = new Length(['min' => 0, 'max' => 10]); + $length = new Length(min: 0, max: 10); $this->assertSame(Length::COUNT_CODEPOINTS, $length->countUnit); } public function testNonDefaultCountUnitCanBeSet() { - $length = new Length(['min' => 0, 'max' => 10, 'countUnit' => Length::COUNT_GRAPHEMES]); + $length = new Length(min: 0, max: 10, countUnit: Length::COUNT_GRAPHEMES); $this->assertSame(Length::COUNT_GRAPHEMES, $length->countUnit); } @@ -59,7 +69,7 @@ public function testInvalidCountUnitThrowsException() { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage(sprintf('The "countUnit" option must be one of the "%s"::COUNT_* constants ("%s" given).', Length::class, 'nonExistentCountUnit')); - new Length(['min' => 0, 'max' => 10, 'countUnit' => 'nonExistentCountUnit']); + new Length(min: 0, max: 10, countUnit: 'nonExistentCountUnit'); } public function testConstraintDefaultOption() @@ -72,7 +82,7 @@ public function testConstraintDefaultOption() public function testConstraintAttributeDefaultOption() { - $constraint = new Length(['value' => 5, 'exactMessage' => 'message']); + $constraint = new Length(exactly: 5, exactMessage: 'message'); self::assertEquals(5, $constraint->min); self::assertEquals(5, $constraint->max); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php index 0afc9e6e15d64..ffbd92788f152 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php @@ -25,17 +25,17 @@ protected function createValidator(): LengthValidator public function testNullIsValid() { - $this->validator->validate(null, new Length(['value' => 6])); + $this->validator->validate(null, new Length(6)); $this->assertNoViolation(); } public function testEmptyStringIsInvalid() { - $this->validator->validate('', new Length([ - 'value' => $limit = 6, - 'exactMessage' => 'myMessage', - ])); + $this->validator->validate('', new Length( + exactly: $limit = 6, + exactMessage: 'myMessage', + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', '""') @@ -50,7 +50,7 @@ public function testEmptyStringIsInvalid() public function testExpectsStringCompatibleType() { $this->expectException(UnexpectedValueException::class); - $this->validator->validate(new \stdClass(), new Length(['value' => 5])); + $this->validator->validate(new \stdClass(), new Length(5)); } public static function getThreeOrLessCharacters() @@ -118,7 +118,7 @@ public static function getThreeCharactersWithWhitespaces() */ public function testValidValuesMin(int|string $value) { - $constraint = new Length(['min' => 5]); + $constraint = new Length(min: 5); $this->validator->validate($value, $constraint); $this->assertNoViolation(); @@ -129,7 +129,7 @@ public function testValidValuesMin(int|string $value) */ public function testValidValuesMax(int|string $value) { - $constraint = new Length(['max' => 3]); + $constraint = new Length(max: 3); $this->validator->validate($value, $constraint); $this->assertNoViolation(); @@ -151,7 +151,7 @@ public function testValidValuesExact(int|string $value) */ public function testValidNormalizedValues($value) { - $constraint = new Length(['min' => 3, 'max' => 3, 'normalizer' => 'trim']); + $constraint = new Length(min: 3, max: 3, normalizer: 'trim'); $this->validator->validate($value, $constraint); $this->assertNoViolation(); @@ -186,10 +186,10 @@ public function testValidBytesValues() */ public function testInvalidValuesMin(int|string $value, int $valueLength) { - $constraint = new Length([ - 'min' => 4, - 'minMessage' => 'myMessage', - ]); + $constraint = new Length( + min: 4, + minMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -227,10 +227,10 @@ public function testInvalidValuesMinNamed(int|string $value, int $valueLength) */ public function testInvalidValuesMax(int|string $value, int $valueLength) { - $constraint = new Length([ - 'max' => 4, - 'maxMessage' => 'myMessage', - ]); + $constraint = new Length( + max: 4, + maxMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -268,11 +268,11 @@ public function testInvalidValuesMaxNamed(int|string $value, int $valueLength) */ public function testInvalidValuesExactLessThanFour(int|string $value, int $valueLength) { - $constraint = new Length([ - 'min' => 4, - 'max' => 4, - 'exactMessage' => 'myMessage', - ]); + $constraint = new Length( + min: 4, + max: 4, + exactMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -310,11 +310,11 @@ public function testInvalidValuesExactLessThanFourNamed(int|string $value, int $ */ public function testInvalidValuesExactMoreThanFour(int|string $value, int $valueLength) { - $constraint = new Length([ - 'min' => 4, - 'max' => 4, - 'exactMessage' => 'myMessage', - ]); + $constraint = new Length( + min: 4, + max: 4, + exactMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -333,12 +333,12 @@ public function testInvalidValuesExactMoreThanFour(int|string $value, int $value */ public function testOneCharset($value, $charset, $isValid) { - $constraint = new Length([ - 'min' => 1, - 'max' => 1, - 'charset' => $charset, - 'charsetMessage' => 'myMessage', - ]); + $constraint = new Length( + min: 1, + max: 1, + charset: $charset, + charsetMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php index ba81bbd554ae9..9fae06ed7b67e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php @@ -30,7 +30,11 @@ protected function createValidator(): LessThanOrEqualValidator protected static function createConstraint(?array $options = null): Constraint { - return new LessThanOrEqual($options); + if (null !== $options) { + return new LessThanOrEqual(...$options); + } + + return new LessThanOrEqual(); } protected function getErrorCode(): ?string diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorWithNegativeOrZeroConstraintTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorWithNegativeOrZeroConstraintTest.php index ea8e245f61674..076fb298269df 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorWithNegativeOrZeroConstraintTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorWithNegativeOrZeroConstraintTest.php @@ -46,6 +46,9 @@ public static function provideInvalidComparisons(): array ]; } + /** + * @group legacy + */ public function testThrowsConstraintExceptionIfPropertyPath() { $this->expectException(ConstraintDefinitionException::class); @@ -54,6 +57,9 @@ public function testThrowsConstraintExceptionIfPropertyPath() return new NegativeOrZero(['propertyPath' => 'field']); } + /** + * @group legacy + */ public function testThrowsConstraintExceptionIfValue() { $this->expectException(ConstraintDefinitionException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php index 0c4afb7b51cfb..7ceb85f34cecf 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php @@ -30,7 +30,11 @@ protected function createValidator(): LessThanValidator protected static function createConstraint(?array $options = null): Constraint { - return new LessThan($options); + if (null !== $options) { + return new LessThan(...$options); + } + + return new LessThan(); } protected function getErrorCode(): ?string diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorWithNegativeConstraintTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorWithNegativeConstraintTest.php index efe2d6d211e70..7efcbfb2595f7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorWithNegativeConstraintTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorWithNegativeConstraintTest.php @@ -46,6 +46,9 @@ public static function provideInvalidComparisons(): array ]; } + /** + * @group legacy + */ public function testThrowsConstraintExceptionIfPropertyPath() { $this->expectException(ConstraintDefinitionException::class); @@ -54,6 +57,9 @@ public function testThrowsConstraintExceptionIfPropertyPath() return new Negative(['propertyPath' => 'field']); } + /** + * @group legacy + */ public function testThrowsConstraintExceptionIfValue() { $this->expectException(ConstraintDefinitionException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php index 1626eecef48ff..a9429f1883818 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php @@ -71,9 +71,7 @@ public static function getValidLocales() */ public function testInvalidLocales($locale) { - $constraint = new Locale([ - 'message' => 'myMessage', - ]); + $constraint = new Locale(message: 'myMessage'); $this->validator->validate($locale, $constraint); @@ -96,9 +94,7 @@ public static function getInvalidLocales() */ public function testValidLocalesWithCanonicalization(string $locale) { - $constraint = new Locale([ - 'message' => 'myMessage', - ]); + $constraint = new Locale(message: 'myMessage'); $this->validator->validate($locale, $constraint); @@ -110,10 +106,10 @@ public function testValidLocalesWithCanonicalization(string $locale) */ public function testValidLocalesWithoutCanonicalization(string $locale) { - $constraint = new Locale([ - 'message' => 'myMessage', - 'canonicalize' => false, - ]); + $constraint = new Locale( + message: 'myMessage', + canonicalize: false, + ); $this->validator->validate($locale, $constraint); @@ -125,10 +121,10 @@ public function testValidLocalesWithoutCanonicalization(string $locale) */ public function testInvalidLocalesWithoutCanonicalization(string $locale) { - $constraint = new Locale([ - 'message' => 'myMessage', - 'canonicalize' => false, - ]); + $constraint = new Locale( + message: 'myMessage', + canonicalize: false, + ); $this->validator->validate($locale, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php index b0571ebd0de12..9eb33bde6ed9e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php @@ -76,9 +76,7 @@ public static function getValidNumbers() */ public function testInvalidNumbers($number, $code) { - $constraint = new Luhn([ - 'message' => 'myMessage', - ]); + $constraint = new Luhn(message: 'myMessage'); $this->validator->validate($number, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NoSuspiciousCharactersValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NoSuspiciousCharactersValidatorTest.php index d15e41660b7d3..c38a431f50ede 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NoSuspiciousCharactersValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NoSuspiciousCharactersValidatorTest.php @@ -32,7 +32,7 @@ protected function createValidator(): NoSuspiciousCharactersValidator */ public function testNonSuspiciousStrings(string $string, array $options = []) { - $this->validator->validate($string, new NoSuspiciousCharacters($options)); + $this->validator->validate($string, new NoSuspiciousCharacters(...$options)); $this->assertNoViolation(); } @@ -58,7 +58,7 @@ public static function provideNonSuspiciousStrings(): iterable */ public function testSuspiciousStrings(string $string, array $options, array $errors) { - $this->validator->validate($string, new NoSuspiciousCharacters($options)); + $this->validator->validate($string, new NoSuspiciousCharacters(...$options)); $violations = null; diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotBlankTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotBlankTest.php index 77435a37a3ca7..d04a65f1cbe82 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotBlankTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotBlankTest.php @@ -24,7 +24,7 @@ class NotBlankTest extends TestCase { public function testNormalizerCanBeSet() { - $notBlank = new NotBlank(['normalizer' => 'trim']); + $notBlank = new NotBlank(normalizer: 'trim'); $this->assertEquals('trim', $notBlank->normalizer); } @@ -45,6 +45,9 @@ public function testAttributes() self::assertSame('myMessage', $bConstraint->message); } + /** + * @group legacy + */ public function testInvalidNormalizerThrowsException() { $this->expectException(InvalidArgumentException::class); @@ -52,6 +55,9 @@ public function testInvalidNormalizerThrowsException() new NotBlank(['normalizer' => 'Unknown Callable']); } + /** + * @group legacy + */ public function testInvalidNormalizerObjectThrowsException() { $this->expectException(InvalidArgumentException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php index 8d1ba3d0f2608..42d5f3a6010bf 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php @@ -45,9 +45,7 @@ public static function getValidValues() public function testNullIsInvalid() { - $constraint = new NotBlank([ - 'message' => 'myMessage', - ]); + $constraint = new NotBlank(message: 'myMessage'); $this->validator->validate(null, $constraint); @@ -59,9 +57,7 @@ public function testNullIsInvalid() public function testBlankIsInvalid() { - $constraint = new NotBlank([ - 'message' => 'myMessage', - ]); + $constraint = new NotBlank(message: 'myMessage'); $this->validator->validate('', $constraint); @@ -73,9 +69,7 @@ public function testBlankIsInvalid() public function testFalseIsInvalid() { - $constraint = new NotBlank([ - 'message' => 'myMessage', - ]); + $constraint = new NotBlank(message: 'myMessage'); $this->validator->validate(false, $constraint); @@ -87,9 +81,7 @@ public function testFalseIsInvalid() public function testEmptyArrayIsInvalid() { - $constraint = new NotBlank([ - 'message' => 'myMessage', - ]); + $constraint = new NotBlank(message: 'myMessage'); $this->validator->validate([], $constraint); @@ -101,10 +93,10 @@ public function testEmptyArrayIsInvalid() public function testAllowNullTrue() { - $constraint = new NotBlank([ - 'message' => 'myMessage', - 'allowNull' => true, - ]); + $constraint = new NotBlank( + message: 'myMessage', + allowNull: true, + ); $this->validator->validate(null, $constraint); $this->assertNoViolation(); @@ -112,10 +104,10 @@ public function testAllowNullTrue() public function testAllowNullFalse() { - $constraint = new NotBlank([ - 'message' => 'myMessage', - 'allowNull' => false, - ]); + $constraint = new NotBlank( + message: 'myMessage', + allowNull: false, + ); $this->validator->validate(null, $constraint); @@ -130,10 +122,10 @@ public function testAllowNullFalse() */ public function testNormalizedStringIsInvalid($value) { - $constraint = new NotBlank([ - 'message' => 'myMessage', - 'normalizer' => 'trim', - ]); + $constraint = new NotBlank( + message: 'myMessage', + normalizer: 'trim', + ); $this->validator->validate($value, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotCompromisedPasswordValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotCompromisedPasswordValidatorTest.php index 253529444d237..6e1bc76bc8161 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotCompromisedPasswordValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotCompromisedPasswordValidatorTest.php @@ -87,7 +87,7 @@ public function testInvalidPassword() public function testThresholdReached() { - $constraint = new NotCompromisedPassword(['threshold' => 3]); + $constraint = new NotCompromisedPassword(threshold: 3); $this->validator->validate(self::PASSWORD_LEAKED, $constraint); $this->buildViolation($constraint->message) @@ -95,20 +95,21 @@ public function testThresholdReached() ->assertRaised(); } - /** - * @dataProvider provideConstraintsWithThreshold - */ - public function testThresholdNotReached(NotCompromisedPassword $constraint) + public function testThresholdNotReached() { - $this->validator->validate(self::PASSWORD_LEAKED, $constraint); + $this->validator->validate(self::PASSWORD_LEAKED, new NotCompromisedPassword(threshold: 10)); $this->assertNoViolation(); } - public static function provideConstraintsWithThreshold(): iterable + /** + * @group legacy + */ + public function testThresholdNotReachedDoctrineStyle() { - yield 'Doctrine style' => [new NotCompromisedPassword(['threshold' => 10])]; - yield 'named arguments' => [new NotCompromisedPassword(threshold: 10)]; + $this->validator->validate(self::PASSWORD_LEAKED, new NotCompromisedPassword(['threshold' => 10])); + + $this->assertNoViolation(); } public function testValidPassword() @@ -208,19 +209,19 @@ public function testApiError() $this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotCompromisedPassword()); } - /** - * @dataProvider provideErrorSkippingConstraints - */ - public function testApiErrorSkipped(NotCompromisedPassword $constraint) + public function testApiErrorSkipped() { - $this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, $constraint); + $this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotCompromisedPassword(skipOnError: true)); $this->assertTrue(true); // No exception have been thrown } - public static function provideErrorSkippingConstraints(): iterable + /** + * @group legacy + */ + public function testApiErrorSkippedDoctrineStyle() { - yield 'Doctrine style' => [new NotCompromisedPassword(['skipOnError' => true])]; - yield 'named arguments' => [new NotCompromisedPassword(skipOnError: true)]; + $this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotCompromisedPassword(['skipOnError' => true])); + $this->assertTrue(true); // No exception have been thrown } private function createHttpClientStub(?string $returnValue = null): HttpClientInterface diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php index fc5275e3ca5c8..6a5b34846d0dd 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php @@ -30,7 +30,11 @@ protected function createValidator(): NotEqualToValidator protected static function createConstraint(?array $options = null): Constraint { - return new NotEqualTo($options); + if (null !== $options) { + return new NotEqualTo(...$options); + } + + return new NotEqualTo(); } protected function getErrorCode(): ?string diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php index 0376403e235cb..736b710be3d5f 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php @@ -30,7 +30,11 @@ protected function createValidator(): NotIdenticalToValidator protected static function createConstraint(?array $options = null): Constraint { - return new NotIdenticalTo($options); + if (null !== $options) { + return new NotIdenticalTo(...$options); + } + + return new NotIdenticalTo(); } protected function getErrorCode(): ?string diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php index 82156e3263336..fec2ec12a362b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php @@ -42,12 +42,9 @@ public static function getValidValues() ]; } - /** - * @dataProvider provideInvalidConstraints - */ - public function testNullIsInvalid(NotNull $constraint) + public function testNullIsInvalid() { - $this->validator->validate(null, $constraint); + $this->validator->validate(null, new NotNull(message: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ value }}', 'null') @@ -55,11 +52,18 @@ public function testNullIsInvalid(NotNull $constraint) ->assertRaised(); } - public static function provideInvalidConstraints(): iterable + /** + * @group legacy + */ + public function testNullIsInvalidDoctrineStyle() { - yield 'Doctrine style' => [new NotNull([ + $this->validator->validate(null, new NotNull([ 'message' => 'myMessage', - ])]; - yield 'named parameters' => [new NotNull(message: 'myMessage')]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 'null') + ->setCode(NotNull::IS_NULL_ERROR) + ->assertRaised(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RangeTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RangeTest.php index f51898299d7ac..f8265beab6807 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RangeTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RangeTest.php @@ -18,6 +18,9 @@ class RangeTest extends TestCase { + /** + * @group legacy + */ public function testThrowsConstraintExceptionIfBothMinLimitAndPropertyPath() { $this->expectException(ConstraintDefinitionException::class); @@ -35,6 +38,9 @@ public function testThrowsConstraintExceptionIfBothMinLimitAndPropertyPathNamed( new Range(min: 'min', minPropertyPath: 'minPropertyPath'); } + /** + * @group legacy + */ public function testThrowsConstraintExceptionIfBothMaxLimitAndPropertyPath() { $this->expectException(ConstraintDefinitionException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php index 876595c37fc94..a1bbcbd08a79c 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php @@ -29,7 +29,7 @@ protected function createValidator(): RangeValidator public function testNullIsValid() { - $this->validator->validate(null, new Range(['min' => 10, 'max' => 20])); + $this->validator->validate(null, new Range(min: 10, max: 20)); $this->assertNoViolation(); } @@ -69,6 +69,7 @@ public static function getMoreThanTwenty(): array } /** + * @group legacy * @dataProvider getTenToTwenty */ public function testValidValuesMin($value) @@ -91,6 +92,7 @@ public function testValidValuesMinNamed($value) } /** + * @group legacy * @dataProvider getTenToTwenty */ public function testValidValuesMax($value) @@ -113,6 +115,7 @@ public function testValidValuesMaxNamed($value) } /** + * @group legacy * @dataProvider getTenToTwenty */ public function testValidValuesMinMax($value) @@ -135,6 +138,7 @@ public function testValidValuesMinMaxNamed($value) } /** + * @group legacy * @dataProvider getLessThanTen */ public function testInvalidValuesMin($value, $formattedValue) @@ -170,6 +174,7 @@ public function testInvalidValuesMinNamed($value, $formattedValue) } /** + * @group legacy * @dataProvider getMoreThanTwenty */ public function testInvalidValuesMax($value, $formattedValue) @@ -205,6 +210,7 @@ public function testInvalidValuesMaxNamed($value, $formattedValue) } /** + * @group legacy * @dataProvider getMoreThanTwenty */ public function testInvalidValuesCombinedMax($value, $formattedValue) @@ -243,6 +249,7 @@ public function testInvalidValuesCombinedMaxNamed($value, $formattedValue) } /** + * @group legacy * @dataProvider getLessThanTen */ public function testInvalidValuesCombinedMin($value, $formattedValue) @@ -344,7 +351,7 @@ public static function getLaterThanTwentiethMarch2014(): array */ public function testValidDatesMin($value) { - $constraint = new Range(['min' => 'March 10, 2014']); + $constraint = new Range(min: 'March 10, 2014'); $this->validator->validate($value, $constraint); $this->assertNoViolation(); @@ -355,7 +362,7 @@ public function testValidDatesMin($value) */ public function testValidDatesMax($value) { - $constraint = new Range(['max' => 'March 20, 2014']); + $constraint = new Range(max: 'March 20, 2014'); $this->validator->validate($value, $constraint); $this->assertNoViolation(); @@ -366,7 +373,7 @@ public function testValidDatesMax($value) */ public function testValidDatesMinMax($value) { - $constraint = new Range(['min' => 'March 10, 2014', 'max' => 'March 20, 2014']); + $constraint = new Range(min: 'March 10, 2014', max: 'March 20, 2014'); $this->validator->validate($value, $constraint); $this->assertNoViolation(); @@ -381,10 +388,10 @@ public function testInvalidDatesMin(\DateTimeInterface $value, string $dateTimeA // Make sure we have the correct version loaded IntlTestHelper::requireIntl($this, '57.1'); - $constraint = new Range([ - 'min' => 'March 10, 2014', - 'minMessage' => 'myMessage', - ]); + $constraint = new Range( + min: 'March 10, 2014', + minMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -404,10 +411,10 @@ public function testInvalidDatesMax(\DateTimeInterface $value, string $dateTimeA // Make sure we have the correct version loaded IntlTestHelper::requireIntl($this, '57.1'); - $constraint = new Range([ - 'max' => 'March 20, 2014', - 'maxMessage' => 'myMessage', - ]); + $constraint = new Range( + max: 'March 20, 2014', + maxMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -427,11 +434,11 @@ public function testInvalidDatesCombinedMax(\DateTimeInterface $value, string $d // Make sure we have the correct version loaded IntlTestHelper::requireIntl($this, '57.1'); - $constraint = new Range([ - 'min' => 'March 10, 2014', - 'max' => 'March 20, 2014', - 'notInRangeMessage' => 'myNotInRangeMessage', - ]); + $constraint = new Range( + min: 'March 10, 2014', + max: 'March 20, 2014', + notInRangeMessage: 'myNotInRangeMessage', + ); $this->validator->validate($value, $constraint); @@ -452,11 +459,11 @@ public function testInvalidDatesCombinedMin($value, $dateTimeAsString) // Make sure we have the correct version loaded IntlTestHelper::requireIntl($this, '57.1'); - $constraint = new Range([ - 'min' => 'March 10, 2014', - 'max' => 'March 20, 2014', - 'notInRangeMessage' => 'myNotInRangeMessage', - ]); + $constraint = new Range( + min: 'March 10, 2014', + max: 'March 20, 2014', + notInRangeMessage: 'myNotInRangeMessage', + ); $this->validator->validate($value, $constraint); @@ -481,10 +488,10 @@ public function getInvalidValues(): array public function testNonNumeric() { - $constraint = new Range([ - 'min' => 10, - 'max' => 20, - ]); + $constraint = new Range( + min: 10, + max: 20, + ); $this->validator->validate('abcd', $constraint); @@ -496,9 +503,9 @@ public function testNonNumeric() public function testNonNumericWithParsableDatetimeMinAndMaxNull() { - $constraint = new Range([ - 'min' => 'March 10, 2014', - ]); + $constraint = new Range( + min: 'March 10, 2014', + ); $this->validator->validate('abcd', $constraint); @@ -510,9 +517,9 @@ public function testNonNumericWithParsableDatetimeMinAndMaxNull() public function testNonNumericWithParsableDatetimeMaxAndMinNull() { - $constraint = new Range([ - 'max' => 'March 20, 2014', - ]); + $constraint = new Range( + max: 'March 20, 2014', + ); $this->validator->validate('abcd', $constraint); @@ -524,10 +531,10 @@ public function testNonNumericWithParsableDatetimeMaxAndMinNull() public function testNonNumericWithParsableDatetimeMinAndMax() { - $constraint = new Range([ - 'min' => 'March 10, 2014', - 'max' => 'March 20, 2014', - ]); + $constraint = new Range( + min: 'March 10, 2014', + max: 'March 20, 2014', + ); $this->validator->validate('abcd', $constraint); @@ -539,10 +546,10 @@ public function testNonNumericWithParsableDatetimeMinAndMax() public function testNonNumericWithNonParsableDatetimeMin() { - $constraint = new Range([ - 'min' => 'March 40, 2014', - 'max' => 'March 20, 2014', - ]); + $constraint = new Range( + min: 'March 40, 2014', + max: 'March 20, 2014', + ); $this->validator->validate('abcd', $constraint); @@ -554,10 +561,10 @@ public function testNonNumericWithNonParsableDatetimeMin() public function testNonNumericWithNonParsableDatetimeMax() { - $constraint = new Range([ - 'min' => 'March 10, 2014', - 'max' => 'March 50, 2014', - ]); + $constraint = new Range( + min: 'March 10, 2014', + max: 'March 50, 2014', + ); $this->validator->validate('abcd', $constraint); @@ -569,10 +576,10 @@ public function testNonNumericWithNonParsableDatetimeMax() public function testNonNumericWithNonParsableDatetimeMinAndMax() { - $constraint = new Range([ - 'min' => 'March 40, 2014', - 'max' => 'March 50, 2014', - ]); + $constraint = new Range( + min: 'March 40, 2014', + max: 'March 50, 2014', + ); $this->validator->validate('abcd', $constraint); @@ -590,10 +597,10 @@ public function testThrowsOnInvalidStringDates($expectedMessage, $value, $min, $ $this->expectException(ConstraintDefinitionException::class); $this->expectExceptionMessage($expectedMessage); - $this->validator->validate($value, new Range([ - 'min' => $min, - 'max' => $max, - ])); + $this->validator->validate($value, new Range( + min: $min, + max: $max, + )); } public static function throwsOnInvalidStringDatesProvider(): array @@ -611,15 +618,16 @@ public function testNoViolationOnNullObjectWithPropertyPaths() { $this->setObject(null); - $this->validator->validate(1, new Range([ - 'minPropertyPath' => 'minPropertyPath', - 'maxPropertyPath' => 'maxPropertyPath', - ])); + $this->validator->validate(1, new Range( + minPropertyPath: 'minPropertyPath', + maxPropertyPath: 'maxPropertyPath', + )); $this->assertNoViolation(); } /** + * @group legacy * @dataProvider getTenToTwenty */ public function testValidValuesMinPropertyPath($value) @@ -652,9 +660,9 @@ public function testValidValuesMaxPropertyPath($value) { $this->setObject(new Limit(20)); - $this->validator->validate($value, new Range([ - 'maxPropertyPath' => 'value', - ])); + $this->validator->validate($value, new Range( + maxPropertyPath: 'value', + )); $this->assertNoViolation(); } @@ -678,10 +686,10 @@ public function testValidValuesMinMaxPropertyPath($value) { $this->setObject(new MinMax(10, 20)); - $this->validator->validate($value, new Range([ - 'minPropertyPath' => 'min', - 'maxPropertyPath' => 'max', - ])); + $this->validator->validate($value, new Range( + minPropertyPath: 'min', + maxPropertyPath: 'max', + )); $this->assertNoViolation(); } @@ -693,10 +701,10 @@ public function testInvalidValuesMinPropertyPath($value, $formattedValue) { $this->setObject(new Limit(10)); - $constraint = new Range([ - 'minPropertyPath' => 'value', - 'minMessage' => 'myMessage', - ]); + $constraint = new Range( + minPropertyPath: 'value', + minMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -715,10 +723,10 @@ public function testInvalidValuesMaxPropertyPath($value, $formattedValue) { $this->setObject(new Limit(20)); - $constraint = new Range([ - 'maxPropertyPath' => 'value', - 'maxMessage' => 'myMessage', - ]); + $constraint = new Range( + maxPropertyPath: 'value', + maxMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -731,6 +739,7 @@ public function testInvalidValuesMaxPropertyPath($value, $formattedValue) } /** + * @group legacy * @dataProvider getMoreThanTwenty */ public function testInvalidValuesCombinedMaxPropertyPath($value, $formattedValue) @@ -781,6 +790,7 @@ public function testInvalidValuesCombinedMaxPropertyPathNamed($value, $formatted } /** + * @group legacy * @dataProvider getLessThanTen */ public function testInvalidValuesCombinedMinPropertyPath($value, $formattedValue) @@ -837,11 +847,11 @@ public function testViolationOnNullObjectWithDefinedMin($value, $formattedValue) { $this->setObject(null); - $this->validator->validate($value, new Range([ - 'min' => 10, - 'maxPropertyPath' => 'max', - 'minMessage' => 'myMessage', - ])); + $this->validator->validate($value, new Range( + min: 10, + maxPropertyPath: 'max', + minMessage: 'myMessage', + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', $formattedValue) @@ -858,11 +868,11 @@ public function testViolationOnNullObjectWithDefinedMax($value, $formattedValue) { $this->setObject(null); - $this->validator->validate($value, new Range([ - 'minPropertyPath' => 'min', - 'max' => 20, - 'maxMessage' => 'myMessage', - ])); + $this->validator->validate($value, new Range( + minPropertyPath: 'min', + max: 20, + maxMessage: 'myMessage', + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', $formattedValue) @@ -879,7 +889,7 @@ public function testValidDatesMinPropertyPath($value) { $this->setObject(new Limit('March 10, 2014')); - $this->validator->validate($value, new Range(['minPropertyPath' => 'value'])); + $this->validator->validate($value, new Range(minPropertyPath: 'value')); $this->assertNoViolation(); } @@ -891,7 +901,7 @@ public function testValidDatesMaxPropertyPath($value) { $this->setObject(new Limit('March 20, 2014')); - $constraint = new Range(['maxPropertyPath' => 'value']); + $constraint = new Range(maxPropertyPath: 'value'); $this->validator->validate($value, $constraint); $this->assertNoViolation(); @@ -904,7 +914,7 @@ public function testValidDatesMinMaxPropertyPath($value) { $this->setObject(new MinMax('March 10, 2014', 'March 20, 2014')); - $constraint = new Range(['minPropertyPath' => 'min', 'maxPropertyPath' => 'max']); + $constraint = new Range(minPropertyPath: 'min', maxPropertyPath: 'max'); $this->validator->validate($value, $constraint); $this->assertNoViolation(); @@ -921,10 +931,10 @@ public function testInvalidDatesMinPropertyPath($value, $dateTimeAsString) $this->setObject(new Limit('March 10, 2014')); - $constraint = new Range([ - 'minPropertyPath' => 'value', - 'minMessage' => 'myMessage', - ]); + $constraint = new Range( + minPropertyPath: 'value', + minMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -947,10 +957,10 @@ public function testInvalidDatesMaxPropertyPath($value, $dateTimeAsString) $this->setObject(new Limit('March 20, 2014')); - $constraint = new Range([ - 'maxPropertyPath' => 'value', - 'maxMessage' => 'myMessage', - ]); + $constraint = new Range( + maxPropertyPath: 'value', + maxMessage: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -973,11 +983,11 @@ public function testInvalidDatesCombinedMaxPropertyPath($value, $dateTimeAsStrin $this->setObject(new MinMax('March 10, 2014', 'March 20, 2014')); - $constraint = new Range([ - 'minPropertyPath' => 'min', - 'maxPropertyPath' => 'max', - 'notInRangeMessage' => 'myNotInRangeMessage', - ]); + $constraint = new Range( + minPropertyPath: 'min', + maxPropertyPath: 'max', + notInRangeMessage: 'myNotInRangeMessage', + ); $this->validator->validate($value, $constraint); @@ -1002,11 +1012,11 @@ public function testInvalidDatesCombinedMinPropertyPath($value, $dateTimeAsStrin $this->setObject(new MinMax('March 10, 2014', 'March 20, 2014')); - $constraint = new Range([ - 'minPropertyPath' => 'min', - 'maxPropertyPath' => 'max', - 'notInRangeMessage' => 'myNotInRangeMessage', - ]); + $constraint = new Range( + minPropertyPath: 'min', + maxPropertyPath: 'max', + notInRangeMessage: 'myNotInRangeMessage', + ); $this->validator->validate($value, $constraint); @@ -1022,7 +1032,7 @@ public function testInvalidDatesCombinedMinPropertyPath($value, $dateTimeAsStrin public static function provideMessageIfMinAndMaxSet(): array { - $notInRangeMessage = (new Range(['min' => '']))->notInRangeMessage; + $notInRangeMessage = (new Range(min: ''))->notInRangeMessage; return [ [ @@ -1037,30 +1047,6 @@ public static function provideMessageIfMinAndMaxSet(): array 'not_in_range_message', Range::NOT_IN_RANGE_ERROR, ], - [ - ['minMessage' => 'min_message'], - 0, - $notInRangeMessage, - Range::NOT_IN_RANGE_ERROR, - ], - [ - ['maxMessage' => 'max_message'], - 0, - $notInRangeMessage, - Range::NOT_IN_RANGE_ERROR, - ], - [ - ['minMessage' => 'min_message'], - 15, - $notInRangeMessage, - Range::NOT_IN_RANGE_ERROR, - ], - [ - ['maxMessage' => 'max_message'], - 15, - $notInRangeMessage, - Range::NOT_IN_RANGE_ERROR, - ], ]; } @@ -1069,7 +1055,7 @@ public static function provideMessageIfMinAndMaxSet(): array */ public function testMessageIfMinAndMaxSet(array $constraintExtraOptions, int $value, string $expectedMessage, string $expectedCode) { - $constraint = new Range(array_merge(['min' => 1, 'max' => 10], $constraintExtraOptions)); + $constraint = new Range(...array_merge(['min' => 1, 'max' => 10], $constraintExtraOptions)); $this->validator->validate($value, $constraint); $this diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RegexTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RegexTest.php index ba24fb0cb2073..96b5d5fc4386d 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RegexTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RegexTest.php @@ -69,10 +69,10 @@ public static function provideHtmlPatterns() */ public function testGetHtmlPattern($pattern, $htmlPattern, $match = true) { - $constraint = new Regex([ - 'pattern' => $pattern, - 'match' => $match, - ]); + $constraint = new Regex( + pattern: $pattern, + match: $match, + ); $this->assertSame($pattern, $constraint->pattern); $this->assertSame($htmlPattern, $constraint->getHtmlPattern()); @@ -80,10 +80,10 @@ public function testGetHtmlPattern($pattern, $htmlPattern, $match = true) public function testGetCustomHtmlPattern() { - $constraint = new Regex([ - 'pattern' => '((?![0-9]$|[a-z]+).)*', - 'htmlPattern' => 'foobar', - ]); + $constraint = new Regex( + pattern: '((?![0-9]$|[a-z]+).)*', + htmlPattern: 'foobar', + ); $this->assertSame('((?![0-9]$|[a-z]+).)*', $constraint->pattern); $this->assertSame('foobar', $constraint->getHtmlPattern()); @@ -91,11 +91,14 @@ public function testGetCustomHtmlPattern() public function testNormalizerCanBeSet() { - $regex = new Regex(['pattern' => '/^[0-9]+$/', 'normalizer' => 'trim']); + $regex = new Regex(pattern: '/^[0-9]+$/', normalizer: 'trim'); $this->assertEquals('trim', $regex->normalizer); } + /** + * @group legacy + */ public function testInvalidNormalizerThrowsException() { $this->expectException(InvalidArgumentException::class); @@ -103,6 +106,9 @@ public function testInvalidNormalizerThrowsException() new Regex(['pattern' => '/^[0-9]+$/', 'normalizer' => 'Unknown Callable']); } + /** + * @group legacy + */ public function testInvalidNormalizerObjectThrowsException() { $this->expectException(InvalidArgumentException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php index 360fb62596810..a686508b3540f 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php @@ -25,14 +25,14 @@ protected function createValidator(): RegexValidator public function testNullIsValid() { - $this->validator->validate(null, new Regex(['pattern' => '/^[0-9]+$/'])); + $this->validator->validate(null, new Regex('/^[0-9]+$/')); $this->assertNoViolation(); } public function testEmptyStringIsValid() { - $this->validator->validate('', new Regex(['pattern' => '/^[0-9]+$/'])); + $this->validator->validate('', new Regex('/^[0-9]+$/')); $this->assertNoViolation(); } @@ -40,7 +40,7 @@ public function testEmptyStringIsValid() public function testExpectsStringCompatibleType() { $this->expectException(UnexpectedValueException::class); - $this->validator->validate(new \stdClass(), new Regex(['pattern' => '/^[0-9]+$/'])); + $this->validator->validate(new \stdClass(), new Regex('/^[0-9]+$/')); } /** @@ -48,13 +48,14 @@ public function testExpectsStringCompatibleType() */ public function testValidValues($value) { - $constraint = new Regex(['pattern' => '/^[0-9]+$/']); + $constraint = new Regex('/^[0-9]+$/'); $this->validator->validate($value, $constraint); $this->assertNoViolation(); } /** + * @group legacy * @dataProvider getValidValuesWithWhitespaces */ public function testValidValuesWithWhitespaces($value) @@ -105,6 +106,7 @@ public static function getValidValuesWithWhitespaces() } /** + * @group legacy * @dataProvider getInvalidValues */ public function testInvalidValues($value) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/SequentiallyValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/SequentiallyValidatorTest.php index 657ff2637f2c9..6a8287d52106d 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/SequentiallyValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/SequentiallyValidatorTest.php @@ -33,7 +33,7 @@ public function testWalkThroughConstraints() { $constraints = [ new Type('number'), - new Range(['min' => 4]), + new Range(min: 4), ]; $value = 6; @@ -50,7 +50,7 @@ public function testStopsAtFirstConstraintWithViolations() { $constraints = [ new Type('string'), - new Regex(['pattern' => '[a-z]']), + new Regex('[a-z]'), new NotEqualTo('Foo'), ]; @@ -68,20 +68,20 @@ public function testNestedConstraintsAreNotExecutedWhenGroupDoesNotMatch() { $validator = Validation::createValidator(); - $violations = $validator->validate(50, new Sequentially([ - 'constraints' => [ - new GreaterThan([ - 'groups' => 'senior', - 'value' => 55, - ]), - new Range([ - 'groups' => 'adult', - 'min' => 18, - 'max' => 55, - ]), + $violations = $validator->validate(50, new Sequentially( + constraints: [ + new GreaterThan( + groups: ['senior'], + value: 55, + ), + new Range( + groups: ['adult'], + min: 18, + max: 55, + ), ], - 'groups' => ['adult', 'senior'], - ]), 'adult'); + groups: ['adult', 'senior'], + ), 'adult'); $this->assertCount(0, $violations); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php index 91d6778ed4ec1..577cc60458a60 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php @@ -74,9 +74,7 @@ public static function getValidTimes() */ public function testValidTimesWithoutSeconds(string $time) { - $this->validator->validate($time, new Time([ - 'withSeconds' => false, - ])); + $this->validator->validate($time, new Time(withSeconds: false)); $this->assertNoViolation(); } @@ -117,9 +115,7 @@ public static function getInvalidTimesWithoutSeconds() */ public function testInvalidTimes($time, $code) { - $constraint = new Time([ - 'message' => 'myMessage', - ]); + $constraint = new Time(message: 'myMessage'); $this->validator->validate($time, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php index 42a38a7110101..1144b2933ad82 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php @@ -25,12 +25,12 @@ class TimezoneTest extends TestCase public function testValidTimezoneConstraints() { new Timezone(); - new Timezone(['zone' => \DateTimeZone::ALL]); + new Timezone(zone: \DateTimeZone::ALL); new Timezone(\DateTimeZone::ALL_WITH_BC); - new Timezone([ - 'zone' => \DateTimeZone::PER_COUNTRY, - 'countryCode' => 'AR', - ]); + new Timezone( + zone: \DateTimeZone::PER_COUNTRY, + countryCode: 'AR', + ); $this->addToAssertionCount(1); } @@ -38,16 +38,16 @@ public function testValidTimezoneConstraints() public function testExceptionForGroupedTimezonesByCountryWithWrongZone() { $this->expectException(ConstraintDefinitionException::class); - new Timezone([ - 'zone' => \DateTimeZone::ALL, - 'countryCode' => 'AR', - ]); + new Timezone( + zone: \DateTimeZone::ALL, + countryCode: 'AR', + ); } public function testExceptionForGroupedTimezonesByCountryWithoutZone() { $this->expectException(ConstraintDefinitionException::class); - new Timezone(['countryCode' => 'AR']); + new Timezone(countryCode: 'AR'); } /** @@ -56,7 +56,7 @@ public function testExceptionForGroupedTimezonesByCountryWithoutZone() public function testExceptionForInvalidGroupedTimezones(int $zone) { $this->expectException(ConstraintDefinitionException::class); - new Timezone(['zone' => $zone]); + new Timezone($zone); } public static function provideInvalidZones(): iterable diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index 527b2a66443ed..223486045c892 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -96,9 +96,7 @@ public static function getValidTimezones(): iterable */ public function testValidGroupedTimezones(string $timezone, int $zone) { - $constraint = new Timezone([ - 'zone' => $zone, - ]); + $constraint = new Timezone($zone); $this->validator->validate($timezone, $constraint); @@ -129,9 +127,7 @@ public static function getValidGroupedTimezones(): iterable */ public function testInvalidTimezoneWithoutZone(string $timezone) { - $constraint = new Timezone([ - 'message' => 'myMessage', - ]); + $constraint = new Timezone(message: 'myMessage'); $this->validator->validate($timezone, $constraint); @@ -154,10 +150,10 @@ public static function getInvalidTimezones(): iterable */ public function testInvalidGroupedTimezones(string $timezone, int $zone) { - $constraint = new Timezone([ - 'zone' => $zone, - 'message' => 'myMessage', - ]); + $constraint = new Timezone( + zone: $zone, + message: 'myMessage', + ); $this->validator->validate($timezone, $constraint); @@ -197,10 +193,10 @@ public function testInvalidGroupedTimezoneNamed() */ public function testValidGroupedTimezonesByCountry(string $timezone, string $country) { - $constraint = new Timezone([ - 'zone' => \DateTimeZone::PER_COUNTRY, - 'countryCode' => $country, - ]); + $constraint = new Timezone( + zone: \DateTimeZone::PER_COUNTRY, + countryCode: $country, + ); $this->validator->validate($timezone, $constraint); @@ -234,11 +230,11 @@ public static function getValidGroupedTimezonesByCountry(): iterable */ public function testInvalidGroupedTimezonesByCountry(string $timezone, string $countryCode) { - $constraint = new Timezone([ - 'message' => 'myMessage', - 'zone' => \DateTimeZone::PER_COUNTRY, - 'countryCode' => $countryCode, - ]); + $constraint = new Timezone( + message: 'myMessage', + zone: \DateTimeZone::PER_COUNTRY, + countryCode: $countryCode, + ); $this->validator->validate($timezone, $constraint); @@ -259,11 +255,11 @@ public static function getInvalidGroupedTimezonesByCountry(): iterable public function testGroupedTimezonesWithInvalidCountry() { - $constraint = new Timezone([ - 'message' => 'myMessage', - 'zone' => \DateTimeZone::PER_COUNTRY, - 'countryCode' => 'foobar', - ]); + $constraint = new Timezone( + message: 'myMessage', + zone: \DateTimeZone::PER_COUNTRY, + countryCode: 'foobar', + ); $this->validator->validate('Europe/Amsterdam', $constraint); @@ -290,9 +286,7 @@ public function testDeprecatedTimezonesAreValidWithBC(string $timezone) */ public function testDeprecatedTimezonesAreInvalidWithoutBC(string $timezone) { - $constraint = new Timezone([ - 'message' => 'myMessage', - ]); + $constraint = new Timezone(message: 'myMessage'); $this->validator->validate($timezone, $constraint); @@ -336,10 +330,10 @@ public function testIntlCompatibility() $this->markTestSkipped('"Europe/Saratov" is expired until 2017, current version is '.$tzDbVersion); } - $constraint = new Timezone([ - 'message' => 'myMessage', - 'intlCompatible' => true, - ]); + $constraint = new Timezone( + message: 'myMessage', + intlCompatible: true, + ); $this->validator->validate('Europe/Saratov', $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php index 99714407fad1b..d1607fd6ae375 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php @@ -26,7 +26,7 @@ protected function createValidator(): TypeValidator public function testNullIsValid() { - $constraint = new Type(['type' => 'integer']); + $constraint = new Type('integer'); $this->validator->validate(null, $constraint); @@ -35,7 +35,7 @@ public function testNullIsValid() public function testEmptyIsValidIfString() { - $constraint = new Type(['type' => 'string']); + $constraint = new Type('string'); $this->validator->validate('', $constraint); @@ -44,10 +44,10 @@ public function testEmptyIsValidIfString() public function testEmptyIsInvalidIfNoString() { - $constraint = new Type([ - 'type' => 'integer', - 'message' => 'myMessage', - ]); + $constraint = new Type( + type: 'integer', + message: 'myMessage', + ); $this->validator->validate('', $constraint); @@ -63,7 +63,7 @@ public function testEmptyIsInvalidIfNoString() */ public function testValidValues($value, $type) { - $constraint = new Type(['type' => $type]); + $constraint = new Type($type); $this->validator->validate($value, $constraint); @@ -123,10 +123,10 @@ public static function getValidValues() */ public function testInvalidValues($value, $type, $valueAsString) { - $constraint = new Type([ - 'type' => $type, - 'message' => 'myMessage', - ]); + $constraint = new Type( + type: $type, + message: 'myMessage', + ); $this->validator->validate($value, $constraint); @@ -195,7 +195,7 @@ public static function getInvalidValues() */ public function testValidValuesMultipleTypes($value, array $types) { - $constraint = new Type(['type' => $types]); + $constraint = new Type($types); $this->validator->validate($value, $constraint); @@ -210,12 +210,9 @@ public static function getValidValuesMultipleTypes() ]; } - /** - * @dataProvider provideConstraintsWithMultipleTypes - */ - public function testInvalidValuesMultipleTypes(Type $constraint) + public function testInvalidValuesMultipleTypes() { - $this->validator->validate('12345', $constraint); + $this->validator->validate('12345', new Type(type: ['boolean', 'array'], message: 'myMessage')); $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"12345"') @@ -224,13 +221,21 @@ public function testInvalidValuesMultipleTypes(Type $constraint) ->assertRaised(); } - public static function provideConstraintsWithMultipleTypes() + /** + * @group legacy + */ + public function testInvalidValuesMultipleTypesDoctrineStyle() { - yield 'Doctrine style' => [new Type([ + $this->validator->validate('12345', new Type([ 'type' => ['boolean', 'array'], 'message' => 'myMessage', - ])]; - yield 'named arguments' => [new Type(type: ['boolean', 'array'], message: 'myMessage')]; + ])); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"12345"') + ->setParameter('{{ type }}', implode('|', ['boolean', 'array'])) + ->setCode(Type::INVALID_TYPE_ERROR) + ->assertRaised(); } protected static function createFile() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UlidValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UlidValidatorTest.php index abe5490d18520..402e55dc10da6 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UlidValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UlidValidatorTest.php @@ -58,9 +58,7 @@ public function testValidUlid() */ public function testInvalidUlid(string $ulid, string $code) { - $constraint = new Ulid([ - 'message' => 'testMessage', - ]); + $constraint = new Ulid(message: 'testMessage'); $this->validator->validate($ulid, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UniqueTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UniqueTest.php index 7d882a9c3cfb3..9fe2599fd0e99 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UniqueTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UniqueTest.php @@ -37,6 +37,9 @@ public function testAttributes() self::assertSame('intval', $dConstraint->normalizer); } + /** + * @group legacy + */ public function testInvalidNormalizerThrowsException() { $this->expectException(InvalidArgumentException::class); @@ -44,6 +47,9 @@ public function testInvalidNormalizerThrowsException() new Unique(['normalizer' => 'Unknown Callable']); } + /** + * @group legacy + */ public function testInvalidNormalizerObjectThrowsException() { $this->expectException(InvalidArgumentException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php index 3c2dd9f21c98f..c84074210fff3 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php @@ -63,9 +63,7 @@ public static function getValidValues() */ public function testInvalidValues($value) { - $constraint = new Unique([ - 'message' => 'myMessage', - ]); + $constraint = new Unique(message: 'myMessage'); $this->validator->validate($value, $constraint); $this->buildViolation('myMessage') @@ -118,9 +116,7 @@ public function testExpectsUniqueObjects($callback) $value = [$object1, $object2, $object3]; - $this->validator->validate($value, new Unique([ - 'normalizer' => $callback, - ])); + $this->validator->validate($value, new Unique(normalizer: $callback)); $this->assertNoViolation(); } @@ -144,10 +140,10 @@ public function testExpectsNonUniqueObjects($callback) $value = [$object1, $object2, $object3]; - $this->validator->validate($value, new Unique([ - 'message' => 'myMessage', - 'normalizer' => $callback, - ])); + $this->validator->validate($value, new Unique( + message: 'myMessage', + normalizer: $callback, + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', 'array') @@ -168,10 +164,10 @@ public static function getCallback(): array public function testExpectsInvalidNonStrictComparison() { - $this->validator->validate([1, '1', 1.0, '1.0'], new Unique([ - 'message' => 'myMessage', - 'normalizer' => 'intval', - ])); + $this->validator->validate([1, '1', 1.0, '1.0'], new Unique( + message: 'myMessage', + normalizer: 'intval', + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', 'array') @@ -183,9 +179,7 @@ public function testExpectsValidNonStrictComparison() { $callback = static fn ($item) => (int) $item; - $this->validator->validate([1, '2', 3, '4.0'], new Unique([ - 'normalizer' => $callback, - ])); + $this->validator->validate([1, '2', 3, '4.0'], new Unique(normalizer: $callback)); $this->assertNoViolation(); } @@ -194,10 +188,10 @@ public function testExpectsInvalidCaseInsensitiveComparison() { $callback = static fn ($item) => mb_strtolower($item); - $this->validator->validate(['Hello', 'hello', 'HELLO', 'hellO'], new Unique([ - 'message' => 'myMessage', - 'normalizer' => $callback, - ])); + $this->validator->validate(['Hello', 'hello', 'HELLO', 'hellO'], new Unique( + message: 'myMessage', + normalizer: $callback, + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', 'array') @@ -209,9 +203,7 @@ public function testExpectsValidCaseInsensitiveComparison() { $callback = static fn ($item) => mb_strtolower($item); - $this->validator->validate(['Hello', 'World'], new Unique([ - 'normalizer' => $callback, - ])); + $this->validator->validate(['Hello', 'World'], new Unique(normalizer: $callback)); $this->assertNoViolation(); } @@ -248,9 +240,10 @@ public static function getInvalidFieldNames(): array */ public function testInvalidCollectionValues(array $value, array $fields) { - $this->validator->validate($value, new Unique([ - 'message' => 'myMessage', - ], fields: $fields)); + $this->validator->validate($value, new Unique( + message: 'myMessage', + fields: $fields, + )); $this->buildViolation('myMessage') ->setParameter('{{ value }}', 'array') diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UrlTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UrlTest.php index 1d641aa925077..8dd593d2e5a76 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UrlTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UrlTest.php @@ -24,11 +24,14 @@ class UrlTest extends TestCase { public function testNormalizerCanBeSet() { - $url = new Url(['normalizer' => 'trim', 'requireTld' => true]); + $url = new Url(normalizer: 'trim', requireTld: true); $this->assertEquals('trim', $url->normalizer); } + /** + * @group legacy + */ public function testInvalidNormalizerThrowsException() { $this->expectException(InvalidArgumentException::class); @@ -36,6 +39,9 @@ public function testInvalidNormalizerThrowsException() new Url(['normalizer' => 'Unknown Callable', 'requireTld' => true]); } + /** + * @group legacy + */ public function testInvalidNormalizerObjectThrowsException() { $this->expectException(InvalidArgumentException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php index d1ae946eb6fa3..e1a4463d675e6 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php @@ -65,10 +65,10 @@ public function testValidUrls($url) */ public function testValidUrlsWithWhitespaces($url) { - $this->validator->validate($url, new Url([ - 'normalizer' => 'trim', - 'requireTld' => true, - ])); + $this->validator->validate($url, new Url( + normalizer: 'trim', + requireTld: true, + )); $this->assertNoViolation(); } @@ -79,10 +79,10 @@ public function testValidUrlsWithWhitespaces($url) */ public function testValidRelativeUrl($url) { - $constraint = new Url([ - 'relativeProtocol' => true, - 'requireTld' => false, - ]); + $constraint = new Url( + relativeProtocol: true, + requireTld: false, + ); $this->validator->validate($url, $constraint); @@ -198,10 +198,10 @@ public static function getValidUrlsWithWhitespaces() */ public function testInvalidUrls($url) { - $constraint = new Url([ - 'message' => 'myMessage', - 'requireTld' => false, - ]); + $constraint = new Url( + message: 'myMessage', + requireTld: false, + ); $this->validator->validate($url, $constraint); @@ -217,11 +217,11 @@ public function testInvalidUrls($url) */ public function testInvalidRelativeUrl($url) { - $constraint = new Url([ - 'message' => 'myMessage', - 'relativeProtocol' => true, - 'requireTld' => false, - ]); + $constraint = new Url( + message: 'myMessage', + relativeProtocol: true, + requireTld: false, + ); $this->validator->validate($url, $constraint); @@ -300,10 +300,10 @@ public static function getInvalidUrls() */ public function testCustomProtocolIsValid($url, $requireTld) { - $constraint = new Url([ - 'protocols' => ['ftp', 'file', 'git'], - 'requireTld' => $requireTld, - ]); + $constraint = new Url( + protocols: ['ftp', 'file', 'git'], + requireTld: $requireTld, + ); $this->validator->validate($url, $constraint); @@ -324,9 +324,7 @@ public static function getValidCustomUrls() */ public function testRequiredTld(string $url, bool $requireTld, bool $isValid) { - $constraint = new Url([ - 'requireTld' => $requireTld, - ]); + $constraint = new Url(requireTld: $requireTld); $this->validator->validate($url, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UuidTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UuidTest.php index 3da8b81336719..22901a9db3027 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UuidTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UuidTest.php @@ -24,11 +24,14 @@ class UuidTest extends TestCase { public function testNormalizerCanBeSet() { - $uuid = new Uuid(['normalizer' => 'trim']); + $uuid = new Uuid(normalizer: 'trim'); $this->assertEquals('trim', $uuid->normalizer); } + /** + * @group legacy + */ public function testInvalidNormalizerThrowsException() { $this->expectException(InvalidArgumentException::class); @@ -36,6 +39,9 @@ public function testInvalidNormalizerThrowsException() new Uuid(['normalizer' => 'Unknown Callable']); } + /** + * @group legacy + */ public function testInvalidNormalizerObjectThrowsException() { $this->expectException(InvalidArgumentException::class); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/UuidValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/UuidValidatorTest.php index b5084b6d7f05f..ea5598b216563 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/UuidValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/UuidValidatorTest.php @@ -93,7 +93,7 @@ public static function getValidStrictUuids() */ public function testValidStrictUuidsWithWhitespaces($uuid, $versions = null) { - $constraint = new Uuid(['normalizer' => 'trim']); + $constraint = new Uuid(normalizer: 'trim'); if (null !== $versions) { $constraint->versions = $versions; @@ -131,9 +131,7 @@ public function testValidStrictUuidWithWhitespacesNamed() */ public function testInvalidStrictUuids($uuid, $code, $versions = null) { - $constraint = new Uuid([ - 'message' => 'testMessage', - ]); + $constraint = new Uuid(message: 'testMessage'); if (null !== $versions) { $constraint->versions = $versions; @@ -195,9 +193,7 @@ public static function getInvalidStrictUuids() */ public function testValidNonStrictUuids($uuid) { - $constraint = new Uuid([ - 'strict' => false, - ]); + $constraint = new Uuid(strict: false); $this->validator->validate($uuid, $constraint); @@ -226,10 +222,10 @@ public static function getValidNonStrictUuids() */ public function testInvalidNonStrictUuids($uuid, $code) { - $constraint = new Uuid([ - 'strict' => false, - 'message' => 'myMessage', - ]); + $constraint = new Uuid( + strict: false, + message: 'myMessage', + ); $this->validator->validate($uuid, $constraint); @@ -270,9 +266,7 @@ public function testInvalidNonStrictUuidNamed() */ public function testTimeBasedUuid(string $uid, bool $expectedTimeBased) { - $constraint = new Uuid([ - 'versions' => Uuid::TIME_BASED_VERSIONS, - ]); + $constraint = new Uuid(versions: Uuid::TIME_BASED_VERSIONS); $this->validator->validate($uid, $constraint); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php index c56cdedd50fd6..a862171f193bd 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php @@ -23,7 +23,7 @@ class ValidTest extends TestCase { public function testGroupsCanBeSet() { - $constraint = new Valid(['groups' => 'foo']); + $constraint = new Valid(groups: ['foo']); $this->assertSame(['foo'], $constraint->groups); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php b/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php index 7cfe13f2f2e78..fa71de02e85d5 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/WhenTest.php @@ -24,6 +24,9 @@ final class WhenTest extends TestCase { + /** + * @group legacy + */ public function testMissingOptionsExceptionIsThrown() { $this->expectException(MissingOptionsException::class); @@ -51,10 +54,10 @@ public function testAttributes() self::assertInstanceOf(When::class, $classConstraint); self::assertSame('true', $classConstraint->expression); self::assertEquals([ - new Callback([ - 'callback' => 'callback', - 'groups' => ['Default', 'WhenTestWithAttributes'], - ]), + new Callback( + callback: 'callback', + groups: ['Default', 'WhenTestWithAttributes'], + ), ], $classConstraint->constraints); [$fooConstraint] = $metadata->properties['foo']->getConstraints(); @@ -62,12 +65,8 @@ public function testAttributes() self::assertInstanceOf(When::class, $fooConstraint); self::assertSame('true', $fooConstraint->expression); self::assertEquals([ - new NotNull([ - 'groups' => ['Default', 'WhenTestWithAttributes'], - ]), - new NotBlank([ - 'groups' => ['Default', 'WhenTestWithAttributes'], - ]), + new NotNull(groups: ['Default', 'WhenTestWithAttributes']), + new NotBlank(groups: ['Default', 'WhenTestWithAttributes']), ], $fooConstraint->constraints); self::assertSame(['Default', 'WhenTestWithAttributes'], $fooConstraint->groups); @@ -76,12 +75,8 @@ public function testAttributes() self::assertInstanceOf(When::class, $fooConstraint); self::assertSame('false', $barConstraint->expression); self::assertEquals([ - new NotNull([ - 'groups' => ['foo'], - ]), - new NotBlank([ - 'groups' => ['foo'], - ]), + new NotNull(groups: ['foo']), + new NotBlank(groups: ['foo']), ], $barConstraint->constraints); self::assertSame(['foo'], $barConstraint->groups); @@ -89,11 +84,7 @@ public function testAttributes() self::assertInstanceOf(When::class, $quxConstraint); self::assertSame('true', $quxConstraint->expression); - self::assertEquals([ - new NotNull([ - 'groups' => ['foo'], - ]), - ], $quxConstraint->constraints); + self::assertEquals([new NotNull(groups: ['foo'])], $quxConstraint->constraints); self::assertSame(['foo'], $quxConstraint->groups); [$bazConstraint] = $metadata->getters['baz']->getConstraints(); @@ -101,12 +92,8 @@ public function testAttributes() self::assertInstanceOf(When::class, $bazConstraint); self::assertSame('true', $bazConstraint->expression); self::assertEquals([ - new NotNull([ - 'groups' => ['Default', 'WhenTestWithAttributes'], - ]), - new NotBlank([ - 'groups' => ['Default', 'WhenTestWithAttributes'], - ]), + new NotNull(groups: ['Default', 'WhenTestWithAttributes']), + new NotBlank(groups: ['Default', 'WhenTestWithAttributes']), ], $bazConstraint->constraints); self::assertSame(['Default', 'WhenTestWithAttributes'], $bazConstraint->groups); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php index cba81b25af34a..696d47122d222 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/WhenValidatorTest.php @@ -33,10 +33,10 @@ public function testConstraintsAreExecuted() $this->expectValidateValue(0, 'Foo', $constraints); - $this->validator->validate('Foo', new When([ - 'expression' => 'true', - 'constraints' => $constraints, - ])); + $this->validator->validate('Foo', new When( + expression: 'true', + constraints: $constraints, + )); } public function testConstraintIsExecuted() @@ -44,10 +44,10 @@ public function testConstraintIsExecuted() $constraint = new NotNull(); $this->expectValidateValue(0, 'Foo', [$constraint]); - $this->validator->validate('Foo', new When([ - 'expression' => 'true', - 'constraints' => $constraint, - ])); + $this->validator->validate('Foo', new When( + expression: 'true', + constraints: $constraint, + )); } public function testConstraintsAreExecutedWithNull() @@ -58,10 +58,10 @@ public function testConstraintsAreExecutedWithNull() $this->expectValidateValue(0, null, $constraints); - $this->validator->validate(null, new When([ - 'expression' => 'true', - 'constraints' => $constraints, - ])); + $this->validator->validate(null, new When( + expression: 'true', + constraints: $constraints, + )); } public function testConstraintsAreExecutedWithObject() @@ -79,10 +79,10 @@ public function testConstraintsAreExecutedWithObject() $this->expectValidateValue(0, $number->value, $constraints); - $this->validator->validate($number->value, new When([ - 'expression' => 'this.type === "positive"', - 'constraints' => $constraints, - ])); + $this->validator->validate($number->value, new When( + expression: 'this.type === "positive"', + constraints: $constraints, + )); } public function testConstraintsAreExecutedWithValue() @@ -93,10 +93,10 @@ public function testConstraintsAreExecutedWithValue() $this->expectValidateValue(0, 'foo', $constraints); - $this->validator->validate('foo', new When([ - 'expression' => 'value === "foo"', - 'constraints' => $constraints, - ])); + $this->validator->validate('foo', new When( + expression: 'value === "foo"', + constraints: $constraints, + )); } public function testConstraintsAreExecutedWithExpressionValues() @@ -107,14 +107,14 @@ public function testConstraintsAreExecutedWithExpressionValues() $this->expectValidateValue(0, 'foo', $constraints); - $this->validator->validate('foo', new When([ - 'expression' => 'activated && value === compared_value', - 'constraints' => $constraints, - 'values' => [ + $this->validator->validate('foo', new When( + expression: 'activated && value === compared_value', + constraints: $constraints, + values: [ 'activated' => true, 'compared_value' => 'foo', ], - ])); + )); } public function testConstraintsNotExecuted() @@ -126,10 +126,10 @@ public function testConstraintsNotExecuted() $this->expectNoValidate(); - $this->validator->validate('', new When([ - 'expression' => 'false', - 'constraints' => $constraints, - ])); + $this->validator->validate('', new When( + expression: 'false', + constraints: $constraints, + )); $this->assertNoViolation(); } @@ -149,10 +149,10 @@ public function testConstraintsNotExecutedWithObject() $this->expectNoValidate(); - $this->validator->validate($number->value, new When([ - 'expression' => 'this.type !== "positive"', - 'constraints' => $constraints, - ])); + $this->validator->validate($number->value, new When( + expression: 'this.type !== "positive"', + constraints: $constraints, + )); $this->assertNoViolation(); } @@ -165,10 +165,10 @@ public function testConstraintsNotExecutedWithValue() $this->expectNoValidate(); - $this->validator->validate('foo', new When([ - 'expression' => 'value === null', - 'constraints' => $constraints, - ])); + $this->validator->validate('foo', new When( + expression: 'value === null', + constraints: $constraints, + )); $this->assertNoViolation(); } @@ -181,14 +181,14 @@ public function testConstraintsNotExecutedWithExpressionValues() $this->expectNoValidate(); - $this->validator->validate('foo', new When([ - 'expression' => 'activated && value === compared_value', - 'constraints' => $constraints, - 'values' => [ + $this->validator->validate('foo', new When( + expression: 'activated && value === compared_value', + constraints: $constraints, + values: [ 'activated' => true, 'compared_value' => 'bar', ], - ])); + )); $this->assertNoViolation(); } @@ -196,9 +196,7 @@ public function testConstraintsNotExecutedWithExpressionValues() public function testConstraintViolations() { $constraints = [ - new Blank([ - 'message' => 'my_message', - ]), + new Blank(message: 'my_message'), ]; $this->expectFailingValueValidation( 0, diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php index af90ddc7473ad..3afaaf84317a6 100644 --- a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php +++ b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php @@ -18,6 +18,6 @@ class EntityStaticCar extends EntityStaticVehicle { public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addPropertyConstraint('wheels', new Length(['max' => 99])); + $metadata->addPropertyConstraint('wheels', new Length(max: 99)); } } diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php index d559074db6458..cb0efe2813f37 100644 --- a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php +++ b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php @@ -18,6 +18,6 @@ class EntityStaticCarTurbo extends EntityStaticCar { public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addPropertyConstraint('wheels', new Length(['max' => 99])); + $metadata->addPropertyConstraint('wheels', new Length(max: 99)); } } diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php index 1190318fa555e..429bffdebdf52 100644 --- a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php +++ b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php @@ -20,6 +20,6 @@ class EntityStaticVehicle public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addPropertyConstraint('wheels', new Length(['max' => 99])); + $metadata->addPropertyConstraint('wheels', new Length(max: 99)); } } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php index d2250114ffbff..68f279ecf0e9b 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php @@ -38,8 +38,8 @@ public function testLoadClassMetadataWithInterface() $metadata = $factory->getMetadataFor(self::PARENT_CLASS); $constraints = [ - new ConstraintA(['groups' => ['Default', 'EntityParent']]), - new ConstraintA(['groups' => ['Default', 'EntityInterfaceA', 'EntityParent']]), + new ConstraintA(groups: ['Default', 'EntityParent']), + new ConstraintA(groups: ['Default', 'EntityInterfaceA', 'EntityParent']), ]; $this->assertEquals($constraints, $metadata->getConstraints()); @@ -51,31 +51,31 @@ public function testMergeParentConstraints() $metadata = $factory->getMetadataFor(self::CLASS_NAME); $constraints = [ - new ConstraintA(['groups' => [ + new ConstraintA(groups: [ 'Default', 'Entity', - ]]), - new ConstraintA(['groups' => [ + ]), + new ConstraintA(groups: [ 'Default', 'EntityParent', 'Entity', - ]]), - new ConstraintA(['groups' => [ + ]), + new ConstraintA(groups: [ 'Default', 'EntityInterfaceA', 'EntityParent', 'Entity', - ]]), - new ConstraintA(['groups' => [ + ]), + new ConstraintA(groups: [ 'Default', 'EntityInterfaceB', 'Entity', - ]]), - new ConstraintA(['groups' => [ + ]), + new ConstraintA(groups: [ 'Default', 'EntityParentInterface', 'Entity', - ]]), + ]), ]; $this->assertEquals($constraints, $metadata->getConstraints()); @@ -87,8 +87,8 @@ public function testCachedMetadata() $factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache); $expectedConstraints = [ - new ConstraintA(['groups' => ['Default', 'EntityParent']]), - new ConstraintA(['groups' => ['Default', 'EntityInterfaceA', 'EntityParent']]), + new ConstraintA(groups: ['Default', 'EntityParent']), + new ConstraintA(groups: ['Default', 'EntityInterfaceA', 'EntityParent']), ]; $metadata = $factory->getMetadataFor(self::PARENT_CLASS); diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/AttributeLoaderTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Loader/AttributeLoaderTest.php index f9cb0da9b2d3c..6e55bdb524bae 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Loader/AttributeLoaderTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/AttributeLoaderTest.php @@ -66,29 +66,29 @@ public function testLoadClassMetadata() $expected->addConstraint(new Sequentially([ new Expression('this.getFirstName() != null'), ])); - $expected->addConstraint(new Callback(['callback' => 'validateMe', 'payload' => 'foo'])); + $expected->addConstraint(new Callback(callback: 'validateMe', payload: 'foo')); $expected->addConstraint(new Callback('validateMeStatic')); $expected->addPropertyConstraint('firstName', new NotNull()); - $expected->addPropertyConstraint('firstName', new Range(['min' => 3])); - $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(['min' => 3])])); - $expected->addPropertyConstraint('firstName', new All(['constraints' => [new NotNull(), new Range(['min' => 3])]])); + $expected->addPropertyConstraint('firstName', new Range(min: 3)); + $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(min: 3)])); + $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(min: 3)])); $expected->addPropertyConstraint('firstName', new Collection([ - 'foo' => [new NotNull(), new Range(['min' => 3])], - 'bar' => new Range(['min' => 5]), + 'foo' => [new NotNull(), new Range(min: 3)], + 'bar' => new Range(min: 5), 'baz' => new Required([new Email()]), 'qux' => new Optional([new NotBlank()]), ], null, null, true)); - $expected->addPropertyConstraint('firstName', new Choice([ - 'message' => 'Must be one of %choices%', - 'choices' => ['A', 'B'], - ])); + $expected->addPropertyConstraint('firstName', new Choice( + message: 'Must be one of %choices%', + choices: ['A', 'B'], + )); $expected->addPropertyConstraint('firstName', new AtLeastOneOf([ new NotNull(), - new Range(['min' => 3]), + new Range(min: 3), ], null, null, 'foo', null, false)); $expected->addPropertyConstraint('firstName', new Sequentially([ new NotBlank(), - new Range(['min' => 5]), + new Range(min: 5), ])); $expected->addPropertyConstraint('childA', new Valid()); $expected->addPropertyConstraint('childB', new Valid()); @@ -152,29 +152,29 @@ public function testLoadClassMetadataAndMerge() $expected->addConstraint(new Sequentially([ new Expression('this.getFirstName() != null'), ])); - $expected->addConstraint(new Callback(['callback' => 'validateMe', 'payload' => 'foo'])); + $expected->addConstraint(new Callback(callback: 'validateMe', payload: 'foo')); $expected->addConstraint(new Callback('validateMeStatic')); $expected->addPropertyConstraint('firstName', new NotNull()); - $expected->addPropertyConstraint('firstName', new Range(['min' => 3])); - $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(['min' => 3])])); - $expected->addPropertyConstraint('firstName', new All(['constraints' => [new NotNull(), new Range(['min' => 3])]])); + $expected->addPropertyConstraint('firstName', new Range(min: 3)); + $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(min: 3)])); + $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(min: 3)])); $expected->addPropertyConstraint('firstName', new Collection([ - 'foo' => [new NotNull(), new Range(['min' => 3])], - 'bar' => new Range(['min' => 5]), + 'foo' => [new NotNull(), new Range(min: 3)], + 'bar' => new Range(min: 5), 'baz' => new Required([new Email()]), 'qux' => new Optional([new NotBlank()]), ], null, null, true)); - $expected->addPropertyConstraint('firstName', new Choice([ - 'message' => 'Must be one of %choices%', - 'choices' => ['A', 'B'], - ])); + $expected->addPropertyConstraint('firstName', new Choice( + message: 'Must be one of %choices%', + choices: ['A', 'B'], + )); $expected->addPropertyConstraint('firstName', new AtLeastOneOf([ new NotNull(), - new Range(['min' => 3]), + new Range(min: 3), ], null, null, 'foo', null, false)); $expected->addPropertyConstraint('firstName', new Sequentially([ new NotBlank(), - new Range(['min' => 5]), + new Range(min: 5), ])); $expected->addPropertyConstraint('childA', new Valid()); $expected->addPropertyConstraint('childB', new Valid()); diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php index 5ba519ab195c5..5dd21a088d070 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php @@ -67,18 +67,18 @@ public function testLoadClassMetadata() $expected->addConstraint(new Callback(['Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback'])); $expected->addConstraint(new Traverse(false)); $expected->addPropertyConstraint('firstName', new NotNull()); - $expected->addPropertyConstraint('firstName', new Range(['min' => 3])); + $expected->addPropertyConstraint('firstName', new Range(min: 3)); $expected->addPropertyConstraint('firstName', new Choice(['A', 'B'])); - $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(['min' => 3])])); - $expected->addPropertyConstraint('firstName', new All(['constraints' => [new NotNull(), new Range(['min' => 3])]])); - $expected->addPropertyConstraint('firstName', new Collection(['fields' => [ - 'foo' => [new NotNull(), new Range(['min' => 3])], - 'bar' => [new Range(['min' => 5])], - ]])); - $expected->addPropertyConstraint('firstName', new Choice([ - 'message' => 'Must be one of %choices%', - 'choices' => ['A', 'B'], + $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(min: 3)])); + $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(min: 3)])); + $expected->addPropertyConstraint('firstName', new Collection([ + 'foo' => [new NotNull(), new Range(min: 3)], + 'bar' => [new Range(min: 5)], ])); + $expected->addPropertyConstraint('firstName', new Choice( + message: 'Must be one of %choices%', + choices: ['A', 'B'], + )); $expected->addGetterConstraint('lastName', new NotNull()); $expected->addGetterConstraint('valid', new IsTrue()); $expected->addGetterConstraint('permissions', new IsTrue()); @@ -94,7 +94,7 @@ public function testLoadClassMetadataWithNonStrings() $loader->loadClassMetadata($metadata); $expected = new ClassMetadata(Entity::class); - $expected->addPropertyConstraint('firstName', new Regex(['pattern' => '/^1/', 'match' => false])); + $expected->addPropertyConstraint('firstName', new Regex(pattern: '/^1/', match: false)); $properties = $metadata->getPropertyMetadata('firstName'); $constraints = $properties[0]->getConstraints(); diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Loader/YamlFileLoaderTest.php index 60493787e1ba5..6d620e85a766c 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/YamlFileLoaderTest.php @@ -111,18 +111,18 @@ public function testLoadClassMetadata() $expected->addConstraint(new Callback('validateMeStatic')); $expected->addConstraint(new Callback(['Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback'])); $expected->addPropertyConstraint('firstName', new NotNull()); - $expected->addPropertyConstraint('firstName', new Range(['min' => 3])); + $expected->addPropertyConstraint('firstName', new Range(min: 3)); $expected->addPropertyConstraint('firstName', new Choice(['A', 'B'])); - $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(['min' => 3])])); - $expected->addPropertyConstraint('firstName', new All(['constraints' => [new NotNull(), new Range(['min' => 3])]])); - $expected->addPropertyConstraint('firstName', new Collection(['fields' => [ - 'foo' => [new NotNull(), new Range(['min' => 3])], - 'bar' => [new Range(['min' => 5])], - ]])); - $expected->addPropertyConstraint('firstName', new Choice([ - 'message' => 'Must be one of %choices%', - 'choices' => ['A', 'B'], + $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(min: 3)])); + $expected->addPropertyConstraint('firstName', new All([new NotNull(), new Range(min: 3)])); + $expected->addPropertyConstraint('firstName', new Collection([ + 'foo' => [new NotNull(), new Range(min: 3)], + 'bar' => [new Range(min: 5)], ])); + $expected->addPropertyConstraint('firstName', new Choice( + message: 'Must be one of %choices%', + choices: ['A', 'B'], + )); $expected->addGetterConstraint('lastName', new NotNull()); $expected->addGetterConstraint('valid', new IsTrue()); $expected->addGetterConstraint('permissions', new IsTrue()); @@ -138,7 +138,7 @@ public function testLoadClassMetadataWithConstants() $loader->loadClassMetadata($metadata); $expected = new ClassMetadata(Entity::class); - $expected->addPropertyConstraint('firstName', new Range(['max' => \PHP_INT_MAX])); + $expected->addPropertyConstraint('firstName', new Range(max: \PHP_INT_MAX)); $this->assertEquals($expected, $metadata); } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/MemberMetadataTest.php b/src/Symfony/Component/Validator/Tests/Mapping/MemberMetadataTest.php index cd429e6769a7e..84d047f102dbc 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/MemberMetadataTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/MemberMetadataTest.php @@ -84,7 +84,7 @@ public function testSerialize() public function testSerializeCollectionCascaded() { - $this->metadata->addConstraint(new Valid(['traverse' => true])); + $this->metadata->addConstraint(new Valid(traverse: true)); $metadata = unserialize(serialize($this->metadata)); @@ -93,7 +93,7 @@ public function testSerializeCollectionCascaded() public function testSerializeCollectionNotCascaded() { - $this->metadata->addConstraint(new Valid(['traverse' => false])); + $this->metadata->addConstraint(new Valid(traverse: false)); $metadata = unserialize(serialize($this->metadata)); diff --git a/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php b/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php index ee183a1bfdf15..2db40c86cd973 100644 --- a/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php @@ -113,10 +113,10 @@ public function testValidate() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $constraint = new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ]); + $constraint = new Callback( + callback: $callback, + groups: ['Group'], + ); $violations = $this->validate('Bernhard', $constraint, 'Group'); @@ -149,10 +149,10 @@ public function testClassConstraint() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -188,10 +188,10 @@ public function testPropertyConstraint() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addPropertyConstraint('firstName', new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addPropertyConstraint('firstName', new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -227,10 +227,10 @@ public function testGetterConstraint() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addGetterConstraint('lastName', new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addGetterConstraint('lastName', new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -264,10 +264,10 @@ public function testArray() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($array, null, 'Group'); @@ -301,10 +301,10 @@ public function testRecursiveArray() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($array, null, 'Group'); @@ -338,10 +338,10 @@ public function testTraversable() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($traversable, null, 'Group'); @@ -377,10 +377,10 @@ public function testRecursiveTraversable() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($traversable, null, 'Group'); @@ -415,10 +415,10 @@ public function testReferenceClassConstraint() }; $this->metadata->addPropertyConstraint('reference', new Valid()); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -456,10 +456,10 @@ public function testReferencePropertyConstraint() }; $this->metadata->addPropertyConstraint('reference', new Valid()); - $this->referenceMetadata->addPropertyConstraint('value', new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->referenceMetadata->addPropertyConstraint('value', new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -497,10 +497,10 @@ public function testReferenceGetterConstraint() }; $this->metadata->addPropertyConstraint('reference', new Valid()); - $this->referenceMetadata->addPropertyConstraint('privateValue', new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->referenceMetadata->addPropertyConstraint('privateValue', new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -563,10 +563,10 @@ public function testArrayReference($constraintMethod) }; $this->metadata->$constraintMethod('reference', new Valid()); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -604,10 +604,10 @@ public function testRecursiveArrayReference($constraintMethod) }; $this->metadata->$constraintMethod('reference', new Valid()); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -632,14 +632,14 @@ public function testOnlyCascadedArraysAreTraversed() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addPropertyConstraint('reference', new Callback([ - 'callback' => function () {}, - 'groups' => 'Group', - ])); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addPropertyConstraint('reference', new Callback( + callback: function () {}, + groups: ['Group'], + )); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -659,9 +659,9 @@ public function testArrayTraversalCannotBeDisabled($constraintMethod) $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->$constraintMethod('reference', new Valid([ - 'traverse' => false, - ])); + $this->metadata->$constraintMethod('reference', new Valid( + traverse: false, + )); $this->referenceMetadata->addConstraint(new Callback($callback)); $violations = $this->validate($entity); @@ -682,9 +682,9 @@ public function testRecursiveArrayTraversalCannotBeDisabled($constraintMethod) $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->$constraintMethod('reference', new Valid([ - 'traverse' => false, - ])); + $this->metadata->$constraintMethod('reference', new Valid( + traverse: false, + )); $this->referenceMetadata->addConstraint(new Callback($callback)); @@ -745,10 +745,10 @@ public function testTraversableReference() }; $this->metadata->addPropertyConstraint('reference', new Valid()); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -774,9 +774,9 @@ public function testDisableTraversableTraversal() }; $this->metadataFactory->addMetadata(new ClassMetadata('ArrayIterator')); - $this->metadata->addPropertyConstraint('reference', new Valid([ - 'traverse' => false, - ])); + $this->metadata->addPropertyConstraint('reference', new Valid( + traverse: false, + )); $this->referenceMetadata->addConstraint(new Callback($callback)); $violations = $this->validate($entity); @@ -790,9 +790,9 @@ public function testMetadataMustExistIfTraversalIsDisabled() $entity = new Entity(); $entity->reference = new \ArrayIterator(); - $this->metadata->addPropertyConstraint('reference', new Valid([ - 'traverse' => false, - ])); + $this->metadata->addPropertyConstraint('reference', new Valid( + traverse: false, + )); $this->expectException(NoSuchMetadataException::class); @@ -819,13 +819,13 @@ public function testEnableRecursiveTraversableTraversal() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addPropertyConstraint('reference', new Valid([ - 'traverse' => true, - ])); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addPropertyConstraint('reference', new Valid( + traverse: true, + )); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, null, 'Group'); @@ -866,14 +866,14 @@ public function testValidateProperty() $context->addViolation('Other violation'); }; - $this->metadata->addPropertyConstraint('firstName', new Callback([ - 'callback' => $callback1, - 'groups' => 'Group', - ])); - $this->metadata->addPropertyConstraint('lastName', new Callback([ - 'callback' => $callback2, - 'groups' => 'Group', - ])); + $this->metadata->addPropertyConstraint('firstName', new Callback( + callback: $callback1, + groups: ['Group'], + )); + $this->metadata->addPropertyConstraint('lastName', new Callback( + callback: $callback2, + groups: ['Group'], + )); $violations = $this->validateProperty($entity, 'firstName', 'Group'); @@ -924,14 +924,14 @@ public function testValidatePropertyValue() $context->addViolation('Other violation'); }; - $this->metadata->addPropertyConstraint('firstName', new Callback([ - 'callback' => $callback1, - 'groups' => 'Group', - ])); - $this->metadata->addPropertyConstraint('lastName', new Callback([ - 'callback' => $callback2, - 'groups' => 'Group', - ])); + $this->metadata->addPropertyConstraint('firstName', new Callback( + callback: $callback1, + groups: ['Group'], + )); + $this->metadata->addPropertyConstraint('lastName', new Callback( + callback: $callback2, + groups: ['Group'], + )); $violations = $this->validatePropertyValue( $entity, @@ -973,14 +973,14 @@ public function testValidatePropertyValueWithClassName() $context->addViolation('Other violation'); }; - $this->metadata->addPropertyConstraint('firstName', new Callback([ - 'callback' => $callback1, - 'groups' => 'Group', - ])); - $this->metadata->addPropertyConstraint('lastName', new Callback([ - 'callback' => $callback2, - 'groups' => 'Group', - ])); + $this->metadata->addPropertyConstraint('firstName', new Callback( + callback: $callback1, + groups: ['Group'], + )); + $this->metadata->addPropertyConstraint('lastName', new Callback( + callback: $callback2, + groups: ['Group'], + )); $violations = $this->validatePropertyValue( self::ENTITY_CLASS, @@ -1060,14 +1060,14 @@ public function testValidateSingleGroup() $context->addViolation('Message'); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group 1', - ])); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group 2', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group 1'], + )); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group 2'], + )); $violations = $this->validate($entity, null, 'Group 2'); @@ -1083,14 +1083,14 @@ public function testValidateMultipleGroups() $context->addViolation('Message'); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group 1', - ])); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group 2', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group 1'], + )); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group 2'], + )); $violations = $this->validate($entity, null, ['Group 1', 'Group 2']); @@ -1109,18 +1109,18 @@ public function testReplaceDefaultGroupByGroupSequenceObject() $context->addViolation('Violation in Group 3'); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => function () {}, - 'groups' => 'Group 1', - ])); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback1, - 'groups' => 'Group 2', - ])); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback2, - 'groups' => 'Group 3', - ])); + $this->metadata->addConstraint(new Callback( + callback: function () {}, + groups: ['Group 1'], + )); + $this->metadata->addConstraint(new Callback( + callback: $callback1, + groups: ['Group 2'], + )); + $this->metadata->addConstraint(new Callback( + callback: $callback2, + groups: ['Group 3'], + )); $sequence = new GroupSequence(['Group 1', 'Group 2', 'Group 3', 'Entity']); $this->metadata->setGroupSequence($sequence); @@ -1143,18 +1143,18 @@ public function testReplaceDefaultGroupByGroupSequenceArray() $context->addViolation('Violation in Group 3'); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => function () {}, - 'groups' => 'Group 1', - ])); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback1, - 'groups' => 'Group 2', - ])); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback2, - 'groups' => 'Group 3', - ])); + $this->metadata->addConstraint(new Callback( + callback: function () {}, + groups: ['Group 1'], + )); + $this->metadata->addConstraint(new Callback( + callback: $callback1, + groups: ['Group 2'], + )); + $this->metadata->addConstraint(new Callback( + callback: $callback2, + groups: ['Group 3'], + )); $sequence = ['Group 1', 'Group 2', 'Group 3', 'Entity']; $this->metadata->setGroupSequence($sequence); @@ -1179,14 +1179,14 @@ public function testPropagateDefaultGroupToReferenceWhenReplacingDefaultGroup() }; $this->metadata->addPropertyConstraint('reference', new Valid()); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback1, - 'groups' => 'Default', - ])); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback2, - 'groups' => 'Group 1', - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback1, + groups: ['Default'], + )); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback2, + groups: ['Group 1'], + )); $sequence = new GroupSequence(['Group 1', 'Entity']); $this->metadata->setGroupSequence($sequence); @@ -1209,14 +1209,14 @@ public function testValidateCustomGroupWhenDefaultGroupWasReplaced() $context->addViolation('Violation in group sequence'); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback1, - 'groups' => 'Other Group', - ])); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback2, - 'groups' => 'Group 1', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback1, + groups: ['Other Group'], + )); + $this->metadata->addConstraint(new Callback( + callback: $callback2, + groups: ['Group 1'], + )); $sequence = new GroupSequence(['Group 1', 'Entity']); $this->metadata->setGroupSequence($sequence); @@ -1243,18 +1243,18 @@ public function testReplaceDefaultGroup($sequence, array $assertViolations) }; $metadata = new ClassMetadata($entity::class); - $metadata->addConstraint(new Callback([ - 'callback' => function () {}, - 'groups' => 'Group 1', - ])); - $metadata->addConstraint(new Callback([ - 'callback' => $callback1, - 'groups' => 'Group 2', - ])); - $metadata->addConstraint(new Callback([ - 'callback' => $callback2, - 'groups' => 'Group 3', - ])); + $metadata->addConstraint(new Callback( + callback: function () {}, + groups: ['Group 1'], + )); + $metadata->addConstraint(new Callback( + callback: $callback1, + groups: ['Group 2'], + )); + $metadata->addConstraint(new Callback( + callback: $callback2, + groups: ['Group 3'], + )); $metadata->setGroupSequenceProvider(true); $this->metadataFactory->addMetadata($metadata); @@ -1348,18 +1348,18 @@ public function testGroupSequenceAbortsAfterFailedGroup() $context->addViolation('Message 2'); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => function () {}, - 'groups' => 'Group 1', - ])); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback1, - 'groups' => 'Group 2', - ])); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback2, - 'groups' => 'Group 3', - ])); + $this->metadata->addConstraint(new Callback( + callback: function () {}, + groups: ['Group 1'], + )); + $this->metadata->addConstraint(new Callback( + callback: $callback1, + groups: ['Group 2'], + )); + $this->metadata->addConstraint(new Callback( + callback: $callback2, + groups: ['Group 3'], + )); $sequence = new GroupSequence(['Group 1', 'Group 2', 'Group 3']); $violations = $this->validator->validate($entity, new Valid(), $sequence); @@ -1382,14 +1382,14 @@ public function testGroupSequenceIncludesReferences() }; $this->metadata->addPropertyConstraint('reference', new Valid()); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback1, - 'groups' => 'Group 1', - ])); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback2, - 'groups' => 'Group 2', - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback1, + groups: ['Group 1'], + )); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback2, + groups: ['Group 2'], + )); $sequence = new GroupSequence(['Group 1', 'Entity']); $violations = $this->validator->validate($entity, new Valid(), $sequence); @@ -1442,14 +1442,14 @@ public function testValidateInSeparateContext() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback1, - 'groups' => 'Group', - ])); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback2, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback1, + groups: ['Group'], + )); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback2, + groups: ['Group'], + )); $violations = $this->validator->validate($entity, new Valid(), 'Group'); @@ -1498,14 +1498,14 @@ public function testValidateInContext() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback1, - 'groups' => 'Group', - ])); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback2, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback1, + groups: ['Group'], + )); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback2, + groups: ['Group'], + )); $violations = $this->validator->validate($entity, new Valid(), 'Group'); @@ -1561,14 +1561,14 @@ public function testValidateArrayInContext() $context->addViolation('Message %param%', ['%param%' => 'value']); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback1, - 'groups' => 'Group', - ])); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback2, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback1, + groups: ['Group'], + )); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback2, + groups: ['Group'], + )); $violations = $this->validator->validate($entity, new Valid(), 'Group'); @@ -1603,10 +1603,10 @@ public function testTraverseTraversableByDefault() }; $this->metadataFactory->addMetadata(new ClassMetadata('ArrayIterator')); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($traversable, new Valid(), 'Group'); @@ -1635,10 +1635,10 @@ public function testTraversalEnabledOnClass() $traversableMetadata->addConstraint(new Traverse(true)); $this->metadataFactory->addMetadata($traversableMetadata); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($traversable, new Valid(), 'Group'); @@ -1659,10 +1659,10 @@ public function testTraversalDisabledOnClass() $traversableMetadata->addConstraint(new Traverse(false)); $this->metadataFactory->addMetadata($traversableMetadata); - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($traversable, new Valid(), 'Group'); @@ -1692,10 +1692,10 @@ public function testReferenceTraversalDisabledOnClass() $traversableMetadata->addConstraint(new Traverse(false)); $this->metadataFactory->addMetadata($traversableMetadata); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $this->metadata->addPropertyConstraint('reference', new Valid()); $violations = $this->validate($entity, new Valid(), 'Group'); @@ -1717,13 +1717,13 @@ public function testReferenceTraversalEnabledOnReferenceDisabledOnClass() $traversableMetadata->addConstraint(new Traverse(false)); $this->metadataFactory->addMetadata($traversableMetadata); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); - $this->metadata->addPropertyConstraint('reference', new Valid([ - 'traverse' => true, - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); + $this->metadata->addPropertyConstraint('reference', new Valid( + traverse: true, + )); $violations = $this->validate($entity, new Valid(), 'Group'); @@ -1744,13 +1744,13 @@ public function testReferenceTraversalDisabledOnReferenceEnabledOnClass() $traversableMetadata->addConstraint(new Traverse(true)); $this->metadataFactory->addMetadata($traversableMetadata); - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); - $this->metadata->addPropertyConstraint('reference', new Valid([ - 'traverse' => false, - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); + $this->metadata->addPropertyConstraint('reference', new Valid( + traverse: false, + )); $violations = $this->validate($entity, new Valid(), 'Group'); @@ -1767,10 +1767,10 @@ public function testReferenceCascadeDisabledByDefault() $this->fail('Should not be called'); }; - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, new Valid(), 'Group'); @@ -1789,10 +1789,10 @@ public function testReferenceCascadeEnabledIgnoresUntyped() $this->fail('Should not be called'); }; - $this->referenceMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $this->referenceMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $violations = $this->validate($entity, new Valid(), 'Group'); @@ -1816,10 +1816,10 @@ public function testTypedReferenceCascadeEnabled() $cascadingMetadata->addConstraint(new Cascade()); $cascadedMetadata = new ClassMetadata(CascadedChild::class); - $cascadedMetadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => 'Group', - ])); + $cascadedMetadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group'], + )); $this->metadataFactory->addMetadata($cascadingMetadata); $this->metadataFactory->addMetadata($cascadedMetadata); @@ -1868,10 +1868,10 @@ public function testNoDuplicateValidationIfClassConstraintInMultipleGroups() $context->addViolation('Message'); }; - $this->metadata->addConstraint(new Callback([ - 'callback' => $callback, - 'groups' => ['Group 1', 'Group 2'], - ])); + $this->metadata->addConstraint(new Callback( + callback: $callback, + groups: ['Group 1', 'Group 2'], + )); $violations = $this->validator->validate($entity, new Valid(), ['Group 1', 'Group 2']); @@ -1887,10 +1887,10 @@ public function testNoDuplicateValidationIfPropertyConstraintInMultipleGroups() $context->addViolation('Message'); }; - $this->metadata->addPropertyConstraint('firstName', new Callback([ - 'callback' => $callback, - 'groups' => ['Group 1', 'Group 2'], - ])); + $this->metadata->addPropertyConstraint('firstName', new Callback( + callback: $callback, + groups: ['Group 1', 'Group 2'], + )); $violations = $this->validator->validate($entity, new Valid(), ['Group 1', 'Group 2']); @@ -2007,8 +2007,8 @@ public function testNestedObjectIsNotValidatedIfGroupInValidConstraintIsNotValid $reference->value = ''; $entity->childA = $reference; - $this->metadata->addPropertyConstraint('firstName', new NotBlank(['groups' => 'group1'])); - $this->metadata->addPropertyConstraint('childA', new Valid(['groups' => 'group1'])); + $this->metadata->addPropertyConstraint('firstName', new NotBlank(groups: ['group1'])); + $this->metadata->addPropertyConstraint('childA', new Valid(groups: ['group1'])); $this->referenceMetadata->addPropertyConstraint('value', new NotBlank()); $violations = $this->validator->validate($entity, null, []); @@ -2024,9 +2024,9 @@ public function testNestedObjectIsValidatedIfGroupInValidConstraintIsValidated() $reference->value = ''; $entity->childA = $reference; - $this->metadata->addPropertyConstraint('firstName', new NotBlank(['groups' => 'group1'])); - $this->metadata->addPropertyConstraint('childA', new Valid(['groups' => 'group1'])); - $this->referenceMetadata->addPropertyConstraint('value', new NotBlank(['groups' => 'group1'])); + $this->metadata->addPropertyConstraint('firstName', new NotBlank(groups: ['group1'])); + $this->metadata->addPropertyConstraint('childA', new Valid(groups: ['group1'])); + $this->referenceMetadata->addPropertyConstraint('value', new NotBlank(groups: ['group1'])); $violations = $this->validator->validate($entity, null, ['Default', 'group1']); @@ -2044,10 +2044,10 @@ public function testNestedObjectIsValidatedInMultipleGroupsIfGroupInValidConstra $entity->childA = $reference; $this->metadata->addPropertyConstraint('firstName', new NotBlank()); - $this->metadata->addPropertyConstraint('childA', new Valid(['groups' => ['group1', 'group2']])); + $this->metadata->addPropertyConstraint('childA', new Valid(groups: ['group1', 'group2'])); - $this->referenceMetadata->addPropertyConstraint('value', new NotBlank(['groups' => 'group1'])); - $this->referenceMetadata->addPropertyConstraint('value', new NotNull(['groups' => 'group2'])); + $this->referenceMetadata->addPropertyConstraint('value', new NotBlank(groups: ['group1'])); + $this->referenceMetadata->addPropertyConstraint('value', new NotNull(groups: ['group2'])); $violations = $this->validator->validate($entity, null, ['Default', 'group1', 'group2']); @@ -2136,10 +2136,10 @@ public function testRelationBetweenChildAAndChildB() public function testCollectionConstraintValidateAllGroupsForNestedConstraints() { - $this->metadata->addPropertyConstraint('data', new Collection(['fields' => [ - 'one' => [new NotBlank(['groups' => 'one']), new Length(['min' => 2, 'groups' => 'two'])], - 'two' => [new NotBlank(['groups' => 'two'])], - ]])); + $this->metadata->addPropertyConstraint('data', new Collection(fields: [ + 'one' => [new NotBlank(groups: ['one']), new Length(min: 2, groups: ['two'])], + 'two' => [new NotBlank(groups: ['two'])], + ])); $entity = new Entity(); $entity->data = ['one' => 't', 'two' => '']; @@ -2154,9 +2154,9 @@ public function testCollectionConstraintValidateAllGroupsForNestedConstraints() public function testGroupedMethodConstraintValidateInSequence() { $metadata = new ClassMetadata(EntityWithGroupedConstraintOnMethods::class); - $metadata->addPropertyConstraint('bar', new NotNull(['groups' => 'Foo'])); - $metadata->addGetterMethodConstraint('validInFoo', 'isValidInFoo', new IsTrue(['groups' => 'Foo'])); - $metadata->addGetterMethodConstraint('bar', 'getBar', new NotNull(['groups' => 'Bar'])); + $metadata->addPropertyConstraint('bar', new NotNull(groups: ['Foo'])); + $metadata->addGetterMethodConstraint('validInFoo', 'isValidInFoo', new IsTrue(groups: ['Foo'])); + $metadata->addGetterMethodConstraint('bar', 'getBar', new NotNull(groups: ['Bar'])); $this->metadataFactory->addMetadata($metadata); @@ -2197,10 +2197,13 @@ public function testNotNullConstraintOnGetterReturningNull() public function testAllConstraintValidateAllGroupsForNestedConstraints() { - $this->metadata->addPropertyConstraint('data', new All(['constraints' => [ - new NotBlank(['groups' => 'one']), - new Length(['min' => 2, 'groups' => 'two']), - ]])); + $this->metadata->addPropertyConstraint('data', new All([ + new NotBlank(groups: ['one']), + new Length( + min: 2, + groups: ['two'], + ), + ])); $entity = new Entity(); $entity->data = ['one' => 't', 'two' => '']; @@ -2330,8 +2333,8 @@ public function testValidateWithExplicitCascade() public function testValidatedConstraintsHashesDoNotCollide() { $metadata = new ClassMetadata(Entity::class); - $metadata->addPropertyConstraint('initialized', new NotNull(['groups' => 'should_pass'])); - $metadata->addPropertyConstraint('initialized', new IsNull(['groups' => 'should_fail'])); + $metadata->addPropertyConstraint('initialized', new NotNull(groups: ['should_pass'])); + $metadata->addPropertyConstraint('initialized', new IsNull(groups: ['should_fail'])); $this->metadataFactory->addMetadata($metadata);