-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
PhoneNumberTest.php
495 lines (420 loc) · 17 KB
/
PhoneNumberTest.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
namespace Propaganistas\LaravelPhone\Tests;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberFormat;
use Propaganistas\LaravelPhone\PhoneNumber;
use Propaganistas\LaravelPhone\Exceptions\CountryCodeException;
use Propaganistas\LaravelPhone\Exceptions\NumberParseException;
use Propaganistas\LaravelPhone\Exceptions\NumberFormatException;
class PhoneNumberTest extends TestCase
{
/** @test */
public function it_can_construct()
{
$object = new PhoneNumber('012345678');
$this->assertInstanceOf(PhoneNumber::class, $object);
$this->assertEquals('012345678', (string) $object);
}
/** @test */
public function it_can_return_the_raw_number()
{
$object = new PhoneNumber('012 34 56 78');
$this->assertEquals('012 34 56 78', $object->getRawNumber());
}
/** @test */
public function it_can_return_the_country()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('NL', 'FR', 'BE');
$this->assertEquals('BE', $object->getCountry());
$object = new PhoneNumber('+3212345678');
$this->assertEquals('BE', $object->getCountry());
}
/** @test */
public function it_will_ignore_invalid_countries()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE', 'foo', 23);
$this->assertEquals('BE', $object->getCountry());
}
/** @test */
public function it_stores_the_country()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('NL', 'FR', 'BE');
$this->assertEquals('BE', $object->getCountry());
$object = new PhoneNumber('+3212345678');
$object->getCountry();
$this->assertEquals('BE', $object->getCountry());
}
/** @test */
public function it_can_check_the_country()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertTrue($object->isOfCountry('BE'));
$this->assertFalse($object->isOfCountry('US'));
$object = new PhoneNumber('+3212345678');
$this->assertTrue($object->isOfCountry('BE'));
$this->assertFalse($object->isOfCountry('US'));
}
/** @test */
public function it_can_check_libphonenumber_specific_regions_as_country()
{
$object = new PhoneNumber('+247501234');
$this->assertTrue($object->isOfCountry('AC'));
$this->assertFalse($object->isOfCountry('US'));
}
/** @test */
public function it_can_make()
{
$object = PhoneNumber::make('012345678');
$this->assertInstanceOf(PhoneNumber::class, $object);
$this->assertEquals('012345678', (string) $object);
$object = PhoneNumber::make('012345678', 'BE');
$this->assertEquals('+3212345678', (string) $object);
$this->assertEquals('BE', $object->getCountry());
$object = PhoneNumber::make('012345678', ['BE', 'NL']);
$this->assertEquals('+3212345678', (string) $object);
$this->assertEquals('BE', $object->getCountry());
}
/** @test */
public function it_can_format()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals('+3212345678', $object->format(PhoneNumberFormat::E164));
}
/** @test */
public function it_can_format_international_numbers_without_given_country()
{
$object = new PhoneNumber('+3212345678');
$this->assertEquals('012 34 56 78', $object->format(PhoneNumberFormat::NATIONAL));
}
/** @test */
public function it_can_format_lenient_international_numbers_without_given_country()
{
$object = new PhoneNumber('+49(0)12-44 614038');
$object->lenient();
$this->assertEquals('+491244614038', $object->formatE164());
}
/** @test */
public function it_throws_an_exception_when_formatting_possible_international_numbers_without_given_country_not_marked_as_lenient()
{
$object = new PhoneNumber('+49(0)12-44 614038');
$this->expectException(NumberParseException::class);
$this->expectExceptionMessage('Number requires a country to be specified');
$object->formatE164();
}
/** @test */
public function it_can_format_international_numbers_with_wrong_country()
{
$object = new PhoneNumber('+3212345678');
$object = $object->ofCountry('US');
$this->assertEquals('012 34 56 78', $object->format(PhoneNumberFormat::NATIONAL));
}
/** @test */
public function it_throws_an_exception_when_formatting_non_international_number_without_given_country()
{
$object = new PhoneNumber('012345678');
$this->expectException(NumberParseException::class);
$this->expectExceptionMessage('Number requires a country to be specified');
$object->format(PhoneNumberFormat::NATIONAL);
}
/** @test */
public function it_can_parse_format_names()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals(
$object->format(PhoneNumberFormat::E164),
$object->format('e164')
);
}
/** @test */
public function it_throws_an_exception_for_invalid_formats()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->expectException(NumberFormatException::class);
$this->expectExceptionMessage('foo');
$object->format('foo');
}
/** @test */
public function it_has_an_international_format_shortcut_method()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals(
$object->format(PhoneNumberFormat::INTERNATIONAL),
$object->formatInternational()
);
}
/** @test */
public function it_has_a_national_format_shortcut_method()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals(
$object->format(PhoneNumberFormat::NATIONAL),
$object->formatNational()
);
}
/** @test */
public function it_has_an_E164_format_shortcut_method()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals(
$object->format(PhoneNumberFormat::E164),
$object->formatE164()
);
}
/** @test */
public function it_has_an_RFC3966_format_shortcut_method()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals(
$object->format(PhoneNumberFormat::RFC3966),
$object->formatRFC3966()
);
}
/** @test */
public function it_can_format_for_dialing_from_within_a_given_country()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals('012 34 56 78', $object->formatForCountry('BE'));
$this->assertEquals('00 32 12 34 56 78', $object->formatForCountry('NL'));
$this->assertEquals('011 32 12 34 56 78', $object->formatForCountry('US'));
}
/** @test */
public function it_can_format_for_dialing_on_mobile_from_within_a_given_country()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals('012345678', $object->formatForMobileDialingInCountry('BE'));
$this->assertEquals('+3212345678', $object->formatForMobileDialingInCountry('NL'));
$this->assertEquals('+3212345678', $object->formatForMobileDialingInCountry('US'));
}
/** @test */
public function it_throws_an_exception_when_an_invalid_country_is_provided_for_formatting_for_dialing()
{
$object = new PhoneNumber('+3212345678');
$this->expectException(CountryCodeException::class);
$this->expectExceptionMessage('foo');
$object->formatForCountry('foo');
}
/** @test */
public function it_throws_an_exception_when_an_invalid_country_is_provided_for_formatting_for_mobile_dialing()
{
$object = new PhoneNumber('+3212345678');
$this->expectException(CountryCodeException::class);
$this->expectExceptionMessage('foo');
$object->formatForMobileDialingInCountry('foo');
}
/** @test */
public function it_can_verify_formats()
{
$this->assertTrue(PhoneNumber::isValidFormat(PhoneNumberFormat::E164));
$this->assertTrue(PhoneNumber::isValidFormat('e164'));
$this->assertFalse(PhoneNumber::isValidFormat(99999));
$this->assertFalse(PhoneNumber::isValidFormat('foo'));
}
/** @test */
public function it_throws_an_exception_when_the_country_is_missing()
{
$object = new PhoneNumber('45678');
$this->expectException(NumberParseException::class);
$this->expectExceptionMessage('Number requires a country to be specified.');
$object->formatRFC3966();
}
/** @test */
public function it_throws_an_exception_when_the_country_is_mismatched()
{
$object = new PhoneNumber('4567');
$object = $object->ofCountry('BE');
$this->expectException(NumberParseException::class);
$this->expectExceptionMessage('Number does not match the provided country');
$object->formatRFC3966();
}
/** @test */
public function it_throws_an_exception_when_an_international_number_also_fails_with_provided_country()
{
$object = new PhoneNumber('+15555555555');
$object = $object->ofCountry('US');
$this->expectException(NumberParseException::class);
$this->expectExceptionMessage('Number does not match the provided country');
$object->formatRFC3966();
}
/** @test */
public function it_can_return_the_type()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals('fixed_line', $object->getType());
$this->assertEquals(PhoneNumberType::FIXED_LINE, $object->getType(true));
$object = new PhoneNumber('0470123456');
$object = $object->ofCountry('BE');
$this->assertEquals('mobile', $object->getType());
$this->assertEquals(PhoneNumberType::MOBILE, $object->getType(true));
}
/** @test */
public function it_can_check_the_type()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertTrue($object->isOfType('fixed_line'));
$this->assertTrue($object->isOfType(PhoneNumberType::FIXED_LINE));
$this->assertFalse($object->isOfType('mobile'));
$this->assertFalse($object->isOfType(PhoneNumberType::MOBILE));
$object = new PhoneNumber('0470123456');
$object = $object->ofCountry('BE');
$this->assertFalse($object->isOfType('fixed_line'));
$this->assertFalse($object->isOfType(PhoneNumberType::FIXED_LINE));
$this->assertTrue($object->isOfType('mobile'));
$this->assertTrue($object->isOfType(PhoneNumberType::MOBILE));
}
/* @test */
public function it_adds_the_unsure_type()
{
// This number is of type FIXED_LINE_OR_MOBILE.
// Without the unsure type, the following check would fail.
$object = new PhoneNumber('8590332334');
$object = $object->ofCountry('IN');
$this->assertTrue($object->isOfType('fixed_line'));
}
/** @test */
public function it_can_verify_types()
{
$this->assertTrue(PhoneNumber::isValidType(PhoneNumberType::MOBILE));
$this->assertTrue(PhoneNumber::isValidType((string) PhoneNumberType::MOBILE));
$this->assertTrue(PhoneNumber::isValidType('mobile'));
$this->assertFalse(PhoneNumber::isValidType(99999));
$this->assertFalse(PhoneNumber::isValidType('99999'));
$this->assertFalse(PhoneNumber::isValidType('foo'));
}
/** @test */
public function it_can_handle_json_encoding()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals('"+3212345678"', $object->toJson());
$this->assertEquals('"+3212345678"', json_encode($object));
}
/** @test */
public function it_can_handle_serialization()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$serialized = serialize($object);
$this->assertTrue(is_string($serialized));
$unserialized = unserialize($serialized);
$this->assertInstanceOf(PhoneNumber::class, $unserialized);
$this->assertEquals('+3212345678', (string) $unserialized);
$this->assertEquals('BE', $unserialized->getCountry());
}
/** @test */
public function it_can_be_cast_to_string()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('BE');
$this->assertEquals($object->formatE164(), (string) $object);
}
/** @test */
public function it_returns_the_original_number_when_unparsable_number_is_cast_to_string()
{
$object = new PhoneNumber('45678');
$this->assertEquals('45678', (string) $object);
$object = $object->ofCountry('BE');
$this->assertEquals('45678', (string) $object);
}
/** @test */
public function it_returns_empty_string_when_null_is_cast_to_string()
{
$object = new PhoneNumber(null);
$this->assertEquals('', (string) $object);
}
/** @test */
public function it_has_a_helper_function()
{
// Test international landline number without country and format parameters.
$actual = phone('+32 12 34 56 78');
$expected = PhoneNumber::make('012345678', 'BE');
$this->assertEquals($expected, (string) $actual);
// Test landline number without format parameter.
$actual = phone('012345678', 'BE');
$expected = PhoneNumber::make('012345678', 'BE');
$this->assertEquals($expected, $actual);
// Test landline number with format parameter.
$actual = phone('012345678', 'BE', PhoneNumberFormat::NATIONAL);
$expected = '012 34 56 78';
$this->assertEquals($expected, $actual);
}
/** @test */
public function it_can_get_the_exceptions_number()
{
$exception = NumberParseException::countryRequired('12345');
$this->assertEquals('12345', $exception->getNumber());
$exception = NumberParseException::countryMismatch('12345', []);
$this->assertEquals('12345', $exception->getNumber());
}
/** @test */
public function it_can_get_the_exceptions_countries()
{
$exception = NumberParseException::countryMismatch('12345', ['BE', 'foo']);
$this->assertEquals(['BE', 'foo'], $exception->getCountries());
}
/** @test */
public function it_doesnt_throw_for_antarctica()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('AQ','BE');
$this->assertEquals('BE', $object->getCountry());
}
/** @test */
public function it_can_check_equality()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('AQ','BE');
$this->assertTrue($object->equals('012345678', 'BE'));
$this->assertTrue($object->equals('012345678', ['BE', 'NL']));
$this->assertTrue($object->equals('+3212345678'));
$this->assertTrue($object->equals(PhoneNumber::make('012345678', 'BE')));
$this->assertFalse($object->equals('012345679', 'BE'));
$this->assertFalse($object->equals('012345679', ['BE', 'NL']));
$this->assertFalse($object->equals('+3212345679'));
$this->assertFalse($object->equals(PhoneNumber::make('012345679', 'BE')));
}
/** @test */
public function it_can_check_inequality()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('AQ','BE');
$this->assertTrue($object->notEquals('012345679', 'BE'));
$this->assertTrue($object->notEquals('012345679', ['BE', 'NL']));
$this->assertTrue($object->notEquals('+3212345679'));
$this->assertTrue($object->notEquals(PhoneNumber::make('012345679', 'BE')));
$this->assertFalse($object->notEquals('012345678', 'BE'));
$this->assertFalse($object->notEquals('012345678', ['BE', 'NL']));
$this->assertFalse($object->notEquals('+3212345678'));
$this->assertFalse($object->notEquals(PhoneNumber::make('012345678', 'BE')));
}
/** @test */
public function it_doesnt_throw_for_invalid_numbers_when_checking_equality()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('AQ','BE');
$this->assertFalse($object->equals('1234'));
$this->assertFalse($object->equals('012345678', 'NL'));
}
/** @test */
public function it_doesnt_throw_for_invalid_numbers_when_checking_inequality()
{
$object = new PhoneNumber('012345678');
$object = $object->ofCountry('AQ','BE');
$this->assertTrue($object->notEquals('1234'));
$this->assertTrue($object->notEquals('012345678', 'NL'));
}
}