diff --git a/CHANGELOG.md b/CHANGELOG.md index c79c732ff..a8bb4092b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * Deprecated the `checkDNS` and `dnsMessage` options of the `Url` constraint. * added a `values` option to the `Expression` constraint + * Deprecated use of `Locale` constraint without setting `true` at "canonicalize" option, which will be the default value in 5.0 4.0.0 ----- diff --git a/Constraints/Locale.php b/Constraints/Locale.php index 0ee4bf65b..076850b1f 100644 --- a/Constraints/Locale.php +++ b/Constraints/Locale.php @@ -29,4 +29,13 @@ class Locale extends Constraint public $message = 'This value is not a valid locale.'; public $canonicalize = false; + + public function __construct($options = null) + { + if (!($options['canonicalize'] ?? false)) { + @trigger_error('The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead.', E_USER_DEPRECATED); + } + + parent::__construct($options); + } } diff --git a/Constraints/LocaleValidator.php b/Constraints/LocaleValidator.php index f3a3516a1..66421ecad 100644 --- a/Constraints/LocaleValidator.php +++ b/Constraints/LocaleValidator.php @@ -40,7 +40,8 @@ public function validate($value, Constraint $constraint) throw new UnexpectedTypeException($value, 'string'); } - $value = (string) $value; + $inputValue = (string) $value; + $value = $inputValue; if ($constraint->canonicalize) { $value = \Locale::canonicalize($value); } @@ -49,7 +50,7 @@ public function validate($value, Constraint $constraint) if (!isset($locales[$value]) && !in_array($value, $localeBundle->getAliases(), true)) { $this->context->buildViolation($constraint->message) - ->setParameter('{{ value }}', $this->formatValue($value)) + ->setParameter('{{ value }}', $this->formatValue($inputValue)) ->setCode(Locale::NO_SUCH_LOCALE_ERROR) ->addViolation(); } diff --git a/Tests/Constraints/LocaleValidatorTest.php b/Tests/Constraints/LocaleValidatorTest.php index 7ebe1cdc8..b9f23db8a 100644 --- a/Tests/Constraints/LocaleValidatorTest.php +++ b/Tests/Constraints/LocaleValidatorTest.php @@ -22,47 +22,123 @@ protected function createValidator() return new LocaleValidator(); } - public function testNullIsValid() + /** + * @group legacy + * @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead. + * + * @dataProvider getValidLocales + */ + public function testLegacyNullIsValid() { $this->validator->validate(null, new Locale()); $this->assertNoViolation(); } - public function testEmptyStringIsValid() + public function testNullIsValid() + { + $this->validator->validate(null, new Locale(array('canonicalize' => true))); + + $this->assertNoViolation(); + } + + /** + * @group legacy + * @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead. + * + * @dataProvider getValidLocales + */ + public function testLegacyEmptyStringIsValid() { $this->validator->validate('', new Locale()); $this->assertNoViolation(); } + public function testEmptyStringIsValid() + { + $this->validator->validate('', new Locale(array('canonicalize' => true))); + + $this->assertNoViolation(); + } + /** + * @group legacy + * @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException */ - public function testExpectsStringCompatibleType() + public function testLegacyExpectsStringCompatibleType() { $this->validator->validate(new \stdClass(), new Locale()); } /** + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + */ + public function testExpectsStringCompatibleType() + { + $this->validator->validate(new \stdClass(), new Locale(array('canonicalize' => true))); + } + + /** + * @group legacy + * @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead. + * * @dataProvider getValidLocales */ - public function testValidLocales($locale) + public function testLegacyValidLocales(string $locale) { $this->validator->validate($locale, new Locale()); $this->assertNoViolation(); } + /** + * @dataProvider getValidLocales + */ + public function testValidLocales($locale, array $options) + { + $this->validator->validate($locale, new Locale($options)); + + $this->assertNoViolation(); + } + public function getValidLocales() { return array( - array('en'), - array('en_US'), - array('pt'), - array('pt_PT'), - array('zh_Hans'), - array('fil_PH'), + array('en', array('canonicalize' => true)), + array('en_US', array('canonicalize' => true)), + array('pt', array('canonicalize' => true)), + array('pt_PT', array('canonicalize' => true)), + array('zh_Hans', array('canonicalize' => true)), + array('fil_PH', array('canonicalize' => true)), + ); + } + + /** + * @group legacy + * @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead. + * @dataProvider getLegacyInvalidLocales + */ + public function testLegacyInvalidLocales(string $locale) + { + $constraint = new Locale(array( + 'message' => 'myMessage', + )); + + $this->validator->validate($locale, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '"'.$locale.'"') + ->setCode(Locale::NO_SUCH_LOCALE_ERROR) + ->assertRaised(); + } + + public function getLegacyInvalidLocales() + { + return array( + array('EN'), + array('foobar'), ); } @@ -73,6 +149,7 @@ public function testInvalidLocales($locale) { $constraint = new Locale(array( 'message' => 'myMessage', + 'canonicalize' => true, )); $this->validator->validate($locale, $constraint); @@ -86,12 +163,14 @@ public function testInvalidLocales($locale) public function getInvalidLocales() { return array( - array('EN'), + array('baz'), array('foobar'), ); } /** + * @group legacy + * @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead. * @dataProvider getUncanonicalizedLocales */ public function testInvalidLocalesWithoutCanonicalization(string $locale)