File: UKNumbersTest.php

package info (click to toggle)
php-giggsey-libphonenumber 9.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,464 kB
  • sloc: php: 484,879; sh: 107; makefile: 37
file content (164 lines) | stat: -rw-r--r-- 6,736 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

declare(strict_types=1);

namespace libphonenumber\Tests\Issues;

use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
use PHPUnit\Framework\TestCase;

class UKNumbersTest extends TestCase
{
    protected PhoneNumberUtil $phoneUtil;

    public function setUp(): void
    {
        PhoneNumberUtil::resetInstance();
        $this->phoneUtil = PhoneNumberUtil::getInstance();
    }

    public function testMobileNumber(): void
    {
        $number = '07987458147';
        $phoneObject = $this->phoneUtil->parse($number, 'GB');

        $valid = $this->phoneUtil->isValidNumber($phoneObject);
        self::assertTrue($valid, 'Checking phone number is valid');

        $type = $this->phoneUtil->getNumberType($phoneObject);
        self::assertSame(PhoneNumberType::MOBILE, $type, 'Checking phone number is detected as mobile');

        $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164);
        self::assertSame('+447987458147', $formattedE164, 'Checking E164 format is correct');

        $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL);
        self::assertSame('07987 458147', $formattedNational, 'Checking National format is correct');
    }

    public function testFixedLine(): void
    {
        $number = '01234512345';
        $phoneObject = $this->phoneUtil->parse($number, 'GB');

        $valid = $this->phoneUtil->isValidNumber($phoneObject);
        self::assertTrue($valid, 'Checking phone number is valid');

        $type = $this->phoneUtil->getNumberType($phoneObject);
        self::assertSame(PhoneNumberType::FIXED_LINE, $type, 'Checking phone number is detected as fixed line');

        $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164);
        self::assertSame('+441234512345', $formattedE164, 'Checking E164 format is correct');

        $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL);
        self::assertSame('01234 512345', $formattedNational, 'Checking National format is correct');
    }

    public function testPersonalNumber(): void
    {
        $number = '07010020249';
        $phoneObject = $this->phoneUtil->parse($number, 'GB');

        $valid = $this->phoneUtil->isValidNumber($phoneObject);
        self::assertTrue($valid, 'Checking phone number is valid');

        $type = $this->phoneUtil->getNumberType($phoneObject);
        self::assertSame(
            PhoneNumberType::PERSONAL_NUMBER,
            $type,
            'Checking phone number is detected as a personal number'
        );

        $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164);
        self::assertSame('+447010020249', $formattedE164, 'Checking E164 format is correct');

        $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL);
        self::assertSame('070 1002 0249', $formattedNational, 'Checking National format is correct');
    }

    public function testUAN(): void
    {
        $number = '03335555555';
        $phoneObject = $this->phoneUtil->parse($number, 'GB');

        $valid = $this->phoneUtil->isValidNumber($phoneObject);
        self::assertTrue($valid, 'Checking phone number is valid');

        $type = $this->phoneUtil->getNumberType($phoneObject);
        self::assertSame(PhoneNumberType::UAN, $type, 'Checking phone number is detected as UAN');

        $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164);
        self::assertSame('+443335555555', $formattedE164, 'Checking E164 format is correct');

        $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL);
        self::assertSame('0333 555 5555', $formattedNational, 'Checking National format is correct');
    }

    public function testTollFree(): void
    {
        $number = '0800800150';
        $phoneObject = $this->phoneUtil->parse($number, 'GB');

        $valid = $this->phoneUtil->isValidNumber($phoneObject);
        self::assertTrue($valid, 'Checking phone number is valid');

        $type = $this->phoneUtil->getNumberType($phoneObject);
        self::assertSame(PhoneNumberType::TOLL_FREE, $type, 'Checking phone number is detected as TOLL FREE');

        $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164);
        self::assertSame('+44800800150', $formattedE164, 'Checking E164 format is correct');

        $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL);
        self::assertSame('0800 800150', $formattedNational, 'Checking National format is correct');
    }

    public function testPremium(): void
    {
        $number = '09063020288';
        $phoneObject = $this->phoneUtil->parse($number, 'GB');

        $valid = $this->phoneUtil->isValidNumber($phoneObject);
        self::assertTrue($valid, 'Checking phone number is valid');

        $type = $this->phoneUtil->getNumberType($phoneObject);
        self::assertSame(PhoneNumberType::PREMIUM_RATE, $type, 'Checking phone number is detected as PREMIUM RATE');

        $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164);
        self::assertSame('+449063020288', $formattedE164, 'Checking E164 format is correct');

        $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL);
        self::assertSame('0906 302 0288', $formattedNational, 'Checking National format is correct');
    }

    public function testChildLine(): void
    {
        $number = '08001111';
        $phoneObject = $this->phoneUtil->parse($number, 'GB');

        $valid = $this->phoneUtil->isValidNumber($phoneObject);
        self::assertTrue($valid, 'Checking phone number is valid');

        $type = $this->phoneUtil->getNumberType($phoneObject);
        self::assertSame(
            PhoneNumberType::TOLL_FREE,
            $type,
            'Checking phone number is detected as TOLL FREE'
        );

        $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164);
        self::assertSame('+448001111', $formattedE164, 'Checking E164 format is correct');

        $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL);
        self::assertSame('0800 1111', $formattedNational, 'Checking National format is correct');
    }

    public function testInvalidNumber(): void
    {
        $number = '123401234512345';
        $phoneObject = $this->phoneUtil->parse($number, 'GB');

        $valid = $this->phoneUtil->isValidNumber($phoneObject);
        self::assertFalse($valid, 'Checking phone number is invalid');
    }
}