feat(phone-parser): normalize RU phones and extract first candidate
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Cf7stg\Tests;
|
||||
|
||||
use Cf7stg\PhoneParser;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class PhoneParserTest extends TestCase
|
||||
{
|
||||
public function test_ru_plus7_formatted(): void
|
||||
{
|
||||
$r = PhoneParser::parse('+7 (903) 123-45-67');
|
||||
self::assertSame('+79031234567', $r['e164']);
|
||||
self::assertSame('+7 (903) 123-45-67', $r['pretty']);
|
||||
self::assertTrue($r['is_russian']);
|
||||
}
|
||||
|
||||
public function test_ru_leading_eight(): void
|
||||
{
|
||||
$r = PhoneParser::parse('89031234567');
|
||||
self::assertSame('+79031234567', $r['e164']);
|
||||
self::assertSame('+7 (903) 123-45-67', $r['pretty']);
|
||||
self::assertTrue($r['is_russian']);
|
||||
}
|
||||
|
||||
public function test_ru_bare_ten_digits(): void
|
||||
{
|
||||
$r = PhoneParser::parse('9031234567');
|
||||
self::assertSame('+79031234567', $r['e164']);
|
||||
self::assertTrue($r['is_russian']);
|
||||
}
|
||||
|
||||
public function test_foreign_us_number(): void
|
||||
{
|
||||
$r = PhoneParser::parse('+1 555 123 4567');
|
||||
self::assertSame('+15551234567', $r['e164']);
|
||||
self::assertFalse($r['is_russian']);
|
||||
}
|
||||
|
||||
public function test_embedded_in_text(): void
|
||||
{
|
||||
$r = PhoneParser::parse('Позвоните мне: +7-903-123-45-67, после 18:00');
|
||||
self::assertSame('+79031234567', $r['e164']);
|
||||
self::assertTrue($r['is_russian']);
|
||||
}
|
||||
|
||||
public function test_returns_first_of_multiple(): void
|
||||
{
|
||||
$r = PhoneParser::parse('Домашний 84951234567, мобильный 89031234567');
|
||||
self::assertSame('+74951234567', $r['e164']);
|
||||
}
|
||||
|
||||
public function test_garbage_returns_null(): void
|
||||
{
|
||||
self::assertNull(PhoneParser::parse('позвоните!!!'));
|
||||
}
|
||||
|
||||
public function test_empty_returns_null(): void
|
||||
{
|
||||
self::assertNull(PhoneParser::parse(''));
|
||||
}
|
||||
|
||||
public function test_too_short_returns_null(): void
|
||||
{
|
||||
self::assertNull(PhoneParser::parse('12345'));
|
||||
}
|
||||
|
||||
public function test_ru_moscow_landline(): void
|
||||
{
|
||||
$r = PhoneParser::parse('+7 (495) 123-45-67');
|
||||
self::assertSame('+74951234567', $r['e164']);
|
||||
self::assertTrue($r['is_russian']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user