You are here

public function ValidatorTest::testIsValid in Telephone Validation 8.2

Tests phone number validation.

::covers isValid.

@dataProvider dataPhoneNumbers

File

tests/src/Unit/ValidatorTest.php, line 67

Class

ValidatorTest
@coversDefaultClass Drupal\telephone_validation\Validator @group Telephone

Namespace

Drupal\Tests\telephone_validation\Unit

Code

public function testIsValid($countryCode, $countryPrefix, $number) {

  // Test if number passes if format is National and number matches allowed
  // country.
  self::assertTrue($this->validator
    ->isValid($number, PhoneNumberFormat::NATIONAL, [
    $countryCode,
  ]));

  // Test if number fails if country is not supported.
  self::assertFalse($this->validator
    ->isValid($number, PhoneNumberFormat::NATIONAL, [
    'XYZ',
  ]));

  // Test if number fails if country doesn't match.
  self::assertFalse($this->validator
    ->isValid($number, PhoneNumberFormat::NATIONAL, [
    'UA',
  ]));

  // Test if number fails if format is wrong.
  self::assertFalse($this->validator
    ->isValid($number, PhoneNumberFormat::INTERNATIONAL, [
    $countryCode,
  ]));
  self::assertFalse($this->validator
    ->isValid($number, PhoneNumberFormat::E164, [
    $countryCode,
  ]));

  // Test if number passes if we add country prefix.
  self::assertTrue($this->validator
    ->isValid($countryPrefix . $number, PhoneNumberFormat::E164, [
    $countryCode,
  ]));

  // Test if number passes if country is not defined.
  self::assertTrue($this->validator
    ->isValid($countryPrefix . $number, PhoneNumberFormat::E164, []));

  // Test if number fails if it's prefix doesn't belong to one of the
  // countries from white-list.
  self::assertFalse($this->validator
    ->isValid($countryPrefix . $number, PhoneNumberFormat::E164, [
    'UA',
  ]));
}