1 | <?php |
||
10 | class UKNumbersTest extends \PHPUnit_Framework_TestCase |
||
11 | { |
||
12 | const META_DATA_FILE_PREFIX = 'PhoneNumberMetadata'; |
||
13 | /** |
||
14 | * @var \libphonenumber\PhoneNumberUtil |
||
15 | */ |
||
16 | protected $phoneUtil; |
||
17 | |||
18 | public function setUp() |
||
19 | { |
||
20 | PhoneNumberUtil::resetInstance(); |
||
21 | $this->phoneUtil = PhoneNumberUtil::getInstance( |
||
22 | self::META_DATA_FILE_PREFIX, |
||
23 | CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap |
||
24 | ); |
||
25 | ; |
||
26 | } |
||
27 | |||
28 | public function testMobileNumber() |
||
29 | { |
||
30 | $number = '07987458147'; |
||
31 | $phoneObject = $this->phoneUtil->parse($number, 'GB'); |
||
32 | |||
33 | $valid = $this->phoneUtil->isValidNumber($phoneObject); |
||
34 | $this->assertTrue($valid, "Checking phone number is valid"); |
||
35 | |||
36 | $type = $this->phoneUtil->getNumberType($phoneObject); |
||
37 | $this->assertEquals(PhoneNumberType::MOBILE, $type, "Checking phone number is detected as mobile"); |
||
38 | |||
39 | $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164); |
||
40 | $this->assertEquals("+447987458147", $formattedE164, "Checking E164 format is correct"); |
||
41 | |||
42 | $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL); |
||
43 | $this->assertEquals("07987 458147", $formattedNational, "Checking National format is correct"); |
||
44 | } |
||
45 | |||
46 | public function testFixedLine() |
||
47 | { |
||
48 | $number = '01234512345'; |
||
49 | $phoneObject = $this->phoneUtil->parse($number, 'GB'); |
||
50 | |||
51 | $valid = $this->phoneUtil->isValidNumber($phoneObject); |
||
52 | $this->assertTrue($valid, "Checking phone number is valid"); |
||
53 | |||
54 | $type = $this->phoneUtil->getNumberType($phoneObject); |
||
55 | $this->assertEquals(PhoneNumberType::FIXED_LINE, $type, "Checking phone number is detected as fixed line"); |
||
56 | |||
57 | $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164); |
||
58 | $this->assertEquals("+441234512345", $formattedE164, "Checking E164 format is correct"); |
||
59 | |||
60 | $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL); |
||
61 | $this->assertEquals("01234 512345", $formattedNational, "Checking National format is correct"); |
||
62 | } |
||
63 | |||
64 | public function testSharedCost() |
||
65 | { |
||
66 | $number = '08451234568'; |
||
67 | $phoneObject = $this->phoneUtil->parse($number, 'GB'); |
||
68 | |||
69 | $valid = $this->phoneUtil->isValidNumber($phoneObject); |
||
70 | $this->assertTrue($valid, "Checking phone number is valid"); |
||
71 | |||
72 | $type = $this->phoneUtil->getNumberType($phoneObject); |
||
73 | $this->assertEquals(PhoneNumberType::SHARED_COST, $type, "Checking phone number is detected as shared cost"); |
||
74 | |||
75 | $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164); |
||
76 | $this->assertEquals("+448451234568", $formattedE164, "Checking E164 format is correct"); |
||
77 | |||
78 | $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL); |
||
79 | $this->assertEquals("0845 123 4568", $formattedNational, "Checking National format is correct"); |
||
80 | } |
||
81 | |||
82 | public function testPersonalNumber() |
||
83 | { |
||
84 | $number = '07010020249'; |
||
85 | $phoneObject = $this->phoneUtil->parse($number, 'GB'); |
||
86 | |||
87 | $valid = $this->phoneUtil->isValidNumber($phoneObject); |
||
88 | $this->assertTrue($valid, "Checking phone number is valid"); |
||
89 | |||
90 | $type = $this->phoneUtil->getNumberType($phoneObject); |
||
91 | $this->assertEquals( |
||
92 | PhoneNumberType::PERSONAL_NUMBER, |
||
93 | $type, |
||
94 | "Checking phone number is detected as a personal number" |
||
95 | ); |
||
96 | |||
97 | $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164); |
||
98 | $this->assertEquals("+447010020249", $formattedE164, "Checking E164 format is correct"); |
||
99 | |||
100 | $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL); |
||
101 | $this->assertEquals("070 1002 0249", $formattedNational, "Checking National format is correct"); |
||
102 | } |
||
103 | |||
104 | public function testUAN() |
||
105 | { |
||
106 | $number = '03335555555'; |
||
107 | $phoneObject = $this->phoneUtil->parse($number, 'GB'); |
||
108 | |||
109 | $valid = $this->phoneUtil->isValidNumber($phoneObject); |
||
110 | $this->assertTrue($valid, "Checking phone number is valid"); |
||
111 | |||
112 | $type = $this->phoneUtil->getNumberType($phoneObject); |
||
113 | $this->assertEquals(PhoneNumberType::UAN, $type, "Checking phone number is detected as UAN"); |
||
114 | |||
115 | $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164); |
||
116 | $this->assertEquals("+443335555555", $formattedE164, "Checking E164 format is correct"); |
||
117 | |||
118 | $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL); |
||
119 | $this->assertEquals("0333 555 5555", $formattedNational, "Checking National format is correct"); |
||
120 | } |
||
121 | |||
122 | public function testTollFree() |
||
123 | { |
||
124 | $number = '0800800150'; |
||
125 | $phoneObject = $this->phoneUtil->parse($number, 'GB'); |
||
126 | |||
127 | $valid = $this->phoneUtil->isValidNumber($phoneObject); |
||
128 | $this->assertTrue($valid, "Checking phone number is valid"); |
||
129 | |||
130 | $type = $this->phoneUtil->getNumberType($phoneObject); |
||
131 | $this->assertEquals(PhoneNumberType::TOLL_FREE, $type, "Checking phone number is detected as TOLL FREE"); |
||
132 | |||
133 | $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164); |
||
134 | $this->assertEquals("+44800800150", $formattedE164, "Checking E164 format is correct"); |
||
135 | |||
136 | $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL); |
||
137 | $this->assertEquals("0800 800150", $formattedNational, "Checking National format is correct"); |
||
138 | } |
||
139 | |||
140 | public function testPremium() |
||
141 | { |
||
142 | $number = '09063020288'; |
||
143 | $phoneObject = $this->phoneUtil->parse($number, 'GB'); |
||
144 | |||
145 | $valid = $this->phoneUtil->isValidNumber($phoneObject); |
||
146 | $this->assertTrue($valid, "Checking phone number is valid"); |
||
147 | |||
148 | $type = $this->phoneUtil->getNumberType($phoneObject); |
||
149 | $this->assertEquals(PhoneNumberType::PREMIUM_RATE, $type, "Checking phone number is detected as PREMIUM RATE"); |
||
150 | |||
151 | $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164); |
||
152 | $this->assertEquals("+449063020288", $formattedE164, "Checking E164 format is correct"); |
||
153 | |||
154 | $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL); |
||
155 | $this->assertEquals("0906 302 0288", $formattedNational, "Checking National format is correct"); |
||
156 | } |
||
157 | |||
158 | public function testChildLine() |
||
159 | { |
||
160 | $number = '08001111'; |
||
161 | $phoneObject = $this->phoneUtil->parse($number, 'GB'); |
||
162 | |||
163 | $valid = $this->phoneUtil->isValidNumber($phoneObject); |
||
164 | $this->assertTrue($valid, "Checking phone number is valid"); |
||
165 | |||
166 | $type = $this->phoneUtil->getNumberType($phoneObject); |
||
167 | $this->assertEquals( |
||
168 | PhoneNumberType::TOLL_FREE, |
||
169 | $type, |
||
170 | "Checking phone number is detected as TOLL FREE" |
||
171 | ); |
||
172 | |||
173 | $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164); |
||
174 | $this->assertEquals("+448001111", $formattedE164, "Checking E164 format is correct"); |
||
175 | |||
176 | $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL); |
||
177 | $this->assertEquals("0800 1111", $formattedNational, "Checking National format is correct"); |
||
178 | } |
||
179 | |||
180 | public function testInvalidNumber() |
||
181 | { |
||
182 | $number = '123401234512345'; |
||
183 | $phoneObject = $this->phoneUtil->parse($number, 'GB'); |
||
184 | |||
185 | $valid = $this->phoneUtil->isValidNumber($phoneObject); |
||
186 | $this->assertFalse($valid, "Checking phone number is invalid"); |
||
187 | } |
||
188 | } |
||
189 |