Skip to content

Commit 2229cec

Browse files
committed
Deprecate use of Locale validation constraint without setting "canonicalize" option to true
1 parent 89d1b65 commit 2229cec

File tree

4 files changed

+102
-13
lines changed

4 files changed

+102
-13
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Deprecated the `checkDNS` and `dnsMessage` options of the `Url` constraint. They will be removed in 5.0.
88
* added a `values` option to the `Expression` constraint
9+
* Deprecated use of `Locale` constraint without setting `true` at "canonicalize" option, which will be the default value in 5.0
910

1011
4.0.0
1112
-----

src/Symfony/Component/Validator/Constraints/Locale.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ class Locale extends Constraint
2929

3030
public $message = 'This value is not a valid locale.';
3131
public $canonicalize = false;
32+
33+
public function __construct($options = null)
34+
{
35+
if (!($options['canonicalize'] ?? false)) {
36+
@trigger_error('"canonicalize" option with value `false` is deprecated since Symfony 4.1 and will be removed in 5.0. Use "canonicalize"=>true instead.', E_USER_DEPRECATED);
37+
}
38+
39+
parent::__construct($options);
40+
}
3241
}

src/Symfony/Component/Validator/Constraints/LocaleValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class LocaleValidator extends ConstraintValidator
3030
public function validate($value, Constraint $constraint)
3131
{
3232
if (!$constraint instanceof Locale) {
33-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Locale');
33+
throw new UnexpectedTypeException($constraint, Locale::class);
3434
}
3535

3636
if (null === $value || '' === $value) {

src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,57 +22,134 @@ protected function createValidator()
2222
return new LocaleValidator();
2323
}
2424

25-
public function testNullIsValid()
25+
/**
26+
* @group legacy
27+
* @expectedDeprecation "canonicalize" option with value `false` is deprecated since Symfony 4.1 and will be removed in 5.0. Use "canonicalize"=>true instead.
28+
*
29+
* @dataProvider getValidLocales
30+
*/
31+
public function testLegacyNullIsValid()
2632
{
2733
$this->validator->validate(null, new Locale());
2834

2935
$this->assertNoViolation();
3036
}
3137

32-
public function testEmptyStringIsValid()
38+
public function testNullIsValid()
39+
{
40+
$this->validator->validate(null, new Locale(array('canonicalize' => true)));
41+
42+
$this->assertNoViolation();
43+
}
44+
45+
/**
46+
* @group legacy
47+
* @expectedDeprecation "canonicalize" option with value `false` is deprecated since Symfony 4.1 and will be removed in 5.0. Use "canonicalize"=>true instead.
48+
*
49+
* @dataProvider getValidLocales
50+
*/
51+
public function testLegacyEmptyStringIsValid()
3352
{
3453
$this->validator->validate('', new Locale());
3554

3655
$this->assertNoViolation();
3756
}
3857

58+
public function testEmptyStringIsValid()
59+
{
60+
$this->validator->validate('', new Locale(array('canonicalize' => true)));
61+
62+
$this->assertNoViolation();
63+
}
64+
3965
/**
66+
* @group legacy
67+
* @expectedDeprecation "canonicalize" option with value `false` is deprecated since Symfony 4.1 and will be removed in 5.0. Use "canonicalize"=>true instead.
4068
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
4169
*/
42-
public function testExpectsStringCompatibleType()
70+
public function testLegacyExpectsStringCompatibleType()
4371
{
4472
$this->validator->validate(new \stdClass(), new Locale());
4573
}
4674

4775
/**
76+
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
77+
*/
78+
public function testExpectsStringCompatibleType()
79+
{
80+
$this->validator->validate(new \stdClass(), new Locale(array('canonicalize' => true)));
81+
}
82+
83+
/**
84+
* @group legacy
85+
* @expectedDeprecation "canonicalize" option with value `false` is deprecated since Symfony 4.1 and will be removed in 5.0. Use "canonicalize"=>true instead.
86+
*
4887
* @dataProvider getValidLocales
4988
*/
50-
public function testValidLocales($locale)
89+
public function testLegacyValidLocales($locale)
5190
{
5291
$this->validator->validate($locale, new Locale());
5392

5493
$this->assertNoViolation();
5594
}
5695

96+
/**
97+
* @dataProvider getValidLocales
98+
*/
99+
public function testValidLocales(string $locale, array $options)
100+
{
101+
$this->validator->validate($locale, new Locale($options));
102+
103+
$this->assertNoViolation();
104+
}
105+
57106
public function getValidLocales()
58107
{
59108
return array(
60-
array('en'),
61-
array('en_US'),
62-
array('pt'),
63-
array('pt_PT'),
64-
array('zh_Hans'),
65-
array('fil_PH'),
109+
array('en', array('canonicalize' => true)),
110+
array('en_US', array('canonicalize' => true)),
111+
array('pt', array('canonicalize' => true)),
112+
array('pt_PT', array('canonicalize' => true)),
113+
array('zh_Hans', array('canonicalize' => true)),
114+
array('fil_PH', array('canonicalize' => true)),
115+
);
116+
}
117+
118+
/**
119+
* @group legacy
120+
* @expectedDeprecation "canonicalize" option with value `false` is deprecated since Symfony 4.1 and will be removed in 5.0. Use "canonicalize"=>true instead.
121+
* @dataProvider getLegacyInvalidLocales
122+
*/
123+
public function testLegacyInvalidLocales($locale)
124+
{
125+
$constraint = new Locale(array(
126+
'message' => 'myMessage',
127+
));
128+
129+
$this->validator->validate($locale, $constraint);
130+
131+
$this->buildViolation('myMessage')
132+
->setParameter('{{ value }}', '"'.$locale.'"')
133+
->setCode(Locale::NO_SUCH_LOCALE_ERROR)
134+
->assertRaised();
135+
}
136+
137+
public function getLegacyInvalidLocales()
138+
{
139+
return array(
140+
array('EN'),
141+
array('foobar'),
66142
);
67143
}
68144

69145
/**
70146
* @dataProvider getInvalidLocales
71147
*/
72-
public function testInvalidLocales($locale)
148+
public function testInvalidLocales(string $locale)
73149
{
74150
$constraint = new Locale(array(
75151
'message' => 'myMessage',
152+
'canonicalize' => true,
76153
));
77154

78155
$this->validator->validate($locale, $constraint);
@@ -86,12 +163,14 @@ public function testInvalidLocales($locale)
86163
public function getInvalidLocales()
87164
{
88165
return array(
89-
array('EN'),
166+
array('baz'),
90167
array('foobar'),
91168
);
92169
}
93170

94171
/**
172+
* @group legacy
173+
* @expectedDeprecation "canonicalize" option with value `false` is deprecated since Symfony 4.1 and will be removed in 5.0. Use "canonicalize"=>true instead.
95174
* @dataProvider getUncanonicalizedLocales
96175
*/
97176
public function testInvalidLocalesWithoutCanonicalization(string $locale)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy