Merge remote-tracking branch 'refs/remotes/origin/master' into arindam-bug-priority

This commit is contained in:
Manish Verma
2016-11-04 18:23:31 +05:30
parent e4b9755294
commit 775e0561d8
2541 changed files with 59048 additions and 90348 deletions

View File

@@ -1,3 +1,4 @@
/vendor
/.idea
composer.phar
composer.lock

View File

@@ -8,10 +8,11 @@ filter:
- 'stub/*'
- 'coverage/*'
- 'vendor/*'
checks:
php: true
tools:
external_code_coverage:
timeout: 1200
runs: 3
php_code_coverage: false

View File

@@ -1,25 +1,18 @@
language: php
matrix:
include:
- php: 5.4
env: PHPUNIT=laravel4.phpunit.xml
before_script: composer require orchestra/testbench ~2.0.0
- php: 5.5
env: PHPUNIT=phpunit.xml
- php: 5.6
env: PHPUNIT=phpunit.xml
- php: 7.0
env: PHPUNIT=phpunit.xml
- php: hhvm
env: PHPUNIT=phpunit.xml
php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev
- if [[ $TRAVIS_PHP_VERSION =~ ^hhvm ]]; then echo 'xdebug.enable = On' >> /etc/hhvm/php.ini; fi
- composer install
script:
- phpunit --coverage-clover=coverage.clover --configuration $PHPUNIT
- vendor/bin/phpunit --coverage-clover=coverage.clover
after_script:
- wget https://scrutinizer-ci.com/ocular.phar

View File

