Skip to content

Commit eeb517d

Browse files
committed
add PhoneNumberAssertions
1 parent 2157b1f commit eeb517d

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
"phpunit/phpunit": "^9.0"
2121
},
2222
"require-dev": {
23+
"giggsey/libphonenumber-for-php": "^8.0",
2324
"hashids/hashids": "^4.0",
2425
"league/iso3166": "^2.0",
2526
"orchestra/testbench": "^6.0",
2627
"ramsey/uuid": "^4.0",
2728
"vinkla/hashids": "^9.0"
2829
},
2930
"suggest": {
31+
"giggsey/libphonenumber-for-php": "\\Astrotomic\\PhpunitAssertions\\PhoneNumberAssertions (^8.0)",
3032
"hashids/hashids": "\\Astrotomic\\PhpunitAssertions\\HashidAssertions (^4.0)",
3133
"league/iso3166": "\\Astrotomic\\PhpunitAssertions\\CountryAssertions (^2.0)",
3234
"ramsey/uuid": "\\Astrotomic\\PhpunitAssertions\\UuidAssertions (^4.0)",

src/PhoneNumberAssertions.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions;
4+
5+
use libphonenumber\PhoneNumber;
6+
use libphonenumber\PhoneNumberUtil;
7+
use PHPUnit\Framework\Assert as PHPUnit;
8+
use Throwable;
9+
10+
trait PhoneNumberAssertions
11+
{
12+
public static function assertE164($actual): void
13+
{
14+
PHPUnit::assertIsString($actual);
15+
PHPUnit::assertMatchesRegularExpression('/^\+[1-9]\d{1,14}$/', $actual);
16+
StringAssertions::assertLengthLessThanOrEqual(16, $actual); // plus-sign and max. 15 digits incl. CC-prefix
17+
}
18+
19+
public static function assertValid($actual): void
20+
{
21+
PHPUnit::assertTrue(
22+
PhoneNumberUtil::getInstance()->isValidNumber(
23+
self::getPhoneNumber($actual)
24+
)
25+
);
26+
}
27+
28+
public static function assertValidForRegion(string $regionCode, $actual): void
29+
{
30+
PHPUnit::assertTrue(
31+
PhoneNumberUtil::getInstance()->isValidNumberForRegion(
32+
self::getPhoneNumber($actual),
33+
$regionCode
34+
),
35+
$regionCode.' // '.$actual
36+
);
37+
}
38+
39+
protected static function getPhoneNumber($actual): PhoneNumber
40+
{
41+
if (is_string($actual)) {
42+
try {
43+
$actual = PhoneNumberUtil::getInstance()->parse($actual);
44+
} catch(Throwable $ex) {
45+
}
46+
}
47+
48+
PHPUnit::assertInstanceOf(PhoneNumber::class, $actual);
49+
50+
return $actual;
51+
}
52+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions\Tests;
4+
5+
use Astrotomic\PhpunitAssertions\PhoneNumberAssertions;
6+
7+
final class PhoneNumberAssertionsTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
* @dataProvider thousandTimes
12+
*/
13+
public static function it_can_validate_e164(): void
14+
{
15+
PhoneNumberAssertions::assertE164(preg_replace('/([^\d+])/', '', self::randomPhoneNumber()['number']));
16+
}
17+
18+
/**
19+
* @test
20+
* @dataProvider thousandTimes
21+
*/
22+
public static function it_can_validate_phone_number(): void
23+
{
24+
PhoneNumberAssertions::assertValid(self::randomPhoneNumber()['number']);
25+
}
26+
27+
/**
28+
* @test
29+
* @dataProvider thousandTimes
30+
*/
31+
public static function it_can_validate_phone_number_for_region(): void
32+
{
33+
$number = self::randomPhoneNumber();
34+
PhoneNumberAssertions::assertValidForRegion($number['code'], $number['number']);
35+
}
36+
37+
protected static function randomPhoneNumber(): array
38+
{
39+
return self::randomElement([
40+
// https://shaun.net/resources/test-phone-numbers/
41+
['number' => '+61 2 9192 0995', 'code' => 'AU'],
42+
['number' => '+64 9 887 6986', 'code' => 'NZ'],
43+
['number' => '+61 1800 801 920', 'code' => 'AU'],
44+
['number' => '+44 20 8759 9036', 'code' => 'GB'],
45+
['number' => '+1 800 444 4444', 'code' => 'US'],
46+
['number' => '+1 213 621 0002', 'code' => 'US'],
47+
['number' => '+1 914 232 9901', 'code' => 'US'],
48+
// https://howtophoneto.com/test/
49+
['number' => '+61 2 91011948', 'code' => 'AU'],
50+
['number' => '+61 02 8335 4600', 'code' => 'AU'],
51+
['number' => '+61 2 8229 4333', 'code' => 'AU'],
52+
['number' => '+61 3 8641 9083', 'code' => 'AU'],
53+
['number' => '+61 3 9683 9999', 'code' => 'AU'],
54+
['number' => '+61 (02) 9293 9262', 'code' => 'AU'],
55+
['number' => '(+61 2) 9293 9270', 'code' => 'AU'],
56+
['number' => '+61 1300 368999', 'code' => 'AU'],
57+
['number' => '+61 03 9640 0999', 'code' => 'AU'],
58+
['number' => '+64 (9) 379-0861', 'code' => 'NZ'],
59+
['number' => '+64-9-977-2232', 'code' => 'NZ'],
60+
['number' => '+64 9 977 2237', 'code' => 'NZ'],
61+
['number' => '+64 4 924 2424', 'code' => 'NZ'],
62+
['number' => '+64 4 470 3142', 'code' => 'NZ'],
63+
['number' => '+64 4 473 11 33', 'code' => 'NZ'],
64+
['number' => '+64 4 915 6666', 'code' => 'NZ'],
65+
['number' => '+64 0508 500 499', 'code' => 'NZ'],
66+
['number' => '+672 3 22147', 'code' => 'NF'],
67+
['number' => '+675 327 3396', 'code' => 'PG'],
68+
['number' => '(+675) 320 1212', 'code' => 'PG'],
69+
['number' => '(+675) 322 0888', 'code' => 'PG'],
70+
// https://www.zamic.com/page/5d916f06cb9ed42094
71+
['number' => '+1 628 246 2222', 'code' => 'US'],
72+
['number' => '+1 202 762 1401', 'code' => 'US'],
73+
// https://www.placetel.de/ratgeber/testrufnummer
74+
['number' => '+49 89 721010 99701', 'code' => 'DE'],
75+
['number' => '+49 89 721010 99702', 'code' => 'DE'],
76+
['number' => '+49 89 721010 99703', 'code' => 'DE'],
77+
// https://www.smartphonevergleich.de/telefonpaul/
78+
['number' => '+49 30-58583772', 'code' => 'DE'],
79+
['number' => '+49 176-34636276', 'code' => 'DE'],
80+
// https://servicenummern.telekom.de/weitere-informationen/zeitansage/
81+
['number' => '+49 180 4 100 100', 'code' => 'DE'],
82+
['number' => '+49 800 330 0800', 'code' => 'DE'],
83+
]);
84+
}
85+
}

tests/Utils/Randomize.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ protected static function randomFloat(float $min = PHP_INT_MIN, float $max = PHP
2828
{
2929
return max($min, min(static::randomInt(intval(floor($min) * 100), intval(ceil($max) * 100)) / 100, $max));
3030
}
31+
32+
protected static function randomElement(array $array)
33+
{
34+
return $array[array_rand($array)];
35+
}
3136
}

tests/Utils/Repeatable.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ public function hundredTimes(): iterable
1010
yield [];
1111
}
1212
}
13+
public function thousandTimes(): iterable
14+
{
15+
for ($i = 0; $i < 1000; $i++) {
16+
yield [];
17+
}
18+
}
1319
}

0 commit comments

Comments
 (0)