From 4b87fb778d7aecbb7ef8e682da7df9f7f32d0c94 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 4 Apr 2017 01:17:01 -0300 Subject: [PATCH 01/14] Add `TimeZoneValidator` --- .../Validator/Constraints/Timezone.php | 45 +++++ .../Constraints/TimezoneValidator.php | 60 +++++++ .../Constraints/TimezoneValidatorTest.php | 159 ++++++++++++++++++ 3 files changed, 264 insertions(+) create mode 100644 src/Symfony/Component/Validator/Constraints/Timezone.php create mode 100644 src/Symfony/Component/Validator/Constraints/TimezoneValidator.php create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php diff --git a/src/Symfony/Component/Validator/Constraints/Timezone.php b/src/Symfony/Component/Validator/Constraints/Timezone.php new file mode 100644 index 0000000000000..80b2b206db2aa --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Timezone.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) + * + * @author Javier Spagnoletti + */ +class Timezone extends Constraint +{ + const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a'; + + public $value = \DateTimeZone::ALL; + + public $message = 'This value is not a valid timezone at {{ timezone_group }}.'; + + protected static $errorNames = array( + self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR', + ); + + /** + * {@inheritdoc} + */ + public function __construct($options = null) + { + if (isset($options['value'])) { + $this->value = $options['value']; + } + + parent::__construct($options); + } +} diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php new file mode 100644 index 0000000000000..a711637c06f1b --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\Validator\Exception\UnexpectedTypeException; + +/** + * Validates whether a value is a valid timezone identifier. + * + * @author Javier Spagnoletti + */ +class TimezoneValidator extends ConstraintValidator +{ + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) + { + if (!$constraint instanceof Timezone) { + throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Timezone'); + } + + if (null === $value || '' === $value) { + return; + } + + if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { + throw new UnexpectedTypeException($value, 'string'); + } + + $value = (string) $value; + $timezoneIds = \DateTimeZone::listIdentifiers($constraint->value); + + if ($timezoneIds && !in_array($value, $timezoneIds, true)) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ timezone_group }}', $this->formatValue($value)) + ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) + ->addViolation(); + } + } + + /** + * {@inheritdoc} + */ + public function getDefaultOption() + { + return 'value'; + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php new file mode 100644 index 0000000000000..068ea846b9b43 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -0,0 +1,159 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\Timezone; +use Symfony\Component\Validator\Constraints\TimezoneValidator; +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; + +/** + * @author Javier Spagnoletti + */ +class TimezoneValidatorTest extends ConstraintValidatorTestCase +{ + protected function createValidator() + { + return new TimezoneValidator(); + } + + public function testNullIsValid() + { + $this->validator->validate(null, new Timezone()); + + $this->assertNoViolation(); + } + + public function testEmptyStringIsValid() + { + $this->validator->validate('', new Timezone()); + + $this->assertNoViolation(); + } + + /** + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException + */ + public function testExpectsStringCompatibleType() + { + $this->validator->validate(new \stdClass(), new Timezone()); + } + + /** + * @dataProvider getValidTimezones + */ + public function testValidTimezones($timezone) + { + $this->validator->validate($timezone, new Timezone()); + + $this->assertNoViolation(); + } + + public function getValidTimezones() + { + return array( + array('America/Argentina/Buenos_Aires'), + array('America/Barbados'), + array('Antarctica/Syowa'), + array('Africa/Douala'), + array('Atlantic/Canary'), + array('Asia/Gaza'), + array('Europe/Copenhagen'), + ); + } + + /** + * @dataProvider getValidGroupedTimezones + */ + public function testValidGroupedTimezones($timezone, $what) + { + $constraint = new Timezone(array( + 'value' => $what, + )); + + $this->validator->validate($timezone, $constraint); + + $this->assertNoViolation(); + } + + public function getValidGroupedTimezones() + { + return array( + array('America/Argentina/Cordoba', \DateTimeZone::AMERICA), + array('America/Barbados', \DateTimeZone::AMERICA), + array('Africa/Cairo', \DateTimeZone::AFRICA), + array('Atlantic/Cape_Verde', \DateTimeZone::ATLANTIC), + array('Europe/Bratislava', \DateTimeZone::EUROPE), + array('Indian/Christmas', \DateTimeZone::INDIAN), + array('Pacific/Kiritimati', \DateTimeZone::ALL), + array('Pacific/Kiritimati', \DateTimeZone::ALL_WITH_BC), + array('Pacific/Kiritimati', \DateTimeZone::PACIFIC), + array('Arctic/Longyearbyen', \DateTimeZone::ARCTIC), + array('Asia/Beirut', \DateTimeZone::ASIA), + array('Atlantic/Bermuda', \DateTimeZone::ASIA | \DateTimeZone::ATLANTIC), + array('Atlantic/Azores', \DateTimeZone::ATLANTIC | \DateTimeZone::ASIA), + ); + } + + /** + * @dataProvider getInvalidTimezones + */ + public function testInvalidTimezones($timezone) + { + $constraint = new Timezone(array( + 'message' => 'myMessage', + )); + + $this->validator->validate($timezone, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ timezone_group }}', '"'.$timezone.'"') + ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) + ->assertRaised(); + } + + public function getInvalidTimezones() + { + return array( + array('Buenos_Aires/Argentina/America'), + array('Mayotte/Indian'), + array('foobar'), + ); + } + + /** + * @dataProvider getInvalidGroupedTimezones + */ + public function testInvalidGroupedTimezones($timezone, $what) + { + $constraint = new Timezone(array( + 'value' => $what, + 'message' => 'myMessage', + )); + + $this->validator->validate($timezone, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ timezone_group }}', '"'.$timezone.'"') + ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) + ->assertRaised(); + } + + public function getInvalidGroupedTimezones() + { + return array( + array('Antarctica/McMurdo', \DateTimeZone::AMERICA), + array('America/Barbados', \DateTimeZone::ANTARCTICA), + array('Europe/Kiev', \DateTimeZone::ARCTIC), + array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN), + ); + } +} From 6cceda0ac17270cb1e97831a2f63353d14e1d58e Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 4 Apr 2017 14:45:55 -0300 Subject: [PATCH 02/14] Add support for `\DateTimeZone::PER_COUNTRY` --- .../Validator/Constraints/Timezone.php | 17 ++++- .../Constraints/TimezoneValidator.php | 4 +- .../Tests/Constraints/TimezoneTest.php | 60 +++++++++++++++++ .../Constraints/TimezoneValidatorTest.php | 65 ++++++++++++++++++- 4 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php diff --git a/src/Symfony/Component/Validator/Constraints/Timezone.php b/src/Symfony/Component/Validator/Constraints/Timezone.php index 80b2b206db2aa..7f22d7bc4d35a 100644 --- a/src/Symfony/Component/Validator/Constraints/Timezone.php +++ b/src/Symfony/Component/Validator/Constraints/Timezone.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; /** * @Annotation @@ -23,7 +24,9 @@ class Timezone extends Constraint { const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a'; - public $value = \DateTimeZone::ALL; + public $timezone = \DateTimeZone::ALL; + + public $countryCode; public $message = 'This value is not a valid timezone at {{ timezone_group }}.'; @@ -36,8 +39,16 @@ class Timezone extends Constraint */ public function __construct($options = null) { - if (isset($options['value'])) { - $this->value = $options['value']; + if (isset($options['timezone'])) { + $this->timezone = $options['timezone']; + } + + if (isset($options['countryCode'])) { + if (\DateTimeZone::PER_COUNTRY !== $this->timezone) { + throw new ConstraintDefinitionException('The option "countryCode" can only be used when "timezone" option has `\DateTimeZone::PER_COUNTRY` as value'); + } + + $this->countryCode = $options['countryCode']; } parent::__construct($options); diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index a711637c06f1b..a93dff92392b2 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -40,7 +40,7 @@ public function validate($value, Constraint $constraint) } $value = (string) $value; - $timezoneIds = \DateTimeZone::listIdentifiers($constraint->value); + $timezoneIds = \DateTimeZone::listIdentifiers($constraint->timezone, $constraint->countryCode); if ($timezoneIds && !in_array($value, $timezoneIds, true)) { $this->context->buildViolation($constraint->message) @@ -55,6 +55,6 @@ public function validate($value, Constraint $constraint) */ public function getDefaultOption() { - return 'value'; + return 'timezone'; } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php new file mode 100644 index 0000000000000..29b709b02e5dc --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Validator\Constraints\Timezone; + +/** + * @author Javier Spagnoletti + */ +class TimezoneTest extends TestCase +{ + public function testValidTimezoneConstraints() + { + $constraint = new Timezone(); + + $constraint = new Timezone(array( + 'message' => 'myMessage', + 'timezone' => \DateTimeZone::PER_COUNTRY, + 'countryCode' => 'AR', + )); + + $constraint = new Timezone(array( + 'message' => 'myMessage', + 'timezone' => \DateTimeZone::ALL, + )); + } + + /** + * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException + */ + public function testExceptionForGroupedTimezonesByCountryWithWrongTimezone() + { + $constraint = new Timezone(array( + 'message' => 'myMessage', + 'timezone' => \DateTimeZone::ALL, + 'countryCode' => 'AR', + )); + } + + /** + * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException + */ + public function testExceptionForGroupedTimezonesByCountryWithoutTimezone() + { + $constraint = new Timezone(array( + 'message' => 'myMessage', + 'countryCode' => 'AR', + )); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index 068ea846b9b43..2bd2926b26dad 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -76,7 +76,7 @@ public function getValidTimezones() public function testValidGroupedTimezones($timezone, $what) { $constraint = new Timezone(array( - 'value' => $what, + 'timezone' => $what, )); $this->validator->validate($timezone, $constraint); @@ -135,7 +135,7 @@ public function getInvalidTimezones() public function testInvalidGroupedTimezones($timezone, $what) { $constraint = new Timezone(array( - 'value' => $what, + 'timezone' => $what, 'message' => 'myMessage', )); @@ -156,4 +156,65 @@ public function getInvalidGroupedTimezones() array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN), ); } + + /** + * @dataProvider getValidGroupedTimezonesByCountry + */ + public function testValidGroupedTimezonesByCountry($timezone, $what, $country) + { + $constraint = new Timezone(array( + 'timezone' => $what, + 'countryCode' => $country, + )); + + $this->validator->validate($timezone, $constraint); + + $this->assertNoViolation(); + } + + public function getValidGroupedTimezonesByCountry() + { + return array( + array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'AR'), + array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'BB'), + array('Africa/Cairo', \DateTimeZone::PER_COUNTRY, 'EG'), + array('Atlantic/Cape_Verde', \DateTimeZone::PER_COUNTRY, 'CV'), + array('Europe/Bratislava', \DateTimeZone::PER_COUNTRY, 'SK'), + array('Indian/Christmas', \DateTimeZone::PER_COUNTRY, 'CX'), + array('Pacific/Kiritimati', \DateTimeZone::PER_COUNTRY, 'KI'), + array('Pacific/Kiritimati', \DateTimeZone::PER_COUNTRY, 'KI'), + array('Pacific/Kiritimati', \DateTimeZone::PER_COUNTRY, 'KI'), + array('Arctic/Longyearbyen', \DateTimeZone::PER_COUNTRY, 'SJ'), + array('Asia/Beirut', \DateTimeZone::PER_COUNTRY, 'LB'), + array('Atlantic/Bermuda', \DateTimeZone::PER_COUNTRY, 'BM'), + array('Atlantic/Azores', \DateTimeZone::PER_COUNTRY, 'PT'), + ); + } + + /** + * @dataProvider getInvalidGroupedTimezonesByCountry + */ + public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country) + { + $constraint = new Timezone(array( + 'message' => 'myMessage', + 'timezone' => $what, + 'countryCode' => $country, + )); + + $this->validator->validate($timezone, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ timezone_group }}', '"'.$timezone.'"') + ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) + ->assertRaised(); + } + + public function getInvalidGroupedTimezonesByCountry() + { + return array( + array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR'), + array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT'), + ); + } } From 6124ef6ccc9f8c2d7281147762d13865c58faeb9 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 4 Apr 2017 16:15:35 -0300 Subject: [PATCH 03/14] Update constraint message --- .../Validator/Constraints/Timezone.php | 2 +- .../Constraints/TimezoneValidator.php | 32 ++++++++++++++++++- .../Constraints/TimezoneValidatorTest.php | 31 +++++++++--------- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Timezone.php b/src/Symfony/Component/Validator/Constraints/Timezone.php index 7f22d7bc4d35a..9ba0fd6e7b203 100644 --- a/src/Symfony/Component/Validator/Constraints/Timezone.php +++ b/src/Symfony/Component/Validator/Constraints/Timezone.php @@ -28,7 +28,7 @@ class Timezone extends Constraint public $countryCode; - public $message = 'This value is not a valid timezone at {{ timezone_group }}.'; + public $message = 'This value is not a valid timezone{{ extra_info }}.'; protected static $errorNames = array( self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR', diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index a93dff92392b2..4035296241f40 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -44,7 +44,7 @@ public function validate($value, Constraint $constraint) if ($timezoneIds && !in_array($value, $timezoneIds, true)) { $this->context->buildViolation($constraint->message) - ->setParameter('{{ timezone_group }}', $this->formatValue($value)) + ->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->timezone, $constraint->countryCode)) ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) ->addViolation(); } @@ -57,4 +57,34 @@ public function getDefaultOption() { return 'timezone'; } + + /** + * Format the extra info which is appended to validation message based on + * constraint options + * + * @param int $timezone + * @param string|null $countryCode + * + * @return string + */ + protected function formatExtraInfo($timezone, $countryCode = null) + { + if (!$timezone) { + return ''; + } + $r = new \ReflectionClass('\DateTimeZone'); + $consts = $r->getConstants(); + + if (!$value = array_search($timezone, $consts, true)) { + $value = $timezone; + } + + $value = ' for "'.$value.'" zone'; + + if ($countryCode) { + $value = ' for ISO 3166-1 country code '.$countryCode; + } + + return $this->formatValue($value); + } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index 2bd2926b26dad..3004f1ef5b9ee 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -106,7 +106,7 @@ public function getValidGroupedTimezones() /** * @dataProvider getInvalidTimezones */ - public function testInvalidTimezones($timezone) + public function testInvalidTimezones($timezone, $extraInfo) { $constraint = new Timezone(array( 'message' => 'myMessage', @@ -115,7 +115,7 @@ public function testInvalidTimezones($timezone) $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ timezone_group }}', '"'.$timezone.'"') + ->setParameter('{{ extra_info }}', '"'.$extraInfo.'"') ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) ->assertRaised(); } @@ -123,16 +123,16 @@ public function testInvalidTimezones($timezone) public function getInvalidTimezones() { return array( - array('Buenos_Aires/Argentina/America'), - array('Mayotte/Indian'), - array('foobar'), + array('Buenos_Aires/Argentina/America', ' for "ALL" zone'), + array('Mayotte/Indian', ' for "ALL" zone'), + array('foobar', ' for "ALL" zone'), ); } /** * @dataProvider getInvalidGroupedTimezones */ - public function testInvalidGroupedTimezones($timezone, $what) + public function testInvalidGroupedTimezones($timezone, $what, $extraInfo) { $constraint = new Timezone(array( 'timezone' => $what, @@ -142,7 +142,7 @@ public function testInvalidGroupedTimezones($timezone, $what) $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ timezone_group }}', '"'.$timezone.'"') + ->setParameter('{{ extra_info }}', '"'.$extraInfo.'"') ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) ->assertRaised(); } @@ -150,10 +150,11 @@ public function testInvalidGroupedTimezones($timezone, $what) public function getInvalidGroupedTimezones() { return array( - array('Antarctica/McMurdo', \DateTimeZone::AMERICA), - array('America/Barbados', \DateTimeZone::ANTARCTICA), - array('Europe/Kiev', \DateTimeZone::ARCTIC), - array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN), + array('Antarctica/McMurdo', \DateTimeZone::AMERICA, ' for "AMERICA" zone'), + array('America/Barbados', \DateTimeZone::ANTARCTICA, ' for "ANTARCTICA" zone'), + array('Europe/Kiev', \DateTimeZone::ARCTIC, ' for "ARCTIC" zone'), + array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN, ' for "INDIAN" zone'), + array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA, ' for "260" zone'), ); } @@ -194,7 +195,7 @@ public function getValidGroupedTimezonesByCountry() /** * @dataProvider getInvalidGroupedTimezonesByCountry */ - public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country) + public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country, $extraInfo) { $constraint = new Timezone(array( 'message' => 'myMessage', @@ -205,7 +206,7 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country) $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ timezone_group }}', '"'.$timezone.'"') + ->setParameter('{{ extra_info }}', '"'.$extraInfo.'"') ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) ->assertRaised(); } @@ -213,8 +214,8 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country) public function getInvalidGroupedTimezonesByCountry() { return array( - array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR'), - array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT'), + array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR', ' for ISO 3166-1 country code FR'), + array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', ' for ISO 3166-1 country code PT'), ); } } From 49e4b51e62749f217b6b80a31c15fdc1e251e21f Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 4 Apr 2017 16:20:45 -0300 Subject: [PATCH 04/14] Renamed "timezone" group to "zone" --- .../Component/Validator/Constraints/Timezone.php | 10 +++++----- .../Validator/Constraints/TimezoneValidator.php | 16 ++++++++-------- .../Validator/Tests/Constraints/TimezoneTest.php | 6 +++--- .../Tests/Constraints/TimezoneValidatorTest.php | 8 ++++---- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Timezone.php b/src/Symfony/Component/Validator/Constraints/Timezone.php index 9ba0fd6e7b203..07af525333ca9 100644 --- a/src/Symfony/Component/Validator/Constraints/Timezone.php +++ b/src/Symfony/Component/Validator/Constraints/Timezone.php @@ -24,7 +24,7 @@ class Timezone extends Constraint { const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a'; - public $timezone = \DateTimeZone::ALL; + public $zone = \DateTimeZone::ALL; public $countryCode; @@ -39,13 +39,13 @@ class Timezone extends Constraint */ public function __construct($options = null) { - if (isset($options['timezone'])) { - $this->timezone = $options['timezone']; + if (isset($options['zone'])) { + $this->zone = $options['zone']; } if (isset($options['countryCode'])) { - if (\DateTimeZone::PER_COUNTRY !== $this->timezone) { - throw new ConstraintDefinitionException('The option "countryCode" can only be used when "timezone" option has `\DateTimeZone::PER_COUNTRY` as value'); + if (\DateTimeZone::PER_COUNTRY !== $this->zone) { + throw new ConstraintDefinitionException('The option "countryCode" can only be used when "zone" option has `\DateTimeZone::PER_COUNTRY` as value'); } $this->countryCode = $options['countryCode']; diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index 4035296241f40..89f9f5cd17e08 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -40,11 +40,11 @@ public function validate($value, Constraint $constraint) } $value = (string) $value; - $timezoneIds = \DateTimeZone::listIdentifiers($constraint->timezone, $constraint->countryCode); + $timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode); if ($timezoneIds && !in_array($value, $timezoneIds, true)) { $this->context->buildViolation($constraint->message) - ->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->timezone, $constraint->countryCode)) + ->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->zone, $constraint->countryCode)) ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) ->addViolation(); } @@ -55,28 +55,28 @@ public function validate($value, Constraint $constraint) */ public function getDefaultOption() { - return 'timezone'; + return 'zone'; } /** * Format the extra info which is appended to validation message based on * constraint options * - * @param int $timezone + * @param int $zone * @param string|null $countryCode * * @return string */ - protected function formatExtraInfo($timezone, $countryCode = null) + protected function formatExtraInfo($zone, $countryCode = null) { - if (!$timezone) { + if (!$zone) { return ''; } $r = new \ReflectionClass('\DateTimeZone'); $consts = $r->getConstants(); - if (!$value = array_search($timezone, $consts, true)) { - $value = $timezone; + if (!$value = array_search($zone, $consts, true)) { + $value = $zone; } $value = ' for "'.$value.'" zone'; diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php index 29b709b02e5dc..a3fea101a2a59 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php @@ -25,13 +25,13 @@ public function testValidTimezoneConstraints() $constraint = new Timezone(array( 'message' => 'myMessage', - 'timezone' => \DateTimeZone::PER_COUNTRY, + 'zone' => \DateTimeZone::PER_COUNTRY, 'countryCode' => 'AR', )); $constraint = new Timezone(array( 'message' => 'myMessage', - 'timezone' => \DateTimeZone::ALL, + 'zone' => \DateTimeZone::ALL, )); } @@ -42,7 +42,7 @@ public function testExceptionForGroupedTimezonesByCountryWithWrongTimezone() { $constraint = new Timezone(array( 'message' => 'myMessage', - 'timezone' => \DateTimeZone::ALL, + 'zone' => \DateTimeZone::ALL, 'countryCode' => 'AR', )); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index 3004f1ef5b9ee..43c480079d8dc 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -76,7 +76,7 @@ public function getValidTimezones() public function testValidGroupedTimezones($timezone, $what) { $constraint = new Timezone(array( - 'timezone' => $what, + 'zone' => $what, )); $this->validator->validate($timezone, $constraint); @@ -135,7 +135,7 @@ public function getInvalidTimezones() public function testInvalidGroupedTimezones($timezone, $what, $extraInfo) { $constraint = new Timezone(array( - 'timezone' => $what, + 'zone' => $what, 'message' => 'myMessage', )); @@ -164,7 +164,7 @@ public function getInvalidGroupedTimezones() public function testValidGroupedTimezonesByCountry($timezone, $what, $country) { $constraint = new Timezone(array( - 'timezone' => $what, + 'zone' => $what, 'countryCode' => $country, )); @@ -199,7 +199,7 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country, { $constraint = new Timezone(array( 'message' => 'myMessage', - 'timezone' => $what, + 'zone' => $what, 'countryCode' => $country, )); From 288a4138ddb3246c8be678c0ced1f06c824d7261 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 4 Apr 2017 16:40:10 -0300 Subject: [PATCH 05/14] Apply CS fixes from `fabbot` --- .../Component/Validator/Constraints/TimezoneValidator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index 89f9f5cd17e08..309bf25284bcd 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -60,9 +60,9 @@ public function getDefaultOption() /** * Format the extra info which is appended to validation message based on - * constraint options + * constraint options. * - * @param int $zone + * @param int $zone * @param string|null $countryCode * * @return string From 75452754a40603920bdaa53793a6289b785bc8f3 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 4 Apr 2017 17:28:18 -0300 Subject: [PATCH 06/14] Update method visibility at `TimezoneValidator` --- .../Component/Validator/Constraints/TimezoneValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index 309bf25284bcd..ebd60c8e3898d 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -67,7 +67,7 @@ public function getDefaultOption() * * @return string */ - protected function formatExtraInfo($zone, $countryCode = null) + private function formatExtraInfo($zone, $countryCode = null) { if (!$zone) { return ''; From de788025e1e26f728c6177e1bdc68c593e8ca4d2 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Wed, 5 Apr 2017 13:00:08 -0300 Subject: [PATCH 07/14] Update validation messages --- .../Constraints/TimezoneValidator.php | 22 ++++++++----------- .../Constraints/TimezoneValidatorTest.php | 6 ++--- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index ebd60c8e3898d..acd3b690ac4ce 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -69,20 +69,16 @@ public function getDefaultOption() */ private function formatExtraInfo($zone, $countryCode = null) { - if (!$zone) { - return ''; - } - $r = new \ReflectionClass('\DateTimeZone'); - $consts = $r->getConstants(); - - if (!$value = array_search($zone, $consts, true)) { - $value = $zone; - } - - $value = ' for "'.$value.'" zone'; - if ($countryCode) { - $value = ' for ISO 3166-1 country code '.$countryCode; + $value = ' for ISO 3166-1 country code "'.$countryCode.'"'; + } else { + $r = new \ReflectionClass('\DateTimeZone'); + $consts = $r->getConstants(); + if ($value = array_search($zone, $consts, true)) { + $value = ' for "'.$value.'" zone'; + } else { + $value = ' for zone with identifier '.$zone; + } } return $this->formatValue($value); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index 43c480079d8dc..747f226c8ce53 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -154,7 +154,7 @@ public function getInvalidGroupedTimezones() array('America/Barbados', \DateTimeZone::ANTARCTICA, ' for "ANTARCTICA" zone'), array('Europe/Kiev', \DateTimeZone::ARCTIC, ' for "ARCTIC" zone'), array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN, ' for "INDIAN" zone'), - array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA, ' for "260" zone'), + array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA, ' for zone with identifier 260'), ); } @@ -214,8 +214,8 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country, public function getInvalidGroupedTimezonesByCountry() { return array( - array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR', ' for ISO 3166-1 country code FR'), - array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', ' for ISO 3166-1 country code PT'), + array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR', ' for ISO 3166-1 country code "FR"'), + array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', ' for ISO 3166-1 country code "PT"'), ); } } From dfa4f984416d9c0e0eb2f4a9b930ffc5301970fe Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Wed, 5 Apr 2017 14:43:32 -0300 Subject: [PATCH 08/14] Update way of handling `zone` option --- .../Component/Validator/Constraints/Timezone.php | 2 +- .../Validator/Constraints/TimezoneValidator.php | 8 ++++++-- .../Tests/Constraints/TimezoneValidatorTest.php | 10 +++++----- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Timezone.php b/src/Symfony/Component/Validator/Constraints/Timezone.php index 07af525333ca9..75162c8dc5ca9 100644 --- a/src/Symfony/Component/Validator/Constraints/Timezone.php +++ b/src/Symfony/Component/Validator/Constraints/Timezone.php @@ -24,7 +24,7 @@ class Timezone extends Constraint { const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a'; - public $zone = \DateTimeZone::ALL; + public $zone; public $countryCode; diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index acd3b690ac4ce..3d941023522d2 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -40,7 +40,8 @@ public function validate($value, Constraint $constraint) } $value = (string) $value; - $timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode); + $zone = null !== $constraint->zone ? $constraint->zone : \DateTimeZone::ALL; + $timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode); if ($timezoneIds && !in_array($value, $timezoneIds, true)) { $this->context->buildViolation($constraint->message) @@ -62,13 +63,16 @@ public function getDefaultOption() * Format the extra info which is appended to validation message based on * constraint options. * - * @param int $zone + * @param int|null $zone * @param string|null $countryCode * * @return string */ private function formatExtraInfo($zone, $countryCode = null) { + if (null === $zone) { + return ''; + } if ($countryCode) { $value = ' for ISO 3166-1 country code "'.$countryCode.'"'; } else { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index 747f226c8ce53..b61dd724d5428 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -106,7 +106,7 @@ public function getValidGroupedTimezones() /** * @dataProvider getInvalidTimezones */ - public function testInvalidTimezones($timezone, $extraInfo) + public function testInvalidTimezonesWithoutZone($timezone, $extraInfo) { $constraint = new Timezone(array( 'message' => 'myMessage', @@ -115,7 +115,7 @@ public function testInvalidTimezones($timezone, $extraInfo) $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ extra_info }}', '"'.$extraInfo.'"') + ->setParameter('{{ extra_info }}', $extraInfo) ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) ->assertRaised(); } @@ -123,9 +123,9 @@ public function testInvalidTimezones($timezone, $extraInfo) public function getInvalidTimezones() { return array( - array('Buenos_Aires/Argentina/America', ' for "ALL" zone'), - array('Mayotte/Indian', ' for "ALL" zone'), - array('foobar', ' for "ALL" zone'), + array('Buenos_Aires/Argentina/America', ''), + array('Mayotte/Indian', ''), + array('foobar', ''), ); } From 6be950a4d5b87f0fd9cccd522afa6e96be7dde9f Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Wed, 5 Apr 2017 20:05:35 -0300 Subject: [PATCH 09/14] Add specific errors to be used when `zone` or `countryCode` are provided --- .../Component/Validator/Constraints/Timezone.php | 2 ++ .../Validator/Constraints/TimezoneValidator.php | 10 +++++++++- .../Tests/Constraints/TimezoneValidatorTest.php | 4 ++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Timezone.php b/src/Symfony/Component/Validator/Constraints/Timezone.php index 75162c8dc5ca9..8aa09997d1359 100644 --- a/src/Symfony/Component/Validator/Constraints/Timezone.php +++ b/src/Symfony/Component/Validator/Constraints/Timezone.php @@ -23,6 +23,8 @@ class Timezone extends Constraint { const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a'; + const NO_SUCH_TIMEZONE_IN_ZONE_ERROR = 'b57767b1-36c0-40ac-a3d7-629420c775b8'; + const NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b'; public $zone; diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index 3d941023522d2..b387c1396edce 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -44,9 +44,17 @@ public function validate($value, Constraint $constraint) $timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode); if ($timezoneIds && !in_array($value, $timezoneIds, true)) { + if ($constraint->countryCode) { + $code = Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR; + } elseif (null !== $constraint->zone) { + $code = Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR; + } else { + $code = Timezone::NO_SUCH_TIMEZONE_ERROR; + } + $this->context->buildViolation($constraint->message) ->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->zone, $constraint->countryCode)) - ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) + ->setCode($code) ->addViolation(); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index b61dd724d5428..2fe968a29f405 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -143,7 +143,7 @@ public function testInvalidGroupedTimezones($timezone, $what, $extraInfo) $this->buildViolation('myMessage') ->setParameter('{{ extra_info }}', '"'.$extraInfo.'"') - ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) + ->setCode(Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR) ->assertRaised(); } @@ -207,7 +207,7 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country, $this->buildViolation('myMessage') ->setParameter('{{ extra_info }}', '"'.$extraInfo.'"') - ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) + ->setCode(Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR) ->assertRaised(); } From 6d23035fc73f5eefd842175f53351e84b81a8063 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Sun, 9 Apr 2017 17:20:02 -0300 Subject: [PATCH 10/14] Add support for deprecated timezones --- .../Constraints/TimezoneValidatorTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index 2fe968a29f405..8bdb19b3a22c3 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -218,4 +218,44 @@ public function getInvalidGroupedTimezonesByCountry() array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', ' for ISO 3166-1 country code "PT"'), ); } + + /** + * @dataProvider getDeprecatedTimezones + */ + public function testDeprecatedTimezonesAreVaildWithBC($timezone) + { + $constraint = new Timezone(array( + 'zone' => \DateTimeZone::ALL_WITH_BC, + )); + + $this->validator->validate($timezone, $constraint); + + $this->assertNoViolation(); + } + + /** + * @dataProvider getDeprecatedTimezones + */ + public function testDeprecatedTimezonesAreInvaildWithoutBC($timezone) + { + $constraint = new Timezone(array( + 'message' => 'myMessage', + )); + + $this->validator->validate($timezone, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ extra_info }}', '') + ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) + ->assertRaised(); + } + + public function getDeprecatedTimezones() + { + return array( + array('America/Buenos_Aires'), + array('Etc/GMT'), + array('US/Pacific'), + ); + } } From 13bc6290d47c2d899bc721ded051e80d51db4e53 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 18 Apr 2017 23:07:01 -0300 Subject: [PATCH 11/14] Remove surplus quotes in validation message --- .../Component/Validator/Constraints/TimezoneValidator.php | 2 +- .../Validator/Tests/Constraints/TimezoneValidatorTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index b387c1396edce..406c0b2eedf64 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -93,6 +93,6 @@ private function formatExtraInfo($zone, $countryCode = null) } } - return $this->formatValue($value); + return $value; } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index 8bdb19b3a22c3..18b17c8166473 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -142,7 +142,7 @@ public function testInvalidGroupedTimezones($timezone, $what, $extraInfo) $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ extra_info }}', '"'.$extraInfo.'"') + ->setParameter('{{ extra_info }}', $extraInfo) ->setCode(Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR) ->assertRaised(); } @@ -206,7 +206,7 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country, $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ extra_info }}', '"'.$extraInfo.'"') + ->setParameter('{{ extra_info }}', $extraInfo) ->setCode(Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR) ->assertRaised(); } From 0491875763682690d0c10eb630a5b962c896390f Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 6 Feb 2018 14:46:40 -0300 Subject: [PATCH 12/14] Update validation message --- .../Validator/Constraints/Timezone.php | 12 ++-- .../Constraints/TimezoneValidator.php | 58 +++++++++++------ .../Constraints/TimezoneValidatorTest.php | 64 ++++++++++--------- 3 files changed, 76 insertions(+), 58 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Timezone.php b/src/Symfony/Component/Validator/Constraints/Timezone.php index 8aa09997d1359..783946014539d 100644 --- a/src/Symfony/Component/Validator/Constraints/Timezone.php +++ b/src/Symfony/Component/Validator/Constraints/Timezone.php @@ -27,23 +27,21 @@ class Timezone extends Constraint const NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b'; public $zone; - public $countryCode; - - public $message = 'This value is not a valid timezone{{ extra_info }}.'; + public $message = 'This value is not a valid timezone{{ zone_message }}{{ country_code_message }}.'; protected static $errorNames = array( self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR', + self::NO_SUCH_TIMEZONE_IN_ZONE_ERROR => 'NO_SUCH_TIMEZONE_IN_ZONE_ERROR', + self::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR => 'NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR', ); /** * {@inheritdoc} */ - public function __construct($options = null) + public function __construct(array $options = null) { - if (isset($options['zone'])) { - $this->zone = $options['zone']; - } + $this->zone = $options['zone'] ?? null; if (isset($options['countryCode'])) { if (\DateTimeZone::PER_COUNTRY !== $this->zone) { diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index 406c0b2eedf64..02fddbff860f0 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -41,7 +41,13 @@ public function validate($value, Constraint $constraint) $value = (string) $value; $zone = null !== $constraint->zone ? $constraint->zone : \DateTimeZone::ALL; - $timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode); + + // @see: https://bugs.php.net/bug.php?id=75928 + if ($constraint->countryCode) { + $timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode); + } else { + $timezoneIds = \DateTimeZone::listIdentifiers($zone); + } if ($timezoneIds && !in_array($value, $timezoneIds, true)) { if ($constraint->countryCode) { @@ -52,10 +58,16 @@ public function validate($value, Constraint $constraint) $code = Timezone::NO_SUCH_TIMEZONE_ERROR; } - $this->context->buildViolation($constraint->message) - ->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->zone, $constraint->countryCode)) + $violation = $this->context->buildViolation($constraint->message); + + foreach ($this->generateValidationMessage($constraint->zone, $constraint->countryCode) as $placeholder => $message) { + $violation->setParameter($placeholder, $message); + } + + $violation ->setCode($code) - ->addViolation(); + ->addViolation() + ; } } @@ -68,31 +80,35 @@ public function getDefaultOption() } /** - * Format the extra info which is appended to validation message based on - * constraint options. + * Generates the replace parameters which are used in validation message. * * @param int|null $zone * @param string|null $countryCode * - * @return string + * @return array */ - private function formatExtraInfo($zone, $countryCode = null) + private function generateValidationMessage(int $zone = null, string $countryCode = null): array { - if (null === $zone) { - return ''; - } - if ($countryCode) { - $value = ' for ISO 3166-1 country code "'.$countryCode.'"'; - } else { - $r = new \ReflectionClass('\DateTimeZone'); - $consts = $r->getConstants(); - if ($value = array_search($zone, $consts, true)) { - $value = ' for "'.$value.'" zone'; - } else { - $value = ' for zone with identifier '.$zone; + $values = array( + '{{ country_code_message }}' => '', + '{{ zone_message }}' => '', + ); + + if (null !== $zone) { + if (\DateTimeZone::PER_COUNTRY !== $zone) { + $r = new \ReflectionClass(\DateTimeZone::class); + $consts = $r->getConstants(); + if ($zoneFound = array_search($zone, $consts, true)) { + $values['{{ zone_message }}'] = ' at "'.$zoneFound.'" zone'; + } else { + $values['{{ zone_message }}'] = ' at zone with identifier '.$zone; + } + } + if ($countryCode) { + $values['{{ country_code_message }}'] = ' for ISO 3166-1 country code "'.$countryCode.'"'; } } - return $value; + return $values; } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index 18b17c8166473..6cdfe43848710 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -20,7 +20,7 @@ */ class TimezoneValidatorTest extends ConstraintValidatorTestCase { - protected function createValidator() + protected function createValidator(): TimezoneValidator { return new TimezoneValidator(); } @@ -50,14 +50,14 @@ public function testExpectsStringCompatibleType() /** * @dataProvider getValidTimezones */ - public function testValidTimezones($timezone) + public function testValidTimezones(string $timezone) { $this->validator->validate($timezone, new Timezone()); $this->assertNoViolation(); } - public function getValidTimezones() + public function getValidTimezones(): iterable { return array( array('America/Argentina/Buenos_Aires'), @@ -73,7 +73,7 @@ public function getValidTimezones() /** * @dataProvider getValidGroupedTimezones */ - public function testValidGroupedTimezones($timezone, $what) + public function testValidGroupedTimezones(string $timezone, int $what) { $constraint = new Timezone(array( 'zone' => $what, @@ -84,7 +84,7 @@ public function testValidGroupedTimezones($timezone, $what) $this->assertNoViolation(); } - public function getValidGroupedTimezones() + public function getValidGroupedTimezones(): iterable { return array( array('America/Argentina/Cordoba', \DateTimeZone::AMERICA), @@ -106,7 +106,7 @@ public function getValidGroupedTimezones() /** * @dataProvider getInvalidTimezones */ - public function testInvalidTimezonesWithoutZone($timezone, $extraInfo) + public function testInvalidTimezonesWithoutZone(string $timezone, string $zoneMessage, string $countryCodeMessage) { $constraint = new Timezone(array( 'message' => 'myMessage', @@ -115,24 +115,25 @@ public function testInvalidTimezonesWithoutZone($timezone, $extraInfo) $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ extra_info }}', $extraInfo) + ->setParameter('{{ zone_message }}', $zoneMessage) + ->setParameter('{{ country_code_message }}', $countryCodeMessage) ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) ->assertRaised(); } - public function getInvalidTimezones() + public function getInvalidTimezones(): iterable { return array( - array('Buenos_Aires/Argentina/America', ''), - array('Mayotte/Indian', ''), - array('foobar', ''), + array('Buenos_Aires/Argentina/America', '', ''), + array('Mayotte/Indian', '', ''), + array('foobar', '', ''), ); } /** * @dataProvider getInvalidGroupedTimezones */ - public function testInvalidGroupedTimezones($timezone, $what, $extraInfo) + public function testInvalidGroupedTimezones(string $timezone, int $what, string $zoneMessage, string $countryCodeMessage) { $constraint = new Timezone(array( 'zone' => $what, @@ -142,26 +143,27 @@ public function testInvalidGroupedTimezones($timezone, $what, $extraInfo) $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ extra_info }}', $extraInfo) + ->setParameter('{{ zone_message }}', $zoneMessage) + ->setParameter('{{ country_code_message }}', $countryCodeMessage) ->setCode(Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR) ->assertRaised(); } - public function getInvalidGroupedTimezones() + public function getInvalidGroupedTimezones(): iterable { return array( - array('Antarctica/McMurdo', \DateTimeZone::AMERICA, ' for "AMERICA" zone'), - array('America/Barbados', \DateTimeZone::ANTARCTICA, ' for "ANTARCTICA" zone'), - array('Europe/Kiev', \DateTimeZone::ARCTIC, ' for "ARCTIC" zone'), - array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN, ' for "INDIAN" zone'), - array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA, ' for zone with identifier 260'), + array('Antarctica/McMurdo', \DateTimeZone::AMERICA, ' at "AMERICA" zone', ''), + array('America/Barbados', \DateTimeZone::ANTARCTICA, ' at "ANTARCTICA" zone', ''), + array('Europe/Kiev', \DateTimeZone::ARCTIC, ' at "ARCTIC" zone', ''), + array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN, ' at "INDIAN" zone', ''), + array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA, ' at zone with identifier 260', ''), ); } /** * @dataProvider getValidGroupedTimezonesByCountry */ - public function testValidGroupedTimezonesByCountry($timezone, $what, $country) + public function testValidGroupedTimezonesByCountry(string $timezone, int $what, string $country) { $constraint = new Timezone(array( 'zone' => $what, @@ -173,7 +175,7 @@ public function testValidGroupedTimezonesByCountry($timezone, $what, $country) $this->assertNoViolation(); } - public function getValidGroupedTimezonesByCountry() + public function getValidGroupedTimezonesByCountry(): iterable { return array( array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'AR'), @@ -195,7 +197,7 @@ public function getValidGroupedTimezonesByCountry() /** * @dataProvider getInvalidGroupedTimezonesByCountry */ - public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country, $extraInfo) + public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what, string $country, string $zoneMessage, string $countryCodeMessage) { $constraint = new Timezone(array( 'message' => 'myMessage', @@ -206,23 +208,24 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country, $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ extra_info }}', $extraInfo) + ->setParameter('{{ zone_message }}', $zoneMessage) + ->setParameter('{{ country_code_message }}', $countryCodeMessage) ->setCode(Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR) ->assertRaised(); } - public function getInvalidGroupedTimezonesByCountry() + public function getInvalidGroupedTimezonesByCountry(): iterable { return array( - array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR', ' for ISO 3166-1 country code "FR"'), - array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', ' for ISO 3166-1 country code "PT"'), + array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR', '', ' for ISO 3166-1 country code "FR"'), + array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', '', ' for ISO 3166-1 country code "PT"'), ); } /** * @dataProvider getDeprecatedTimezones */ - public function testDeprecatedTimezonesAreVaildWithBC($timezone) + public function testDeprecatedTimezonesAreVaildWithBC(string $timezone) { $constraint = new Timezone(array( 'zone' => \DateTimeZone::ALL_WITH_BC, @@ -236,7 +239,7 @@ public function testDeprecatedTimezonesAreVaildWithBC($timezone) /** * @dataProvider getDeprecatedTimezones */ - public function testDeprecatedTimezonesAreInvaildWithoutBC($timezone) + public function testDeprecatedTimezonesAreInvaildWithoutBC(string $timezone) { $constraint = new Timezone(array( 'message' => 'myMessage', @@ -245,12 +248,13 @@ public function testDeprecatedTimezonesAreInvaildWithoutBC($timezone) $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ extra_info }}', '') + ->setParameter('{{ zone_message }}', '') + ->setParameter('{{ country_code_message }}', '') ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) ->assertRaised(); } - public function getDeprecatedTimezones() + public function getDeprecatedTimezones(): iterable { return array( array('America/Buenos_Aires'), From 14689f7095a2884a444695afade0f7ed6d0dac71 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Thu, 29 Nov 2018 10:14:06 -0300 Subject: [PATCH 13/14] Update `TimezoneValidator` --- .../Validator/Constraints/Timezone.php | 17 ++---- .../Constraints/TimezoneValidator.php | 55 ++++++------------- .../Resources/translations/validators.en.xlf | 4 ++ .../Resources/translations/validators.es.xlf | 4 ++ .../Constraints/TimezoneValidatorTest.php | 38 ++++++------- 5 files changed, 48 insertions(+), 70 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Timezone.php b/src/Symfony/Component/Validator/Constraints/Timezone.php index 783946014539d..485e14b602a63 100644 --- a/src/Symfony/Component/Validator/Constraints/Timezone.php +++ b/src/Symfony/Component/Validator/Constraints/Timezone.php @@ -25,10 +25,9 @@ class Timezone extends Constraint const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a'; const NO_SUCH_TIMEZONE_IN_ZONE_ERROR = 'b57767b1-36c0-40ac-a3d7-629420c775b8'; const NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b'; - - public $zone; + public $zone = \DateTimeZone::ALL; public $countryCode; - public $message = 'This value is not a valid timezone{{ zone_message }}{{ country_code_message }}.'; + public $message = 'This value is not a valid timezone.'; protected static $errorNames = array( self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR', @@ -41,16 +40,10 @@ class Timezone extends Constraint */ public function __construct(array $options = null) { - $this->zone = $options['zone'] ?? null; - - if (isset($options['countryCode'])) { - if (\DateTimeZone::PER_COUNTRY !== $this->zone) { - throw new ConstraintDefinitionException('The option "countryCode" can only be used when "zone" option has `\DateTimeZone::PER_COUNTRY` as value'); - } + parent::__construct($options); - $this->countryCode = $options['countryCode']; + if ($this->countryCode && \DateTimeZone::PER_COUNTRY !== $this->zone) { + throw new ConstraintDefinitionException('The option "countryCode" can only be used when "zone" option has `\DateTimeZone::PER_COUNTRY` as value'); } - - parent::__construct($options); } } diff --git a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php index 02fddbff860f0..88030595d5e93 100644 --- a/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimezoneValidator.php @@ -35,39 +35,32 @@ public function validate($value, Constraint $constraint) return; } - if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { + if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { throw new UnexpectedTypeException($value, 'string'); } $value = (string) $value; - $zone = null !== $constraint->zone ? $constraint->zone : \DateTimeZone::ALL; // @see: https://bugs.php.net/bug.php?id=75928 if ($constraint->countryCode) { - $timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode); + $timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode); } else { - $timezoneIds = \DateTimeZone::listIdentifiers($zone); + $timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone); } - if ($timezoneIds && !in_array($value, $timezoneIds, true)) { + if ($timezoneIds && !\in_array($value, $timezoneIds, true)) { if ($constraint->countryCode) { $code = Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR; - } elseif (null !== $constraint->zone) { + } elseif (\DateTimeZone::ALL !== $constraint->zone) { $code = Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR; } else { $code = Timezone::NO_SUCH_TIMEZONE_ERROR; } - $violation = $this->context->buildViolation($constraint->message); - - foreach ($this->generateValidationMessage($constraint->zone, $constraint->countryCode) as $placeholder => $message) { - $violation->setParameter($placeholder, $message); - } - - $violation + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode($code) - ->addViolation() - ; + ->addViolation(); } } @@ -80,35 +73,23 @@ public function getDefaultOption() } /** - * Generates the replace parameters which are used in validation message. - * - * @param int|null $zone - * @param string|null $countryCode - * - * @return array + * {@inheritdoc} */ - private function generateValidationMessage(int $zone = null, string $countryCode = null): array + protected function formatValue($value, $format = 0) { - $values = array( - '{{ country_code_message }}' => '', - '{{ zone_message }}' => '', - ); - - if (null !== $zone) { - if (\DateTimeZone::PER_COUNTRY !== $zone) { + $value = parent::formatValue($value, $format); + if ($value) { + if (\DateTimeZone::PER_COUNTRY !== $value) { $r = new \ReflectionClass(\DateTimeZone::class); $consts = $r->getConstants(); - if ($zoneFound = array_search($zone, $consts, true)) { - $values['{{ zone_message }}'] = ' at "'.$zoneFound.'" zone'; - } else { - $values['{{ zone_message }}'] = ' at zone with identifier '.$zone; + if ($zoneFound = array_search($value, $consts, true)) { + return $zoneFound; } - } - if ($countryCode) { - $values['{{ country_code_message }}'] = ' for ISO 3166-1 country code "'.$countryCode.'"'; + + return $value; } } - return $values; + return $value; } } diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf index 4bb2760b418e3..ce058b2a3587a 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf @@ -326,6 +326,10 @@ This value should be a multiple of {{ compared_value }}. This value should be a multiple of {{ compared_value }}. + + This value is not a valid timezone. + This value is not a valid timezone. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf index 18eb8f4ca2da3..989f14592e280 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf @@ -326,6 +326,10 @@ This value should be a multiple of {{ compared_value }}. Este valor debería ser un múltiplo de {{ compared_value }}. + + This value is not a valid timezone. + Este valor no es una zona horaria válida. + diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php index 6cdfe43848710..41422e31d92db 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php @@ -106,7 +106,7 @@ public function getValidGroupedTimezones(): iterable /** * @dataProvider getInvalidTimezones */ - public function testInvalidTimezonesWithoutZone(string $timezone, string $zoneMessage, string $countryCodeMessage) + public function testInvalidTimezonesWithoutZone(string $timezone) { $constraint = new Timezone(array( 'message' => 'myMessage', @@ -115,8 +115,7 @@ public function testInvalidTimezonesWithoutZone(string $timezone, string $zoneMe $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ zone_message }}', $zoneMessage) - ->setParameter('{{ country_code_message }}', $countryCodeMessage) + ->setParameter('{{ value }}', '"'.$timezone.'"') ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) ->assertRaised(); } @@ -124,16 +123,16 @@ public function testInvalidTimezonesWithoutZone(string $timezone, string $zoneMe public function getInvalidTimezones(): iterable { return array( - array('Buenos_Aires/Argentina/America', '', ''), - array('Mayotte/Indian', '', ''), - array('foobar', '', ''), + array('Buenos_Aires/Argentina/America'), + array('Mayotte/Indian'), + array('foobar'), ); } /** * @dataProvider getInvalidGroupedTimezones */ - public function testInvalidGroupedTimezones(string $timezone, int $what, string $zoneMessage, string $countryCodeMessage) + public function testInvalidGroupedTimezones(string $timezone, int $what) { $constraint = new Timezone(array( 'zone' => $what, @@ -143,8 +142,7 @@ public function testInvalidGroupedTimezones(string $timezone, int $what, string $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ zone_message }}', $zoneMessage) - ->setParameter('{{ country_code_message }}', $countryCodeMessage) + ->setParameter('{{ value }}', '"'.$timezone.'"') ->setCode(Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR) ->assertRaised(); } @@ -152,11 +150,11 @@ public function testInvalidGroupedTimezones(string $timezone, int $what, string public function getInvalidGroupedTimezones(): iterable { return array( - array('Antarctica/McMurdo', \DateTimeZone::AMERICA, ' at "AMERICA" zone', ''), - array('America/Barbados', \DateTimeZone::ANTARCTICA, ' at "ANTARCTICA" zone', ''), - array('Europe/Kiev', \DateTimeZone::ARCTIC, ' at "ARCTIC" zone', ''), - array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN, ' at "INDIAN" zone', ''), - array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA, ' at zone with identifier 260', ''), + array('Antarctica/McMurdo', \DateTimeZone::AMERICA), + array('America/Barbados', \DateTimeZone::ANTARCTICA), + array('Europe/Kiev', \DateTimeZone::ARCTIC), + array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN), + array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA), ); } @@ -197,7 +195,7 @@ public function getValidGroupedTimezonesByCountry(): iterable /** * @dataProvider getInvalidGroupedTimezonesByCountry */ - public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what, string $country, string $zoneMessage, string $countryCodeMessage) + public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what, string $country) { $constraint = new Timezone(array( 'message' => 'myMessage', @@ -208,8 +206,7 @@ public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ zone_message }}', $zoneMessage) - ->setParameter('{{ country_code_message }}', $countryCodeMessage) + ->setParameter('{{ value }}', '"'.$timezone.'"') ->setCode(Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR) ->assertRaised(); } @@ -217,8 +214,8 @@ public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what public function getInvalidGroupedTimezonesByCountry(): iterable { return array( - array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR', '', ' for ISO 3166-1 country code "FR"'), - array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', '', ' for ISO 3166-1 country code "PT"'), + array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR'), + array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT'), ); } @@ -248,8 +245,7 @@ public function testDeprecatedTimezonesAreInvaildWithoutBC(string $timezone) $this->validator->validate($timezone, $constraint); $this->buildViolation('myMessage') - ->setParameter('{{ zone_message }}', '') - ->setParameter('{{ country_code_message }}', '') + ->setParameter('{{ value }}', '"'.$timezone.'"') ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) ->assertRaised(); } From c3dce5c7e4fbb1be64f5a41f899335a9c8176bc9 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Thu, 29 Nov 2018 11:03:58 -0300 Subject: [PATCH 14/14] Add simple assertion at `TimezoneTest::testValidTimezoneConstraints()` --- .../Component/Validator/Tests/Constraints/TimezoneTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php index a3fea101a2a59..cd9e556a8885e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php @@ -33,6 +33,9 @@ public function testValidTimezoneConstraints() 'message' => 'myMessage', 'zone' => \DateTimeZone::ALL, )); + + // Make an assertion in order to avoid this test to be marked as risky + $this->assertInstanceOf(Timezone::class, $constraint); } /**