8000 [Validator] Extra timezone tests by ro0NL · Pull Request #31324 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Extra timezone tests #31324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loadi 8000 ng
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Validator] Extra timezone tests
  • Loading branch information
ro0NL committed Apr 30, 2019
commit 4f2bba7cb58fb84461a17e9e756360b794b095d1
8 changes: 6 additions & 2 deletions src/Symfony/Component/Validator/Constraints/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ public function __construct(array $options = null)
{
parent::__construct($options);

if ($this->countryCode && \DateTimeZone::PER_COUNTRY !== $this->zone) {
throw new ConstraintDefinitionException('The option "countryCode" can only be used when "zone" option is configured with `\DateTimeZone::PER_COUNTRY`.');
if (null === $this->countryCode) {
if (0 >= $this->zone || \DateTimeZone::ALL_WITH_BC < $this->zone) {
throw new ConstraintDefinitionException('The option "zone" must be a valid range of "\DateTimeZone" constants.');
}
} elseif (\DateTimeZone::PER_COUNTRY !== (\DateTimeZone::PER_COUNTRY & $this->zone)) {
throw new ConstraintDefinitionException('The option "countryCode" can only be used when the "zone" option is configured with "\DateTimeZone::PER_COUNTRY".');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public function validate($value, Constraint $constraint)

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

if ($timezoneIds && \in_array($value, $timezoneIds, true)) {
if (\in_array($value, $timezoneIds, true)) {
return;
}

Expand Down
44 changes: 25 additions & 19 deletions src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,23 @@ class TimezoneTest extends TestCase
{
public function testValidTimezoneConstraints()
{
$constraint = new Timezone();

$constraint = new Timezone([
'message' => 'myMessage',
new Timezone();
new Timezone(['zone' => \DateTimeZone::ALL]);
new Timezone(['zone' => \DateTimeZone::ALL_WITH_BC]);
new Timezone([
'zone' => \DateTimeZone::PER_COUNTRY,
'countryCode' => 'AR',
]);

$constraint = new Timezone([
'message' => 'myMessage',
'zone' => \DateTimeZone::ALL,
]);

// Make an assertion in order to avoid this test to be marked as risky
$this->assertInstanceOf(Timezone::class, $constraint);
$this->addToAssertionCount(1);
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testExceptionForGroupedTimezonesByCountryWithWrongTimezone()
public function testExceptionForGroupedTimezonesByCountryWithWrongZone()
{
$constraint = new Timezone([
'message' => 'myMessage',
new Timezone([
'zone' => \DateTimeZo 8000 ne::ALL,
'countryCode' => 'AR',
]);
Expand All @@ -53,11 +46,24 @@ public function testExceptionForGroupedTimezonesByCountryWithWrongTimezone()
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testExceptionForGroupedTimezonesByCountryWithoutTimezone()
public function testExceptionForGroupedTimezonesByCountryWithoutZone()
{
$constraint = new Timezone([
'message' => 'myMessage',
'countryCode' => 'AR',
]);
new Timezone(['countryCode' => 'AR']);
}

/**
* @dataProvider provideInvalidZones
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testExceptionForInvalidGroupedTimezones(int $zone)
{
new Timezone(['zone' => $zone]);
}

public function provideInvalidZones(): iterable
{
yield [-1];
yield [0];
yield [\DateTimeZone::ALL_WITH_BC + 1];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,17 @@ public function getValidGroupedTimezonesByCountry(): iterable
yield ['Europe/Monaco', 'MC'];
yield ['Indian/Christmas', 'CX'];
yield ['Pacific/Kiritimati', 'KI'];
yield ['Pacific/Kiritimati', 'KI'];
yield ['Pacific/Kiritimati', 'KI'];
}

/**
* @dataProvider getInvalidGroupedTimezonesByCountry
*/
public function testInvalidGroupedTimezonesByCountry(string $timezone, string $invalidCountryCode)
public function testInvalidGroupedTimezonesByCountry(string $timezone, string $countryCode)
{
$constraint = new Timezone([
'message' => 'myMessage',
'zone' => \DateTimeZone::PER_COUNTRY,
'countryCode' => $invalidCountryCode,
'countryCode' => $countryCode,
]);

$this->validator->validate($timezone, $constraint);
Expand All @@ -217,6 +215,23 @@ public function getInvalidGroupedTimezonesByCountry(): iterable
yield ['America/Argentina/Cordoba', 'FR'];
yield ['America/Barbados', 'PT'];
yield ['Europe/Bern', 'FR'];
yield ['Europe/Amsterdam', 'AC']; // "AC" has no timezones, but is a valid country code
}

public function testGroupedTimezonesWithInvalidCountry()
{
$constraint = new Timezone([
'message' => 'myMessage',
'zone' => \DateTimeZone::PER_COUNTRY,
'countryCode' => 'foobar',
]);

$this->validator->validate('Europe/Amsterdam', $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"Europe/Amsterdam"')
->setCode(Timezone::TIMEZONE_IDENTIFIER_IN_COUNTRY_ERROR)
->assertRaised();
}

/**
Expand Down
0