You are here

public function MobileNumberClassTest::testNumberValidity in Mobile Number 8

Same name and namespace in other branches
  1. 2.0.x src/Tests/MobileNumberClassTest.php \Drupal\mobile_number\Tests\MobileNumberClassTest::testNumberValidity()

Test number validation.

File

src/Tests/MobileNumberClassTest.php, line 43

Class

MobileNumberClassTest
Mobile number class functionality.

Namespace

Drupal\mobile_number\Tests

Code

public function testNumberValidity() {
  $local_numbers = [
    '+972502345678' => 'International IL',
    '091234567' => 'None mobile IL',
    '0502345678' => 'Valid IL',
    '111' => 'Invalid IL',
    NULL => 'Empty',
  ];
  $countries = [
    'IL' => 'IL',
    'US' => 'US',
    NULL => 'Empty',
  ];
  foreach ($countries as $country => $country_text) {
    foreach ($local_numbers as $number => $number_text) {
      $valid = TRUE;
      try {
        $this->util
          ->testMobileNumber($number, $country);
      } catch (\Exception $e) {
        $valid = FALSE;
      }
      $supposed_valid = FALSE;
      switch ($country) {
        case 'IL':
          $supposed_valid = $number == '+972502345678' || $number == '0502345678';
          break;
        case NULL:
          $supposed_valid = $number == '+972502345678';
          break;
      }
      $success_text = $supposed_valid ? 'valid' : 'invalid';
      $this
        ->assertTrue($valid == $supposed_valid, "{$country_text} country, {$number_text} number, is {$success_text}.");
    }
  }
}