From 8ef8b5d75d01cb74e769a9386ea2b9db6c3fcfaf Mon Sep 17 00:00:00 2001 From: Kai Dederichs Date: Fri, 28 Jan 2022 12:50:26 +0100 Subject: [PATCH 1/3] Fix constructor breaking with format as input Fix CS --- src/Validator/Constraints/PhoneNumber.php | 6 --- .../Validator/Constraints/PhoneNumberTest.php | 50 +++++++++---------- .../Constraints/PhoneNumberValidatorTest.php | 19 ++----- 3 files changed, 28 insertions(+), 47 deletions(-) diff --git a/src/Validator/Constraints/PhoneNumber.php b/src/Validator/Constraints/PhoneNumber.php index b2dd8356..9412b976 100644 --- a/src/Validator/Constraints/PhoneNumber.php +++ b/src/Validator/Constraints/PhoneNumber.php @@ -54,12 +54,6 @@ class PhoneNumber extends Constraint */ public function __construct($format = null, $type = null, string $defaultRegion = null, string $regionPath = null, string $message = null, array $groups = null, $payload = null, array $options = []) { - if (\is_array($format)) { - $options = array_merge($format, $options); - } elseif (null !== $format) { - $options['value'] = $format; - } - parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; diff --git a/tests/Validator/Constraints/PhoneNumberTest.php b/tests/Validator/Constraints/PhoneNumberTest.php index ceb348e8..f1ec5ce1 100644 --- a/tests/Validator/Constraints/PhoneNumberTest.php +++ b/tests/Validator/Constraints/PhoneNumberTest.php @@ -11,6 +11,7 @@ namespace Misd\PhoneNumberBundle\Tests\Validator\Constraints; +use libphonenumber\PhoneNumberFormat; use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber; use PHPUnit\Framework\TestCase; @@ -32,44 +33,39 @@ public function testProperties() /** * @dataProvider messageProvider */ - public function testMessage($message, $type, $expectedMessage) + public function testMessage($message, $type, $format, $expectedMessage) { - $phoneNumber = new PhoneNumber(); - - if (null !== $message) { - $phoneNumber->message = $message; - } - if (null !== $type) { - $phoneNumber->type = $type; - } - + $phoneNumber = new PhoneNumber($format, $type, null, null, $message); $this->assertSame($expectedMessage, $phoneNumber->getMessage()); + $this->assertSame($format, $phoneNumber->format); } /** * 0 => Message (optional) * 1 => Type (optional) - * 2 => Expected message. + * 2 => Format (optional) + * 3 => Expected message. */ public function messageProvider() { return [ - [null, null, 'This value is not a valid phone number.'], - [null, 'fixed_line', 'This value is not a valid fixed-line number.'], - [null, 'mobile', 'This value is not a valid mobile number.'], - [null, 'pager', 'This value is not a valid pager number.'], - [null, 'personal_number', 'This value is not a valid personal number.'], - [null, 'premium_rate', 'This value is not a valid premium-rate number.'], - [null, 'shared_cost', 'This value is not a valid shared-cost number.'], - [null, 'toll_free', 'This value is not a valid toll-free number.'], - [null, 'uan', 'This value is not a valid UAN.'], - [null, 'voip', 'This value is not a valid VoIP number.'], - [null, 'voicemail', 'This value is not a valid voicemail access number.'], - [null, ['fixed_line', 'voip'], 'This value is not a valid number.'], - [null, ['uan', 'fixed_line'], 'This value is not a valid number.'], - ['foo', null, 'foo'], - ['foo', 'fixed_line', 'foo'], - ['foo', 'mobile', 'foo'], + [null, null, null, 'This value is not a valid phone number.'], + [null, 'fixed_line', null, 'This value is not a valid fixed-line number.'], + [null, 'mobile', null, 'This value is not a valid mobile number.'], + [null, 'pager', null, 'This value is not a valid pager number.'], + [null, 'personal_number', null, 'This value is not a valid personal number.'], + [null, 'premium_rate', null, 'This value is not a valid premium-rate number.'], + [null, 'shared_cost', null, 'This value is not a valid shared-cost number.'], + [null, 'toll_free', null, 'This value is not a valid toll-free number.'], + [null, 'uan', null, 'This value is not a valid UAN.'], + [null, 'voip', null, 'This value is not a valid VoIP number.'], + [null, 'voicemail', null, 'This value is not a valid voicemail access number.'], + [null, ['fixed_line', 'voip'], null, 'This value is not a valid number.'], + [null, ['uan', 'fixed_line'], null, 'This value is not a valid number.'], + ['foo', null, null, 'foo'], + ['foo', 'fixed_line', null, 'foo'], + ['foo', 'mobile', null, 'foo'], + [null, null, PhoneNumberFormat::E164, 'This value is not a valid phone number.'], ]; } } diff --git a/tests/Validator/Constraints/PhoneNumberValidatorTest.php b/tests/Validator/Constraints/PhoneNumberValidatorTest.php index d417455d..e4abdf9f 100644 --- a/tests/Validator/Constraints/PhoneNumberValidatorTest.php +++ b/tests/Validator/Constraints/PhoneNumberValidatorTest.php @@ -11,6 +11,7 @@ namespace Misd\PhoneNumberBundle\Tests\Validator\Constraints; +use libphonenumber\PhoneNumberFormat; use libphonenumber\PhoneNumberUtil; use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber; use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumberValidator; @@ -50,21 +51,9 @@ protected function setUp(): void /** * @dataProvider validateProvider */ - public function testValidate($value, $violates, $type = null, $defaultRegion = null, $regionPath = null) + public function testValidate($value, $violates, $type = null, $defaultRegion = null, $regionPath = null, $format = null) { - $constraint = new PhoneNumber(); - - if (null !== $type) { - $constraint->type = $type; - } - - if (null !== $defaultRegion) { - $constraint->defaultRegion = $defaultRegion; - } - - if (null !== $regionPath) { - $constraint->regionPath = $regionPath; - } + $constraint = new PhoneNumber($format, $type, $defaultRegion, $regionPath); if (true === $violates) { $constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class); @@ -162,6 +151,8 @@ public function validateProvider() ['foo', true], ['+441234567890', true, 'mobile', null, 'regionPath'], ['+33606060606', false, 'mobile', null, 'regionPath'], + ['+33606060606', false, 'mobile', null, null, PhoneNumberFormat::E164], + ['2015555555', true, null, null, null, PhoneNumberFormat::E164], ]; } From ab8a128e419be9292910ef671e883a235cc2ce11 Mon Sep 17 00:00:00 2001 From: Maxime Veber Date: Thu, 24 Feb 2022 00:49:12 +0100 Subject: [PATCH 2/3] feat: add bc layer on validation constraints options This is related to #102 that remove the part that I add here. --- src/Validator/Constraints/PhoneNumber.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Validator/Constraints/PhoneNumber.php b/src/Validator/Constraints/PhoneNumber.php index 9412b976..4d24a481 100644 --- a/src/Validator/Constraints/PhoneNumber.php +++ b/src/Validator/Constraints/PhoneNumber.php @@ -49,15 +49,23 @@ class PhoneNumber extends Constraint /** * {@inheritdoc} * - * @param string|array|null $format + * @param int|array|null $format Specify the format (\libphonenumber\PhoneNumberFormat::*) + * or options (an associative array) * @param string|array|null $type */ public function __construct($format = null, $type = null, string $defaultRegion = null, string $regionPath = null, string $message = null, array $groups = null, $payload = null, array $options = []) { + if (\is_array($format)) { + @trigger_error('Usage of the argument $format to specify options is deprecated and will be removed in 4.0. Use "$option" argument instead.', \E_USER_DEPRECATED); + $options = array_merge($format, $options); + } else { + $phoneFormat = $format; + } + parent::__construct($options, $groups, $payload); $this->message = $message ?? $this->message; - $this->format = $format ?? $this->format; + $this->format = $phoneFormat ?? $this->format; $this->type = $type ?? $this->type; $this->defaultRegion = $defaultRegion ?? $this->defaultRegion; $this->regionPath = $regionPath ?? $this->regionPath; From 88ddd3ac282e98eea54dc0d2bac0a76170cfbbfd Mon Sep 17 00:00:00 2001 From: Maxime Veber Date: Thu, 24 Feb 2022 01:03:39 +0100 Subject: [PATCH 3/3] doc: prepare release 3.6.2 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index df374c85..48e7155d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [3.6.2] - 2022-02-24 + +### Fixed + +- Fix option format while using the validation constraint as attribute + ## [3.6.1] - 2021-12-29 - Added return types