class ValidatorTest in Telephone Validation 8.2
@coversDefaultClass Drupal\telephone_validation\Validator @group Telephone
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\telephone_validation\Unit\ValidatorTest
Expanded class hierarchy of ValidatorTest
File
- tests/
src/ Unit/ ValidatorTest.php, line 14
Namespace
Drupal\Tests\telephone_validation\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
ValidatorTest:: |
protected | property | Country manager service mock. | |
ValidatorTest:: |
protected | property | Validator. | |
ValidatorTest:: |
public | function | Provides test data to testCountryList(). | |
ValidatorTest:: |
public | function | Provides test data to dataPhoneNumbers(). | |
ValidatorTest:: |
public | function |
Overrides UnitTestCase:: |
|
ValidatorTest:: |
public | function | Tests get country list. | |
ValidatorTest:: |
public | function | Tests phone number validation. |