1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace libphonenumber\Tests\core; |
4
|
|
|
|
5
|
|
|
use libphonenumber\MatcherAPIInterface; |
6
|
|
|
use libphonenumber\PhoneNumber; |
7
|
|
|
use libphonenumber\PhoneNumberDesc; |
8
|
|
|
use libphonenumber\RegexBasedMatcher; |
9
|
|
|
|
10
|
|
|
class MatcherTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
public function testRegexBasedMatcher() |
13
|
|
|
{ |
14
|
|
|
$this->checkMatcherBehavesAsExpected(RegexBasedMatcher::create()); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
private function checkMatcherBehavesAsExpected(MatcherAPIInterface $matcher) |
18
|
|
|
{ |
19
|
|
|
$desc = $this->createDesc(""); |
20
|
|
|
|
21
|
|
|
// Test if there is no matcher data. |
22
|
|
|
$this->assertInvalid($matcher, "1", $desc); |
23
|
|
|
|
24
|
|
|
$desc = $this->createDesc("9\\d{2}"); |
25
|
|
|
$this->assertInvalid($matcher, "91", $desc); |
26
|
|
|
$this->assertInvalid($matcher, "81", $desc); |
27
|
|
|
$this->assertMatched($matcher, "911", $desc); |
28
|
|
|
$this->assertInvalid($matcher, "811", $desc); |
29
|
|
|
$this->assertTooLong($matcher, "9111", $desc); |
30
|
|
|
$this->assertInvalid($matcher, "8111", $desc); |
31
|
|
|
|
32
|
|
|
$desc = $this->createDesc("\\d{1,2}"); |
33
|
|
|
$this->assertMatched($matcher, "2", $desc); |
34
|
|
|
$this->assertMatched($matcher, "20", $desc); |
35
|
|
|
|
36
|
|
|
$desc = $this->createDesc("20?"); |
37
|
|
|
$this->assertMatched($matcher, "2", $desc); |
38
|
|
|
$this->assertMatched($matcher, "20", $desc); |
39
|
|
|
|
40
|
|
|
$desc = $this->createDesc("2|20"); |
41
|
|
|
$this->assertMatched($matcher, "2", $desc); |
42
|
|
|
// Subtle case where lookingAt() and matches() result in different ends(). |
43
|
|
|
$this->assertMatched($matcher, "20", $desc); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Helper method to set national number fields in the PhoneNumberDesc proto. Empty fields won't be |
48
|
|
|
* set. |
49
|
|
|
* |
50
|
|
|
* @param string $nationalNumberPattern |
51
|
|
|
* @return PhoneNumberDesc |
52
|
|
|
*/ |
53
|
|
|
private function createDesc($nationalNumberPattern) |
54
|
|
|
{ |
55
|
|
|
$desc = new PhoneNumberDesc(); |
56
|
|
|
if (strlen($nationalNumberPattern) > 0) { |
57
|
|
|
$desc->setNationalNumberPattern($nationalNumberPattern); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $desc; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function assertMatched(MatcherAPIInterface $matcher, $number, PhoneNumberDesc $desc) |
64
|
|
|
{ |
65
|
|
|
$this->assertTrue($matcher->matchNationalNumber($number, $desc, false), "{$number} should have matched {$this->descToString($desc)}"); |
66
|
|
|
$this->assertTrue($matcher->matchNationalNumber($number, $desc, true), "{$number} should have matched {$this->descToString($desc)}"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function assertInvalid(MatcherAPIInterface $matcher, $number, PhoneNumberDesc $desc) |
70
|
|
|
{ |
71
|
|
|
$this->assertFalse($matcher->matchNationalNumber($number, $desc, false), "{$number} should not have matched {$this->descToString($desc)}"); |
72
|
|
|
$this->assertFalse($matcher->matchNationalNumber($number, $desc, true), "{$number} should not have matched {$this->descToString($desc)}"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function assertTooLong(MatcherAPIInterface $matcher, $number, PhoneNumberDesc $desc) |
76
|
|
|
{ |
77
|
|
|
$this->assertFalse($matcher->matchNationalNumber($number, $desc, false), "{$number} should have been too long for {$this->descToString($desc)}"); |
78
|
|
|
$this->assertTrue($matcher->matchNationalNumber($number, $desc, true), "{$number} should have been too long for {$this->descToString($desc)}"); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function descToString(PhoneNumberDesc $desc) |
82
|
|
|
{ |
83
|
|
|
$string = "pattern: "; |
84
|
|
|
if ($desc->hasNationalNumberPattern()) { |
85
|
|
|
$string .= $desc->getNationalNumberPattern(); |
86
|
|
|
} else { |
87
|
|
|
$string .= "none"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $string; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|