8000 minor #31324 [Validator] Extra timezone tests (ro0NL) · weaverryan/symfony@2dacfca · GitHub
[go: up one dir, main page]

Skip to content

Commit 2dacfca

Browse files
committed
minor symfony#31324 [Validator] Extra timezone tests (ro0NL)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Validator] Extra timezone tests | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> This PR adds some extra tests, to cover the timezone domain. Ideally to go before symfony#31292 :) Commits ------- 4f2bba7 [Validator] Extra timezone tests
2 parents 8fdcd6e + 4f2bba7 commit 2dacfca

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ public function __construct(array $options = null)
4444
{
4545
parent::__construct($options);
4646

47-
if ($this->countryCode && \DateTimeZone::PER_COUNTRY !== $this->zone) {
48-
throw new ConstraintDefinitionException('The option "countryCode" can only be used when "zone" option is configured with `\DateTimeZone::PER_COUNTRY`.');
47+
if (null === $this->countryCode) {
48+
if (0 >= $this->zone || \DateTimeZone::ALL_WITH_BC < $this->zone) {
49+
throw new ConstraintDefinitionException('The option "zone" must be a valid range of "\DateTimeZone" constants.');
50+
}
51+
} elseif (\DateTimeZone::PER_COUNTRY !== (\DateTimeZone::PER_COUNTRY & $this->zone)) {
52+
throw new ConstraintDefinitionException('The option "countryCode" can only be used when the "zone" option is configured with "\DateTimeZone::PER_COUNTRY".');
4953
}
5054
}
5155
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public function validate($value, Constraint $constraint)
4545

4646
// @see: https://bugs.php.net/bug.php?id=75928
4747
if ($constraint->countryCode) {
48-
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode);
48+
$timezoneIds = @\DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode) ?: [];
4949
} else {
5050
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone);
5151
}
5252

53-
if ($timezoneIds && \in_array($value, $timezoneIds, true)) {
53+
if (\in_array($value, $timezoneIds, true)) {
5454
return;
5555
}
5656

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,23 @@ class TimezoneTest extends TestCase
2121
{
2222
public function testValidTimezoneConstraints()
2323
{
24-
$constraint = new Timezone();
25-
26-
$constraint = new Timezone([
27-
'message' => 'myMessage',
24+
new Timezone();
25+
new Timezone(['zone' => \DateTimeZone::ALL]);
26+
new Timezone(['zone' => \DateTimeZone::ALL_WITH_BC]);
27+
new Timezone([
2828
'zone' => \DateTimeZone::PER_COUNTRY,
2929
'countryCode' => 'AR',
3030
]);
3131

32-
$constraint = new Timezone([
33-
'message' => 'myMessage',
34-
'zone' => \DateTimeZone::ALL,
35-
]);
36-
37-
// Make an assertion in order to avoid this test to be marked as risky
38-
$this->assertInstanceOf(Timezone::class, $constraint);
32+
$this->addToAssertionCount(1);
3933
}
4034

4135
/**
4236
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
4337
*/
44-
public function testExceptionForGroupedTimezonesByCountryWithWrongTimezone()
38+
public function testExceptionForGroupedTimezonesByCountryWithWrongZone()
4539
{
46-
$constraint = new Timezone([
47-
'message' => 'myMessage',
40+
new Timezone([
4841
'zone' => \DateTimeZone::ALL,
4942
'countryCode' => 'AR',
5043
]);
@@ -53,11 +46,24 @@ public function testExceptionForGroupedTimezonesByCountryWithWrongTimezone()
5346
/**
5447
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
5548
*/
56-
public function testExceptionForGroupedTimezonesByCountryWithoutTimezone()
49+
public function testExceptionForGroupedTimezonesByCountryWithoutZone()
5750
{
58-
$constraint = new Timezone([
59-
'message' => 'myMessage',
60-
'countryCode' => 'AR',
61-
]);
51+
new Timezone(['countryCode' => 'AR']);
52+
}
53+
54+
/**
55+
* @dataProvider provideInvalidZones
56+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
57+
*/
58+
public function testExceptionForInvalidGroupedTimezones(int $zone)
59+
{
60+
new Timezone(['zone' => $zone]);
61+
}
62+
63+
public function provideInvalidZones(): iterable
64+
{
65+
yield [-1];
66+
yield [0];
67+
yield [\DateTimeZone::ALL_WITH_BC + 1];
6268
}
6369
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,17 @@ public function getValidGroupedTimezonesByCountry(): iterable
189189
yield ['Europe/Monaco', 'MC'];
190190
yield ['Indian/Christmas', 'CX'];
191191
yield ['Pacific/Kiritimati', 'KI'];
192-
yield ['Pacific/Kiritimati', 'KI'];
193-
yield ['Pacific/Kiritimati', 'KI'];
194192
}
195193

196194
/**
197195
* @dataProvider getInvalidGroupedTimezonesByCountry
198196
*/
199-
public function testInvalidGroupedTimezonesByCountry(string $timezone, string $invalidCountryCode)
197+
public function testInvalidGroupedTimezonesByCountry(string $timezone, string $countryCode)
200198
{
201199
$constraint = new Timezone([
202200
'message' => 'myMessage',
203201
'zone' => \DateTimeZone::PER_COUNTRY,
204-
'countryCode' => $invalidCountryCode,
202+
'countryCode' => $countryCode,
205203
]);
206204

207205
$this->validator->validate($timezone, $constraint);
@@ -217,6 +215,23 @@ public function getInvalidGroupedTimezonesByCountry(): iterable
217215
yield ['America/Argentina/Cordoba', 'FR'];
218216
yield ['America/Barbados', 'PT'];
219217
yield ['Europe/Bern', 'FR'];
218+
yield ['Europe/Amsterdam', 'AC']; // "AC" has no timezones, but is a valid country code
219+
}
220+
221+
public function testGroupedTimezonesWithInvalidCountry()
222+
{
223+
$constraint = new Timezone([
224+
'message' => 'myMessage',
225+
'zone' => \DateTimeZone::PER_COUNTRY,
226+
'countryCode' => 'foobar',
227+
]);
228+
229+
$this->validator->validate('Europe/Amsterdam', $constraint);
230+
231+
$this->buildViolation('myMessage')
232+
->setParameter('{{ value }}', '"Europe/Amsterdam"')
233+
->setCode(Timezone::TIMEZONE_IDENTIFIER_IN_COUNTRY_ERROR)
234+
->assertRaised();
220235
}
221236

222237
/**

0 commit comments

Comments
 (0)
0