Skip to content

Commit

Permalink
Merge pull request #102 from KDederichs/fix_101
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- authored Feb 24, 2022
2 parents a119943 + 88ddd3a commit d6e588d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/Validator/Constraints/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +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);
} elseif (null !== $format) {
$options['value'] = $format;
} 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;
Expand Down
50 changes: 23 additions & 27 deletions tests/Validator/Constraints/PhoneNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Misd\PhoneNumberBundle\Tests\Validator\Constraints;

use libphonenumber\PhoneNumberFormat;
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber;
use PHPUnit\Framework\TestCase;

Expand All @@ -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.'],
];
}
}
19 changes: 5 additions & 14 deletions tests/Validator/Constraints/PhoneNumberValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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],
];
}

Expand Down

0 comments on commit d6e588d

Please sign in to comment.