@@ -7,49 +7,40 @@
[![Total Downloads](https://poser.pugx.org/propaganistas/laravel-phone/downloads)](https://packagist.org/packages/propaganistas/laravel-phone)
[![License](https://poser.pugx.org/propaganistas/laravel-phone/license)](https://packagist.org/packages/propaganistas/laravel-phone)
Adds a phone validator to Laravel 4 and 5 based on the [PHP port](https://github.com/giggsey/libphonenumber-for-php) of [Google's libphonenumber API](https://github.com/googlei18n/libphonenumber) by [giggsey](https://github.com/giggsey).
Adds a phone validator to Laravel 4|5 and Lumen based on the [PHP port](https://github.com/giggsey/libphonenumber-for-php) of [Google's libphonenumber API](https://github.com/googlei18n/libphonenumber) by [giggsey](https://github.com/giggsey).
### Installation
1. In the `require` key of `composer.json` file add the following
Run the following command to install the latest version of the package
```json
"propaganistas/laravel-phone": "~2.0"
```
```bash
composer require propaganistas/laravel-phone
```
2. Run the Composer update command
**Laravel**
```bash
composer update
```
In your app config, add the Service Provider to the `$providers` array
3. In your app config, add the Service Provider to the end of the `$providers` array
```php
'providers' => [
...
Propaganistas\LaravelPhone\LaravelPhoneServiceProvider::class,
],
```
**Laravel 5**
```php
'providers' => [
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
...
Propaganistas\LaravelPhone\LaravelPhoneServiceProvider::class,
],
```
**Laravel 4**
```php
'providers' => [
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
...
'Propaganistas\LaravelPhone\LaravelPhoneServiceProvider',
],
```
In your languages directory, add for each language an extra language line for the validator:
4. In your languages directory, add for each language an extra language line for the validator:
```php
```php
"phone" => "The :attribute field contains an invalid number.",
```
```
**Lumen**
In `bootstrap/app.php`, register the Service Provider
```php
$app->register(Propaganistas\LaravelPhone\LaravelPhoneServiceProvider::class);
```
### Usage
@@ -102,11 +93,11 @@ You can also enable more lenient validation (for example, fixed lines without ar
'phonefield' => 'phone:LENIENT,US',
```
### Display
Format a fetched phone value using the helper function. `$country_code` is the country the phone number belongs to.
### Helper function
Format a fetched phone value using the `phone()` helper function. `$country_code` is the country the phone number belongs to.
```php
phone_format($phone_number, $country_code = null, $format = PhoneNumberFormat::INTERNATIONAL)
phone($phone_number, $country_code = null, $format = PhoneNumberFormat::INTERNATIONAL)
```
If no `$country_code` was given, the current application locale will be used as default.
The `$format` parameter is optional and should be a constant of `libphonenumber\PhoneNumberFormat` (defaults to `libphonenumber\PhoneNumberFormat::INTERNATIONAL`)

View File

@@ -1,8 +1,9 @@
{
"name": "propaganistas/laravel-phone",
"description": "Adds a phone validator to Laravel based on Google's libphonenumber API.",
"description": "Adds a phone validator to Laravel and Lumen based on Google's libphonenumber API.",
"keywords": [
"laravel",
"lumen",
"libphonenumber",
"validation",
"phone"
@@ -16,13 +17,14 @@
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "~4.0|~5.0",
"illuminate/validation": "~4.0|~5.0",
"giggsey/libphonenumber-for-php": "~7.0",
"julien-c/iso3166": "~2.0"
"illuminate/support": "^4.0|^5.0",
"illuminate/validation": "^4.0|^5.0",
"giggsey/libphonenumber-for-php": "^7.0",
"julien-c/iso3166": "^2.0"
},
"require-dev": {
"orchestra/testbench": "^3.1"
"orchestra/testbench": "^2.0|^3.0",
"phpunit/phpunit": "^4.0|^5.0"
},
"autoload": {
"psr-4": {
@@ -39,6 +41,5 @@
},
"suggest": {
"monarobase/country-list": "Adds a compatible (and fully translated) country list API."
},
"prefer-stable": true
}
}

View File

@@ -1,29 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<file>./tests/Laravel4.php</file>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<exclude>
<file>./src/LaravelPhoneServiceProvider.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="./clover.xml"/>
</logging>
</phpunit>

View File

@@ -12,15 +12,12 @@
>
<testsuites>
<testsuite name="Package Test Suite">
<file>./tests/Laravel5.php</file>
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<exclude>
<file>./src/LaravelPhoneServiceProvider.php</file>
</exclude>
</whitelist>
</filter>
<logging>

View File

@@ -0,0 +1,16 @@
<?php namespace Propaganistas\LaravelPhone;
use Illuminate\Support\Facades\Facade;
class LaravelPhoneFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'libphonenumber';
}
}

View File

@@ -6,27 +6,27 @@ use libphonenumber\PhoneNumberUtil;
class LaravelPhoneServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->app['validator']->extend('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone');
}
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->app['validator']->extend('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
// Make libphonenumber available in the application container.
$this->app->singleton('libphonenumber', function($app) {
return PhoneNumberUtil::getInstance();
});
$this->app->alias('libphonenumber', 'libphonenumber\PhoneNumberUtil');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('libphonenumber', function ($app) {
return PhoneNumberUtil::getInstance();
});
$this->app->alias('libphonenumber', 'libphonenumber\PhoneNumberUtil');
}
}

View File

@@ -10,247 +10,246 @@ use Propaganistas\LaravelPhone\Exceptions\InvalidParameterException;
class PhoneValidator
{
/**
* @var \libphonenumber\PhoneNumberUtil
*/
protected $lib;
/**
* @var \libphonenumber\PhoneNumberUtil
*/
protected $lib;
/**
* Dotted validator data.
*
* @var array
*/
protected $data;
/**
* Dotted validator data.
*
* @var array
*/
protected $data;
/**
* Whether the country should be auto-detected.
*
* @var bool
*/
protected $autodetect = false;
/**
* Whether the country should be auto-detected.
*
* @var bool
*/
protected $autodetect = false;
/**
* Whether to allow lenient checking of numbers (i.e. landline without area codes).
*
* @var bool
*/
protected $lenient = false;
/**
* Whether to allow lenient checking of numbers (i.e. landline without area codes).
*
* @var bool
*/
protected $lenient = false;
/**
* The field to use for country if not passed as a parameter.
*
* @var string|null
*/
protected $countryField = null;
/**
* The field to use for country if not passed as a parameter.
*
* @var string|null
*/
protected $countryField = null;
/**
* Countries to validate against.
*
* @var array
*/
protected $countries = [];
/**
* Countries to validate against.
*
* @var array
*/
protected $countries = [];
/**
* Transformed phone number types to validate against.
*
* @var array
*/
protected $types = [];
/**
* Transformed phone number types to validate against.
*
* @var array
*/
protected $types = [];
/**
* PhoneValidator constructor.
*/
public function __construct(PhoneNumberUtil $lib)
{
$this->lib = $lib;
}
/**
* PhoneValidator constructor.
*/
public function __construct(PhoneNumberUtil $lib)
{
$this->lib = $lib;
}
/**
* Validates a phone number.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @param object $validator
* @return bool
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
*/
public function validatePhone($attribute, $value, array $parameters, $validator)
{
$this->data = array_dot($validator->getData());
/**
* Validates a phone number.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @param object $validator
* @return bool
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
*/
public function validatePhone($attribute, $value, array $parameters, $validator)
{
$this->data = array_dot($validator->getData());
$this->assignParameters($parameters);
$this->assignParameters($parameters);
$this->checkCountries($attribute);
$this->checkCountries($attribute);
// If autodetecting, let's first try without a country.
// Otherwise use provided countries as default.
if ($this->autodetect || ($this->lenient && empty($this->countries))) {
array_unshift($this->countries, null);
}
// If autodetecting, let's first try without a country.
// Otherwise use provided countries as default.
if ($this->autodetect || ($this->lenient && empty($this->countries))) {
array_unshift($this->countries, null);
}
foreach ($this->countries as $country) {
if ($this->isValidNumber($value, $country)) {
return true;
}
}
foreach ($this->countries as $country) {
if ($this->isValidNumber($value, $country)) {
return true;
}
}
// All specified country validations have failed.
return false;
}
// All specified country validations have failed.
return false;
}
/**
* Parses the supplied validator parameters.
*
* @param array $parameters
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
*/
protected function assignParameters(array $parameters)
{
$types = array();
/**
* Parses the supplied validator parameters.
*
* @param array $parameters
* @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
*/
protected function assignParameters(array $parameters)
{
$types = [];
foreach ($parameters as $parameter) {
// First check if the parameter is some phone type configuration.
if ($this->isPhoneType($parameter)) {
$types[] = strtoupper($parameter);
} elseif ($this->isPhoneCountry($parameter)) {
$this->countries[] = strtoupper($parameter);
} elseif ($parameter == 'AUTO') {
$this->autodetect = true;
} elseif ($parameter == 'LENIENT') {
$this->lenient = true;
}
// Lastly check if it is an input field containing the country.
elseif ($this->isInputField($parameter)) {
$this->countryField = $parameter;
} else {
// Force developers to write proper code.
throw new InvalidParameterException($parameter);
}
}
foreach ($parameters as $parameter) {
// First check if the parameter is some phone type configuration.
if ($this->isPhoneType($parameter)) {
$types[] = strtoupper($parameter);
} elseif ($this->isPhoneCountry($parameter)) {
$this->countries[] = strtoupper($parameter);
} elseif ($parameter == 'AUTO') {
$this->autodetect = true;
} elseif ($parameter == 'LENIENT') {
$this->lenient = true;
} // Lastly check if it is an input field containing the country.
elseif ($this->isInputField($parameter)) {
$this->countryField = $parameter;
} else {
// Force developers to write proper code.
throw new InvalidParameterException($parameter);
}
}
$this->types = $this->parseTypes($types);
$this->types = $this->parseTypes($types);
}
}
/**
* Checks the detected countries. Overrides countries if a country field is present.
* When using a country field, we should validate to false if country is empty so no exception
* will be thrown.
*
* @param string $attribute
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
*/
protected function checkCountries($attribute)
{
$countryField = (is_null($this->countryField) ? $attribute . '_country' : $this->countryField);
/**
* Checks the detected countries. Overrides countries if a country field is present.
* When using a country field, we should validate to false if country is empty so no exception
* will be thrown.
*
* @param string $attribute
* @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
*/
protected function checkCountries($attribute)
{
$countryField = (is_null($this->countryField) ? $attribute . '_country' : $this->countryField);
if ($this->isInputField($countryField)) {
$this->countries = array(array_get($this->data, $countryField));
} elseif (!$this->autodetect && !$this->lenient && empty($this->countries)) {
throw new NoValidCountryFoundException;
}
}
if ($this->isInputField($countryField)) {
$this->countries = [array_get($this->data, $countryField)];
} elseif (! $this->autodetect && ! $this->lenient && empty($this->countries)) {
throw new NoValidCountryFoundException;
}
}
/**
* Performs the actual validation of the phone number.
*
* @param mixed $number
* @param null $country
* @return bool
*/
protected function isValidNumber($number, $country = null)
{
try {
// Throws NumberParseException if not parsed correctly against the supplied country.
// If no country was given, tries to discover the country code from the number itself.
$phoneNumber = $this->lib->parse($number, $country);
/**
* Performs the actual validation of the phone number.
*
* @param mixed $number
* @param null $country
* @return bool
*/
protected function isValidNumber($number, $country = null)
{
try {
// Throws NumberParseException if not parsed correctly against the supplied country.
// If no country was given, tries to discover the country code from the number itself.
$phoneNumber = $this->lib->parse($number, $country);
// Check if type is allowed.
if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) {
// Check if type is allowed.
if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) {
// Lenient validation.
if ($this->lenient) {
return $this->lib->isPossibleNumber($phoneNumber, $country);
}
// Lenient validation.
if ($this->lenient) {
return $this->lib->isPossibleNumber($phoneNumber, $country);
}
// For automatic detection, the number should have a country code.
if ($phoneNumber->hasCountryCode()) {
// For automatic detection, the number should have a country code.
if ($phoneNumber->hasCountryCode()) {
// Automatic detection:
if ($this->autodetect) {
// Validate if the international phone number is valid for its contained country.
return $this->lib->isValidNumber($phoneNumber);
}
// Automatic detection:
if ($this->autodetect) {
// Validate if the international phone number is valid for its contained country.
return $this->lib->isValidNumber($phoneNumber);
}
// Validate number against the specified country.
return $this->lib->isValidNumberForRegion($phoneNumber, $country);
}
}
// Validate number against the specified country.
return $this->lib->isValidNumberForRegion($phoneNumber, $country);
}
}
} catch (NumberParseException $e) {
// Proceed to default validation error.
}
} catch (NumberParseException $e) {
// Proceed to default validation error.
}
return false;
}
return false;
}
/**
* Parses the supplied phone number types.
*
* @param array $types
* @return array
*/
protected function parseTypes(array $types)
{
// Transform types to their namespaced class constant.
array_walk($types, function(&$type) {
$type = constant('\libphonenumber\PhoneNumberType::' . $type);
});
/**
* Parses the supplied phone number types.
*
* @param array $types
* @return array
*/
protected function parseTypes(array $types)
{
// Transform types to their namespaced class constant.
array_walk($types, function (&$type) {
$type = constant('\libphonenumber\PhoneNumberType::' . $type);
});
// Add in the unsure number type if applicable.
if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) {
$types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;
}
// Add in the unsure number type if applicable.
if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) {
$types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;
}
return $types;
}
return $types;
}
/**
* Checks if the given field is an actual input field.
*
* @param string $field
* @return bool
*/
public function isInputField($field)
{
return !is_null(array_get($this->data, $field));
}
/**
* Checks if the given field is an actual input field.
*
* @param string $field
* @return bool
*/
public function isInputField($field)
{
return ! is_null(array_get($this->data, $field));
}
/**
* Checks if the supplied string is a valid country code.
*
* @param string $country
* @return bool
*/
public function isPhoneCountry($country)
{
return ISO3166::isValid(strtoupper($country));
}
/**
* Checks if the supplied string is a valid country code.
*
* @param string $country
* @return bool
*/
public function isPhoneCountry($country)
{
return ISO3166::isValid(strtoupper($country));
}
/**
* Checks if the supplied string is a valid phone number type.
*
* @param string $type
* @return bool
*/
public function isPhoneType($type)
{
// Legacy support.
$type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type);
/**
* Checks if the supplied string is a valid phone number type.
*
* @param string $type
* @return bool
*/
public function isPhoneType($type)
{
// Legacy support.
$type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type);
return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type));
}
return defined('\libphonenumber\PhoneNumberType::' . strtoupper($type));
}
}

View File

@@ -3,25 +3,44 @@
use Illuminate\Support\Facades\App;
use libphonenumber\PhoneNumberFormat;
if (!function_exists('phone_format')) {
/**
* Formats a phone number and country for display.
*
* @param string $phone
* @param string $country
* @param int|null $format
* @return string
*/
function phone_format($phone, $country = null, $format = PhoneNumberFormat::INTERNATIONAL)
{
$lib = App::make('libphonenumber');
if (! function_exists('phone_format')) {
/**
* Get the PhoneNumberUtil or format a phone number for display.
*
* @return \libphonenumber\PhoneNumberUtil|string
*/
function phone()
{
$lib = App::make('libphonenumber');
if (!$country) {
$country = App::getLocale();
}
if (! $arguments = func_get_args()) {
return $lib;
}
$phoneNumber = $lib->parse($phone, $country);
$phone = $arguments[0];
$country = isset($arguments[1]) ? $arguments[1] : App::getLocale();
$format = isset($arguments[2]) ? $arguments[2] : PhoneNumberFormat::INTERNATIONAL;
return $lib->format($phoneNumber, $format);
}
return $lib->format(
$lib->parse($phone, $country),
$format
);
}
}
if (! function_exists('phone_format')) {
/**
* Formats a phone number and country for display.
*
* @param string $phone
* @param string $country
* @param int|null $format
* @return string
*
* @deprecated 2.8.0
*/
function phone_format($phone, $country = null, $format = PhoneNumberFormat::INTERNATIONAL)
{
return phone($phone, $country, $format);
}
}

View File

@@ -1,10 +0,0 @@
<?php namespace Propaganistas\LaravelPhone\Tests;
class Laravel4 extends PhoneValidatorTest
{
protected function getPackageProviders()
{
return ['Propaganistas\LaravelPhone\LaravelPhoneServiceProvider'];
}
}

View File

@@ -1,10 +0,0 @@
<?php namespace Propaganistas\LaravelPhone\Tests;
class Laravel5 extends PhoneValidatorTest
{
protected function getPackageProviders($app)
{
return ['Propaganistas\LaravelPhone\LaravelPhoneServiceProvider'];
}
}

View File

@@ -1,366 +1,382 @@
<?php namespace Propaganistas\LaravelPhone\Tests;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use Orchestra\Testbench\TestCase;
use Phone;
class PhoneValidatorTest extends TestCase
{
protected $validator;
protected $validator;
public function setUp()
{
parent::setUp();
protected function getPackageProviders()
{
return [
'Propaganistas\LaravelPhone\LaravelPhoneServiceProvider',
];
}
$this->validator = $this->app['validator'];
}
protected function getPackageAliases()
{
return [
'Phone' => 'Propaganistas\LaravelPhone\LaravelPhoneFacade',
];
}
private function performValidation($data)
{
$rule = 'phone' . (isset($data['rule']) ? ':' . $data['rule'] : '');
public function setUp()
{
parent::setUp();
$input = array_except($data, ['rule']);
$input['nested']['field'] = $input['field'];
if (isset($input['field_country'])) {
$input['nested']['field_country'] = $input['field_country'];
}
$this->validator = $this->app['validator'];
}
$validator = $this->validator->make($input,
[
'field' => $rule,
'nested.field' => $rule
]
);
private function performValidation($data)
{
$rule = 'phone' . (isset($data['rule']) ? ':' . $data['rule'] : '');
return $validator->passes();
}
$input = array_except($data, ['rule']);
$input['nested']['field'] = $input['field'];
if (isset($input['field_country'])) {
$input['nested']['field_country'] = $input['field_country'];
}
public function testValidatePhoneWithDefaultCountryNoType()
{
// Validator with correct country field.
$this->assertTrue($this->performValidation([
'field' => '016123456',
'rule' => 'BE'
]));
$validator = $this->validator->make($input,
[
'field' => $rule,
'nested.field' => $rule,
]
);
// Validator with wrong country value.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'NL'
]));
return $validator->passes();
}
// Validator with multiple country values, one correct.
$this->assertTrue($this->performValidation([
'field' => '016123456',
'rule' => 'BE,NL'
]));
public function testFacade()
{
$phoneNumber = Phone::parse('650-429-2057', 'US');
$this->assertTrue($phoneNumber instanceof PhoneNumber);
}
// Validator with multiple country values, value correct for second country in list.
$this->assertTrue($this->performValidation([
'field' => '016123456',
'rule' => 'NL,BE'
]));
public function testValidatePhoneWithDefaultCountryNoType()
{
// Validator with correct country field.
$this->assertTrue($this->performValidation([
'field' => '016123456',
'rule' => 'BE',
]));
// Validator with multiple wrong country values.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'DE,NL'
]));
}
// Validator with wrong country value.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'NL',
]));
public function testValidatePhoneWithCountryFieldNoType()
{
// Validator with correct country field supplied.
$this->assertTrue($this->performValidation([
'field' => '016123456',
'field_country' => 'BE'
]));
// Validator with multiple country values, one correct.
$this->assertTrue($this->performValidation([
'field' => '016123456',
'rule' => 'BE,NL',
]));
// Validator with wrong country field supplied.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'field_country' => 'NL'
]));
}
// Validator with multiple country values, value correct for second country in list.
$this->assertTrue($this->performValidation([
'field' => '016123456',
'rule' => 'NL,BE',
]));
public function testValidatePhoneWithDefaultCountryWithType()
{
// Validator with correct country value, correct type.
$this->assertTrue($this->performValidation([
'field' => '0499123456',
'rule' => 'BE,mobile'
]));
// Validator with multiple wrong country values.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'DE,NL',
]));
}
// Validator with correct country value, wrong type.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'BE,mobile'
]));
public function testValidatePhoneWithCountryFieldNoType()
{
// Validator with correct country field supplied.
$this->assertTrue($this->performValidation([
'field' => '016123456',
'field_country' => 'BE',
]));
// Validator with wrong country value, correct type.
$this->assertFalse($this->performValidation([
'field' => '0499123456',
'rule' => 'NL,mobile'
]));
// Validator with wrong country field supplied.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'field_country' => 'NL',
]));
}
// Validator with wrong country value, wrong type.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'NL,mobile'
]));
public function testValidatePhoneWithDefaultCountryWithType()
{
// Validator with correct country value, correct type.
$this->assertTrue($this->performValidation([
'field' => '0499123456',
'rule' => 'BE,mobile',
]));
// Validator with multiple country values, one correct, correct type.
$this->assertTrue($this->performValidation([
'field' => '0499123456',
'rule' => 'BE,NL,mobile'
]));
// Validator with correct country value, wrong type.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'BE,mobile',
]));
// Validator with multiple country values, one correct, wrong type.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'BE,NL,mobile'
]));
// Validator with wrong country value, correct type.
$this->assertFalse($this->performValidation([
'field' => '0499123456',
'rule' => 'NL,mobile',
]));
// Validator with multiple country values, none correct, correct type.
$this->assertFalse($this->performValidation([
'field' => '0499123456',
'rule' => 'DE,NL,mobile'
]));
// Validator with wrong country value, wrong type.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => '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, one correct, correct type.
$this->assertTrue($this->performValidation([
'field' => '0499123456',
'rule' => 'BE,NL,mobile',
]));
public function testValidatePhoneWithCountryFieldWithType()
{
// Validator with correct country field supplied, correct type.
$this->assertTrue($this->performValidation([
'field' => '0499123456',
'rule' => 'mobile',
'field_country' => 'BE'
]));
// Validator with multiple country values, one correct, wrong type.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'BE,NL,mobile',
]));
// Validator with correct country field supplied, wrong type.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'mobile',
'field_country' => 'BE'
]));
// Validator with multiple country values, none correct, correct type.
$this->assertFalse($this->performValidation([
'field' => '0499123456',
'rule' => 'DE,NL,mobile',
]));
// Validator with wrong country field supplied, correct type.
$this->assertFalse($this->performValidation([
'field' => '0499123456',
'rule' => 'mobile',
'field_country' => 'NL'
]));
// Validator with multiple country values, none correct, wrong type.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'DE,NL,mobile',
]));
}
// Validator with wrong country field supplied, wrong type.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'mobile',
'field_country' => 'NL'
]));
}
public function testValidatePhoneWithCountryFieldWithType()
{
// Validator with correct country field supplied, correct type.
$this->assertTrue($this->performValidation([
'field' => '0499123456',
'rule' => 'mobile',
'field_country' => 'BE',
]));
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 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,country_code',
'country_code' => '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,country_code',
'country_code' => '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,country_code',
'country_code' => 'NL'
]));
}
public function testValidatePhoneWithCustomCountryField()
{
// Validator with correct country field supplied, correct type.
$this->assertTrue($this->performValidation([
'field' => '0499123456',
'rule' => 'mobile,country_code',
'country_code' => 'BE',
]));
public function testValidatePhoneAutomaticDetectionFromInternationalInput()
{
// Validator with correct international input.
$this->assertTrue($this->performValidation([
'field' => '+3216123456',
'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' => '003216123456',
'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.
$this->assertFalse($this->performValidation([
'field' => '+321456',
'rule' => 'AUTO'
]));
// 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 but correct default country.
$this->assertTrue($this->performValidation([
'field' => '016123456',
'rule' => 'AUTO,NL,BE'
]));
public function testValidatePhoneAutomaticDetectionFromInternationalInput()
{
// Validator with correct international input.
$this->assertTrue($this->performValidation([
'field' => '+3216123456',
'rule' => 'AUTO',
]));
// Validator with wrong international input and wrong default country.
$this->assertFalse($this->performValidation([
'field' => '016123456',
'rule' => 'AUTO,NL,DE'
]));
}
// Validator with wrong international input.
$this->assertFalse($this->performValidation([
'field' => '003216123456',
'rule' => 'AUTO',
]));
public function testValidatePhoneNoDefaultCountryNoCountryField()
{
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException');
// Validator with wrong international input.
$this->assertFalse($this->performValidation([
'field' => '+321456',
'rule' => 'AUTO',
]));
// Validator with no country field or given country.
$this->performValidation([
'field' => '016123456'
]);
// 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, wrong type.
$this->performValidation([
'field' => '016123456',
'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.
$this->performValidation([
'field' => '0499123456',
'rule' => 'mobile'
]);
public function testValidatePhoneNoDefaultCountryNoCountryField()
{
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException');
// Validator with no country field or given country, correct type, faulty parameter.
$this->performValidation([
'field' => '0499123456',
'rule' => 'mobile,xyz'
]);
}
// Validator with no country field or given country.
$this->performValidation([
'field' => '016123456',
]);
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, wrong type.
$this->performValidation([
'field' => '016123456',
'rule' => 'mobile',
]);
// Validator with AU area code, lenient on
$this->assertTrue($this->performValidation([
'field' => '88885555',
'rule' => 'LENIENT,AU'
]));
// Validator with no country field or given country, correct type.
$this->performValidation([
'field' => '0499123456',
'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, faulty parameter.
$this->performValidation([
'field' => '0499123456',
'rule' => 'mobile,xyz',
]);
}
// Validator with wrong country field supplied, lenient on
$this->assertTrue($this->performValidation([
'field' => '88885555',
'rule' => 'LENIENT',
'field_country' => 'BE'
]));
public function testValidatePhoneLenient()
{
// Validator with AU area code, lenient off
$this->assertFalse($this->performValidation([
'field' => '88885555',
'rule' => 'AU',
]));
// Validator with no area code, lenient on
$this->assertTrue($this->performValidation([
'field' => '+16502530000',
'rule' => 'LENIENT'
]));
// Validator with AU area code, lenient on
$this->assertTrue($this->performValidation([
'field' => '88885555',
'rule' => 'LENIENT,AU',
]));
// Validator with US area code, lenient on
$this->assertTrue($this->performValidation([
'field' => '+16502530000',
'rule' => 'LENIENT,US'
]));
// Validator with correct country field supplied, lenient on
$this->assertTrue($this->performValidation([
'field' => '88885555',
'rule' => 'LENIENT',
'field_country' => 'AU',
]));
// Validator with no area code, lenient off
$this->assertFalse($this->performValidation([
'field' => '6502530000',
'rule' => 'LENIENT'
]));
// 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 on
$this->assertTrue($this->performValidation([
'field' => '6502530000',
'rule' => 'LENIENT,US'
]));
// Validator with no area code, lenient on
$this->assertTrue($this->performValidation([
'field' => '+16502530000',
'rule' => 'LENIENT',
]));
// Validator with US area code, lenient off
$this->assertFalse($this->performValidation([
'field' => '2530000',
'rule' => 'LENIENT'
]));
// Validator with US area code, lenient on
$this->assertTrue($this->performValidation([
'field' => '+16502530000',
'rule' => 'LENIENT,US',
]));
// Validator with US area code, lenient on
$this->assertTrue($this->performValidation([
'field' => '2530000',
'rule' => 'LENIENT,US'
]));
}
// Validator with no area code, lenient off
$this->assertFalse($this->performValidation([
'field' => '6502530000',
'rule' => 'LENIENT',
]));
public function testValidatePhoneFaultyParameters()
{
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\InvalidParameterException');
// Validator with US area code, lenient on
$this->assertTrue($this->performValidation([
'field' => '6502530000',
'rule' => 'LENIENT,US',
]));
// Validator with given country, correct type, faulty parameter.
$this->performValidation([
'field' => '016123456',
'rule' => 'BE,mobile,xyz'
]);
// Validator with US area code, lenient off
$this->assertFalse($this->performValidation([
'field' => '2530000',
'rule' => 'LENIENT',
]));
// Validator with country field, correct type, faulty parameter.
$this->performValidation([
'field' => '016123456',
'rule' => 'mobile,xyz',
'field_country' => 'BE'
]);
}
// Validator with US area code, lenient on
$this->assertTrue($this->performValidation([
'field' => '2530000',
'rule' => 'LENIENT,US',
]));
}
public function testPhoneFormatHelperFunction()
{
// Test landline number without format parameter.
$actual = phone_format('016123456', 'BE');
$expected = '+32 16 12 34 56';
$this->assertEquals($expected, $actual);
public function testValidatePhoneFaultyParameters()
{
$this->setExpectedException('Propaganistas\LaravelPhone\Exceptions\InvalidParameterException');
// Test landline number with format parameter.
$actual = phone_format('016123456', 'BE', PhoneNumberFormat::NATIONAL);
$expected = '016 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 without format parameter.
$actual = phone_format('0499123456', 'BE');
$expected = '+32 499 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',
]);
}
// Test mobile number with format parameter.
$actual = phone_format('0499123456', 'BE', PhoneNumberFormat::NATIONAL);
$expected = '0499 12 34 56';
$this->assertEquals($expected, $actual);
}
public function testHelperFunction()
{
// Test landline number without format parameter.
$actual = phone('016123456', 'BE');
$expected = '+32 16 12 34 56';
$this->assertEquals($expected, $actual);
// Test landline number with format parameter.
$actual = phone('016123456', 'BE', PhoneNumberFormat::NATIONAL);
$expected = '016 12 34 56';
$this->assertEquals($expected, $actual);
// Test fetching of util.
$this->assertTrue(phone() instanceof PhoneNumberUtil);
}
}