update v 1.0.7.5
This commit is contained in:
@@ -6,205 +6,361 @@ use Orchestra\Testbench\TestCase;
|
||||
class PhoneValidatorTest extends TestCase
|
||||
{
|
||||
|
||||
protected $validator;
|
||||
protected $validator;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->validator = $this->app['validator'];
|
||||
}
|
||||
$this->validator = $this->app['validator'];
|
||||
}
|
||||
|
||||
private function performValidation($data)
|
||||
{
|
||||
$rule = 'phone' . (isset($data['rule']) ? ':' . $data['rule'] : '');
|
||||
private function performValidation($data)
|
||||
{
|
||||
$rule = 'phone' . (isset($data['rule']) ? ':' . $data['rule'] : '');
|
||||
|
||||
$input = array_only($data, ['field', 'field_country']);
|
||||
$input['nested']['field'] = $input['field'];
|
||||
if (isset($input['field_country'])) {
|
||||
$input['nested']['field_country'] = $input['field_country'];
|
||||
}
|
||||
$input = array_except($data, ['rule']);
|
||||
$input['nested']['field'] = $input['field'];
|
||||
if (isset($input['field_country'])) {
|
||||
$input['nested']['field_country'] = $input['field_country'];
|
||||
}
|
||||
|
||||
$validator = $this->validator->make($input,
|
||||
['field' => $rule,
|
||||
'nested.field' => $rule]
|
||||
);
|
||||
$validator = $this->validator->make($input,
|
||||
[
|
||||
'field' => $rule,
|
||||
'nested.field' => $rule
|
||||
]
|
||||
);
|
||||
|
||||
return $validator->passes();
|
||||
}
|
||||
return $validator->passes();
|
||||
}
|
||||
|
||||
public function testValidatePhoneWithDefaultCountryNoType()
|
||||
{
|
||||
// Validator with correct country field.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'rule' => 'BE']));
|
||||
public function testValidatePhoneWithDefaultCountryNoType()
|
||||
{
|
||||
// Validator with correct country field.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with wrong country value.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'NL']));
|
||||
// Validator with wrong country value.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'NL'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, one correct.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'rule' => 'BE,NL']));
|
||||
// Validator with multiple country values, one correct.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'BE,NL'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, value correct for second country in list.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'rule' => 'NL,BE']));
|
||||
// Validator with multiple country values, value correct for second country in list.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'NL,BE'
|
||||
]));
|
||||
|
||||
// Validator with multiple wrong country values.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'DE,NL']));
|
||||
}
|
||||
// Validator with multiple wrong country values.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'DE,NL'
|
||||
]));
|
||||
}
|
||||
|
||||
public function testValidatePhoneWithCountryFieldNoType()
|
||||
{
|
||||
// Validator with correct country field supplied.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'field_country' => 'BE']));
|
||||
public function testValidatePhoneWithCountryFieldNoType()
|
||||
{
|
||||
// Validator with correct country field supplied.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'field_country' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with wrong country field supplied.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'field_country' => 'NL']));
|
||||
}
|
||||
// Validator with wrong country field supplied.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'field_country' => 'NL'
|
||||
]));
|
||||
}
|
||||
|
||||
public function testValidatePhoneWithDefaultCountryWithType()
|
||||
{
|
||||
// Validator with correct country value, correct type.
|
||||
$this->assertTrue($this->performValidation(['field' => '0499123456', 'rule' => 'BE,mobile']));
|
||||
public function testValidatePhoneWithDefaultCountryWithType()
|
||||
{
|
||||
// Validator with correct country value, correct type.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'BE,mobile'
|
||||
]));
|
||||
|
||||
// Validator with correct country value, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'BE,mobile']));
|
||||
// Validator with correct country value, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'BE,mobile'
|
||||
]));
|
||||
|
||||
// Validator with wrong country value, correct type.
|
||||
$this->assertFalse($this->performValidation(['field' => '0499123456', 'rule' => 'NL,mobile']));
|
||||
// Validator with wrong country value, correct type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'NL,mobile'
|
||||
]));
|
||||
|
||||
// Validator with wrong country value, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'NL,mobile']));
|
||||
// Validator with wrong country value, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'NL,mobile'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, one correct, correct type.
|
||||
$this->assertTrue($this->performValidation(['field' => '0499123456', 'rule' => 'BE,NL,mobile']));
|
||||
// Validator with multiple country values, one correct, correct type.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'BE,NL,mobile'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, one correct, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'BE,NL,mobile']));
|
||||
// Validator with multiple country values, one correct, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'BE,NL,mobile'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, none correct, correct type.
|
||||
$this->assertFalse($this->performValidation(['field' => '0499123456', 'rule' => 'DE,NL,mobile']));
|
||||
// Validator with multiple country values, none correct, correct type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'DE,NL,mobile'
|
||||
]));
|
||||
|
||||
// Validator with multiple country values, none correct, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'DE,NL,mobile']));
|
||||
}
|
||||
// Validator with multiple country values, none correct, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'DE,NL,mobile'
|
||||
]));
|
||||
}
|
||||
|
||||
public function testValidatePhoneWithCountryFieldWithType()
|
||||
{
|
||||
// Validator with correct country field supplied, correct type.
|
||||
$this->assertTrue($this->performValidation(['field' => '0499123456', 'rule' => 'mobile', 'field_country' => 'BE']));
|
||||
public function testValidatePhoneWithCountryFieldWithType()
|
||||
{
|
||||
// Validator with correct country field supplied, correct type.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile',
|
||||
'field_country' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with correct country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'mobile', 'field_country' => 'BE'
|
||||
]));
|
||||
// Validator with correct country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile',
|
||||
'field_country' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with wrong country field supplied, correct type.
|
||||
$this->assertFalse($this->performValidation(['field' => '0499123456', 'rule' => 'mobile', 'field_country' => 'NL'
|
||||
]));
|
||||
// Validator with wrong country field supplied, correct type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile',
|
||||
'field_country' => 'NL'
|
||||
]));
|
||||
|
||||
// Validator with wrong country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'mobile', 'field_country' => 'NL'
|
||||
]));
|
||||
}
|
||||
// Validator with wrong country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile',
|
||||
'field_country' => 'NL'
|
||||
]));
|
||||
}
|
||||
|
||||
public function testValidatePhoneAutomaticDetectionFromInternationalInput()
|
||||
{
|
||||
// Validator with correct international input.
|
||||
$this->assertTrue($this->performValidation(['field' => '+3216123456', 'rule' => 'AUTO']));
|
||||
public function testValidatePhoneWithCustomCountryField()
|
||||
{
|
||||
// Validator with correct country field supplied, correct type.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile,country_code',
|
||||
'country_code' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with wrong international input.
|
||||
$this->assertFalse($this->performValidation(['field' => '003216123456', 'rule' => 'AUTO']));
|
||||
// Validator with correct country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile,country_code',
|
||||
'country_code' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with wrong international input.
|
||||
$this->assertFalse($this->performValidation(['field' => '+321456', 'rule' => 'AUTO']));
|
||||
// Validator with wrong country field supplied, correct type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile,country_code',
|
||||
'country_code' => 'NL'
|
||||
]));
|
||||
|
||||
// Validator with wrong international input but correct default country.
|
||||
$this->assertTrue($this->performValidation(['field' => '016123456', 'rule' => 'AUTO,NL,BE']));
|
||||
// Validator with wrong country field supplied, wrong type.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile,country_code',
|
||||
'country_code' => 'NL'
|
||||
]));
|
||||
}
|
||||
|
||||
// Validator with wrong international input and wrong default country.
|
||||
$this->assertFalse($this->performValidation(['field' => '016123456', 'rule' => 'AUTO,NL,DE']));
|
||||
}
|
||||
public function testValidatePhoneAutomaticDetectionFromInternationalInput()
|
||||
{
|
||||
// Validator with correct international input.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '+3216123456',
|
||||
'rule' => 'AUTO'
|
||||
]));
|
||||
|
||||
public function testValidatePhoneNoDefaultCountryNoCountryField()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException');
|
||||
// Validator with wrong international input.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '003216123456',
|
||||
'rule' => 'AUTO'
|
||||
]));
|
||||
|
||||
// Validator with no country field or given country.
|
||||
$this->performValidation(['field' => '016123456']);
|
||||
// Validator with wrong international input.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '+321456',
|
||||
'rule' => 'AUTO'
|
||||
]));
|
||||
|
||||
// Validator with no country field or given country, wrong type.
|
||||
$this->performValidation(['field' => '016123456', 'rule' => 'mobile']);
|
||||
// Validator with wrong international input but correct default country.
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'AUTO,NL,BE'
|
||||
]));
|
||||
|
||||
// Validator with no country field or given country, correct type.
|
||||
$this->performValidation(['field' => '0499123456', 'rule' => 'mobile']);
|
||||
// Validator with wrong international input and wrong default country.
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'AUTO,NL,DE'
|
||||
]));
|
||||
}
|
||||
|
||||
// Validator with no country field or given country, correct type, faulty parameter.
|
||||
$this->performValidation(['field' => '0499123456', 'rule' => 'mobile,xyz']);
|
||||
}
|
||||
public function testValidatePhoneNoDefaultCountryNoCountryField()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException');
|
||||
|
||||
public function testValidatePhoneLenient()
|
||||
{
|
||||
// Validator with AU area code, lenient off
|
||||
$this->assertFalse($this->performValidation(['field' => '88885555', 'rule' => 'AU']));
|
||||
// Validator with no country field or given country.
|
||||
$this->performValidation([
|
||||
'field' => '016123456'
|
||||
]);
|
||||
|
||||
// Validator with AU area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT,AU']));
|
||||
// Validator with no country field or given country, wrong type.
|
||||
$this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile'
|
||||
]);
|
||||
|
||||
// Validator with correct country field supplied, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT', 'field_country' => 'AU']));
|
||||
// Validator with no country field or given country, correct type.
|
||||
$this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile'
|
||||
]);
|
||||
|
||||
// Validator with wrong country field supplied, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '88885555', 'rule' => 'LENIENT', 'field_country' => 'BE']));
|
||||
// Validator with no country field or given country, correct type, faulty parameter.
|
||||
$this->performValidation([
|
||||
'field' => '0499123456',
|
||||
'rule' => 'mobile,xyz'
|
||||
]);
|
||||
}
|
||||
|
||||
// Validator with no area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '+16502530000', 'rule' => 'LENIENT']));
|
||||
public function testValidatePhoneLenient()
|
||||
{
|
||||
// Validator with AU area code, lenient off
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '88885555',
|
||||
'rule' => 'AU'
|
||||
]));
|
||||
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '+16502530000', 'rule' => 'LENIENT,US']));
|
||||
// Validator with AU area code, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '88885555',
|
||||
'rule' => 'LENIENT,AU'
|
||||
]));
|
||||
|
||||
// Validator with no area code, lenient off
|
||||
$this->assertFalse($this->performValidation(['field' => '6502530000', 'rule' => 'LENIENT']));
|
||||
// Validator with correct country field supplied, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '88885555',
|
||||
'rule' => 'LENIENT',
|
||||
'field_country' => 'AU'
|
||||
]));
|
||||
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '6502530000', 'rule' => 'LENIENT,US']));
|
||||
// Validator with wrong country field supplied, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '88885555',
|
||||
'rule' => 'LENIENT',
|
||||
'field_country' => 'BE'
|
||||
]));
|
||||
|
||||
// Validator with US area code, lenient off
|
||||
$this->assertFalse($this->performValidation(['field' => '2530000', 'rule' => 'LENIENT']));
|
||||
// Validator with no area code, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '+16502530000',
|
||||
'rule' => 'LENIENT'
|
||||
]));
|
||||
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation(['field' => '2530000', 'rule' => 'LENIENT,US']));
|
||||
}
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '+16502530000',
|
||||
'rule' => 'LENIENT,US'
|
||||
]));
|
||||
|
||||
public function testValidatePhoneFaultyParameters()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\InvalidParameterException');
|
||||
// Validator with no area code, lenient off
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '6502530000',
|
||||
'rule' => 'LENIENT'
|
||||
]));
|
||||
|
||||
// Validator with given country, correct type, faulty parameter.
|
||||
$this->performValidation(['field' => '016123456', 'rule' => 'BE,mobile,xyz']);
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '6502530000',
|
||||
'rule' => 'LENIENT,US'
|
||||
]));
|
||||
|
||||
// Validator with country field, correct type, faulty parameter.
|
||||
$this->performValidation(['field' => '016123456', 'rule' => 'mobile,xyz', 'field_country' => 'BE']);
|
||||
}
|
||||
// Validator with US area code, lenient off
|
||||
$this->assertFalse($this->performValidation([
|
||||
'field' => '2530000',
|
||||
'rule' => 'LENIENT'
|
||||
]));
|
||||
|
||||
public function testPhoneFormatHelperFunction()
|
||||
{
|
||||
// Test landline number without format parameter.
|
||||
$actual = phone_format('016123456', 'BE');
|
||||
$expected = '+32 16 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
// Validator with US area code, lenient on
|
||||
$this->assertTrue($this->performValidation([
|
||||
'field' => '2530000',
|
||||
'rule' => 'LENIENT,US'
|
||||
]));
|
||||
}
|
||||
|
||||
// Test landline number with format parameter.
|
||||
$actual = phone_format('016123456', 'BE', PhoneNumberFormat::NATIONAL);
|
||||
$expected = '016 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
public function testValidatePhoneFaultyParameters()
|
||||
{
|
||||
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\InvalidParameterException');
|
||||
|
||||
// Test mobile number without format parameter.
|
||||
$actual = phone_format('0499123456', 'BE');
|
||||
$expected = '+32 499 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
// Validator with given country, correct type, faulty parameter.
|
||||
$this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'BE,mobile,xyz'
|
||||
]);
|
||||
|
||||
// Test mobile number with format parameter.
|
||||
$actual = phone_format('0499123456', 'BE', PhoneNumberFormat::NATIONAL);
|
||||
$expected = '0499 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
// Validator with country field, correct type, faulty parameter.
|
||||
$this->performValidation([
|
||||
'field' => '016123456',
|
||||
'rule' => 'mobile,xyz',
|
||||
'field_country' => 'BE'
|
||||
]);
|
||||
}
|
||||
|
||||
public function testPhoneFormatHelperFunction()
|
||||
{
|
||||
// Test landline number without format parameter.
|
||||
$actual = phone_format('016123456', 'BE');
|
||||
$expected = '+32 16 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
// Test landline number with format parameter.
|
||||
$actual = phone_format('016123456', 'BE', PhoneNumberFormat::NATIONAL);
|
||||
$expected = '016 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
// Test mobile number without format parameter.
|
||||
$actual = phone_format('0499123456', 'BE');
|
||||
$expected = '+32 499 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
// Test mobile number with format parameter.
|
||||
$actual = phone_format('0499123456', 'BE', PhoneNumberFormat::NATIONAL);
|
||||
$expected = '0499 12 34 56';
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user