You are here

class ValidatorTest in Telephone Validation 8.2

@coversDefaultClass Drupal\telephone_validation\Validator @group Telephone

Hierarchy

Expanded class hierarchy of ValidatorTest

File

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

Namespace

Drupal\Tests\telephone_validation\Unit
View source
class ValidatorTest extends UnitTestCase {

  /**
   * Country manager service mock.
   *
   * @var \Drupal\Core\Locale\CountryManagerInterface
   */
  protected $countryManager;

  /**
   * Validator.
   *
   * @var \Drupal\telephone_validation\Validator
   */
  protected $validator;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $mock = $this
      ->createMock(CountryManagerInterface::class);
    $mock
      ->expects($this
      ->any())
      ->method('getList')
      ->withAnyParameters()
      ->willReturn([
      'NO' => 'Norway',
      'CA' => 'Canada',
      'US' => 'United States',
    ]);
    $this->countryManager = $mock;

    // Instantiate validator.
    $this->validator = new Validator($this->countryManager);
  }

  /**
   * Tests get country list.
   *
   * ::covers getCountryList.
   *
   * @dataProvider dataCountryList
   */
  public function testCountryList($countryCode, $label) {
    $validator = new Validator($this->countryManager);
    $list = $validator
      ->getCountryList();
    self::assertEquals($label, $list[$countryCode]);
  }

  /**
   * Tests phone number validation.
   *
   * ::covers isValid.
   *
   * @dataProvider dataPhoneNumbers
   */
  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',
    ]));
  }

  /**
   * Provides test data to testCountryList().
   *
   * @return array
   */
  public function dataCountryList() {
    return [
      [
        'NO',
        'Norway - +47',
      ],
      [
        'CA',
        'Canada - +1',
      ],
      [
        'US',
        'United States - +1',
      ],
    ];
  }

  /**
   * Provides test data to dataPhoneNumbers().
   *
   * @return array
   */
  public function dataPhoneNumbers() {
    return [
      [
        'CA',
        '+1',
        '2507638884',
      ],
      [
        'NO',
        '+47',
        '98765432',
      ],
      [
        'DK',
        '+45',
        '55555555',
      ],
      [
        'PL',
        '+48',
        '748111111',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
ValidatorTest::$countryManager protected property Country manager service mock.
ValidatorTest::$validator protected property Validator.
ValidatorTest::dataCountryList public function Provides test data to testCountryList().
ValidatorTest::dataPhoneNumbers public function Provides test data to dataPhoneNumbers().
ValidatorTest::setUp public function Overrides UnitTestCase::setUp
ValidatorTest::testCountryList public function Tests get country list.
ValidatorTest::testIsValid public function Tests phone number validation.