Skip to content

Commit 7e9c812

Browse files
committed
prepare for changing the default value of the requireTld option
1 parent 0a9cba3 commit 7e9c812

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

UPGRADE-7.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ TwigBundle
6060
Validator
6161
---------
6262

63+
* Deprecate not passing a value for the `requireTld` option to the `Url` constraint (the default value will become `true` in 8.0)
6364
* Deprecate `Bic::INVALID_BANK_CODE_ERROR`
6465

6566
Workflow

src/Symfony/Component/Validator/CHANGELOG.md

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

7+
* Deprecate not passing a value for the `requireTld` option to the `Url` constraint (the default value will become `true` in 8.0)
78
* Add the calculated strength to violations in `PasswordStrengthValidator`
89
* Add support for `Stringable` values when using the `Cidr`, `CssColor`, `ExpressionSyntax` and `PasswordStrength` constraints
910
* Add `MacAddress` constraint

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function __construct(
5757
) {
5858
parent::__construct($options, $groups, $payload);
5959

60+
if (null === ($options['requireTld'] ?? $requireTld)) {
61+
trigger_deprecation('symfony/validator', '7.1', 'Not passing a value for the "requireTld" option to the Url constraint is deprecated. Its default value will change to "true".');
62+
}
63+
6064
$this->message = $message ?? $this->message;
6165
$this->protocols = $protocols ?? $this->protocols;
6266
$this->relativeProtocol = $relativeProtocol ?? $this->relativeProtocol;

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class UrlTest extends TestCase
2424
{
2525
public function testNormalizerCanBeSet()
2626
{
27-
$url = new Url(['normalizer' => 'trim']);
27+
$url = new Url(['normalizer' => 'trim', 'requireTld' => true]);
2828

2929
$this->assertEquals('trim', $url->normalizer);
3030
}
@@ -33,14 +33,14 @@ public function testInvalidNormalizerThrowsException()
3333
{
3434
$this->expectException(InvalidArgumentException::class);
3535
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("string" given).');
36-
new Url(['normalizer' => 'Unknown Callable']);
36+
new Url(['normalizer' => 'Unknown Callable', 'requireTld' => true]);
3737
}
3838

3939
public function testInvalidNormalizerObjectThrowsException()
4040
{
4141
$this->expectException(InvalidArgumentException::class);
4242
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).');
43-
new Url(['normalizer' => new \stdClass()]);
43+
new Url(['normalizer' => new \stdClass(), 'requireTld' => true]);
4444
}
4545

4646
public function testAttributes()
@@ -73,17 +73,27 @@ public function testAttributes()
7373
self::assertNull($aConstraint->normalizer);
7474
self::assertTrue($dConstraint->requireTld);
7575
}
76+
77+
/**
78+
* @group legacy
79+
*/
80+
public function testRequireTldDefaultsToFalse()
81+
{
82+
$constraint = new Url();
83+
84+
$this->assertFalse($constraint->requireTld);
85+
}
7686
}
7787

7888
class UrlDummy
7989
{
80-
#[Url]
90+
#[Url(requireTld: false)]
8191
private $a;
8292

83-
#[Url(message: 'myMessage', protocols: ['ftp', 'gopher'], normalizer: 'trim')]
93+
#[Url(message: 'myMessage', protocols: ['ftp', 'gopher'], normalizer: 'trim', requireTld: false)]
8494
private $b;
8595

86-
#[Url(relativeProtocol: true, groups: ['my_group'], payload: 'some attached data')]
96+
#[Url(relativeProtocol: true, groups: ['my_group'], payload: 'some attached data', requireTld: false)]
8797
private $c;
8898

8999
#[Url(requireTld: true)]

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,37 @@ protected function createValidator(): UrlValidator
2525

2626
public function testNullIsValid()
2727
{
28-
$this->validator->validate(null, new Url());
28+
$this->validator->validate(null, new Url(requireTld: true));
2929

3030
$this->assertNoViolation();
3131
}
3232

3333
public function testEmptyStringIsValid()
3434
{
35-
$this->validator->validate('', new Url());
35+
$this->validator->validate('', new Url(requireTld: true));
3636

3737
$this->assertNoViolation();
3838
}
3939

4040
public function testEmptyStringFromObjectIsValid()
4141
{
42-
$this->validator->validate(new EmailProvider(), new Url());
42+
$this->validator->validate(new EmailProvider(), new Url(requireTld: true));
4343

4444
$this->assertNoViolation();
4545
}
4646

4747
public function testExpectsStringCompatibleType()
4848
{
4949
$this->expectException(UnexpectedValueException::class);
50-
$this->validator->validate(new \stdClass(), new Url());
50+
$this->validator->validate(new \stdClass(), new Url(requireTld: true));
5151
}
5252

5353
/**
5454
* @dataProvider getValidUrls
5555
*/
5656
public function testValidUrls($url)
5757
{
58-
$this->validator->validate($url, new Url());
58+
$this->validator->validate($url, new Url(requireTld: false));
5959

6060
$this->assertNoViolation();
6161
}
@@ -65,7 +65,10 @@ public function testValidUrls($url)
6565
*/
6666
public function testValidUrlsWithWhitespaces($url)
6767
{
68-
$this->validator->validate($url, new Url(['normalizer' => 'trim']));
68+
$this->validator->validate($url, new Url([
69+
'normalizer' => 'trim',
70+
'requireTld' => true,
71+
]));
6972

7073
$this->assertNoViolation();
7174
}
@@ -78,6 +81,7 @@ public function testValidRelativeUrl($url)
7881
{
7982
$constraint = new Url([
8083
'relativeProtocol' => true,
84+
'requireTld' => false,
8185
]);
8286

8387
$this->validator->validate($url, $constraint);
@@ -196,6 +200,7 @@ public function testInvalidUrls($url)
196200
{
197201
$constraint = new Url([
198202
'message' => 'myMessage',
203+
'requireTld' => false,
199204
]);
200205

201206
$this->validator->validate($url, $constraint);
@@ -215,6 +220,7 @@ public function testInvalidRelativeUrl($url)
215220
$constraint = new Url([
216221
'message' => 'myMessage',
217222
'relativeProtocol' => true,
223+
'requireTld' => false,
218224
]);
219225

220226
$this->validator->validate($url, $constraint);
@@ -292,10 +298,11 @@ public static function getInvalidUrls()
292298
/**
293299
* @dataProvider getValidCustomUrls
294300
*/
295-
public function testCustomProtocolIsValid($url)
301+
public function testCustomProtocolIsValid($url, $requireTld)
296302
{
297303
$constraint = new Url([
298304
'protocols' => ['ftp', 'file', 'git'],
305+
'requireTld' => $requireTld,
299306
]);
300307

301308
$this->validator->validate($url, $constraint);
@@ -306,9 +313,9 @@ public function testCustomProtocolIsValid($url)
306313
public static function getValidCustomUrls()
307314
{
308315
return [
309-
['ftp://example.com'],
310-
['file://127.0.0.1'],
311-
['git://[::1]/'],
316+
['ftp://example.com', true],
317+
['file://127.0.0.1', false],
318+
['git://[::1]/', false],
312319
];
313320
}
314321

src/Symfony/Component/Validator/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"symfony/polyfill-ctype": "~1.8",
2121
"symfony/polyfill-mbstring": "~1.0",
2222
"symfony/polyfill-php83": "^1.27",
23+
"symfony/deprecation-contracts": "^2.5|^3",
2324
"symfony/translation-contracts": "^2.5|^3"
2425
},
2526
"require-dev": {

0 commit comments

Comments
 (0)