Skip to content

Commit

Permalink
Merge pull request #13 from giggsey/google_v5.9
Browse files Browse the repository at this point in the history
Google v5.9
  • Loading branch information
giggsey committed Nov 13, 2013
2 parents 718d82b + 896a350 commit 7e563c9
Show file tree
Hide file tree
Showing 562 changed files with 3,396 additions and 7,856 deletions.
2 changes: 1 addition & 1 deletion Tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
$loader->add('libphonenumber\\buildtools\\', __DIR__ . '/../build/');


/* EOF */
/* EOF */
97 changes: 53 additions & 44 deletions Tests/libphonenumber/Tests/core/ExampleNumbersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
use libphonenumber\RegionCode;
use libphonenumber\ShortNumberCost;
use libphonenumber\ShortNumberInfo;

Expand All @@ -33,9 +32,14 @@ class ExampleNumbersTest extends \PHPUnit_Framework_TestCase
*/
private $shortNumberInfo;

public function setUp()
public static function setUpBeforeClass()
{
PhoneNumberUtil::resetInstance();
PhoneNumberUtil::getInstance();
}

public function setUp()
{
$this->phoneNumberUtil = PhoneNumberUtil::getInstance();
$this->shortNumberInfo = ShortNumberInfo::getInstance($this->phoneNumberUtil);
}
Expand Down Expand Up @@ -185,38 +189,6 @@ public function shortNumberRegionList()
return $returnList;
}

/**
* @dataProvider shortNumberRegionList
*/
public function testEmergency($regionCode)
{
if ($regionCode == RegionCode::PG) {
// The only short number for Papua New Guinea is 000, which fails the test, since the
// national prefix is 0. This needs to be fixed.
return;
}

$desc = $this->shortNumberInfo->getMetadataForRegion($regionCode)->getEmergency();
if ($desc->hasExampleNumber()) {
$exampleNumber = $desc->getExampleNumber();
$possibleNumberPattern = new Matcher($desc->getPossibleNumberPattern(), $exampleNumber);
if (!$possibleNumberPattern->matches() || !$this->shortNumberInfo->isEmergencyNumber(
$exampleNumber,
$regionCode
)
) {
$this->fail("Emergency example number test failed for " . $regionCode);
} else {
$emergencyNumber = $this->phoneNumberUtil->parse($exampleNumber, $regionCode);
if ($this->shortNumberInfo->getExpectedCost($emergencyNumber) !== ShortNumberCost::TOLL_FREE) {
// TODO: Reenable this when a method is available to get the expected cost for a
// particular region.
// $this->fail("Emergency example number not toll free for " . $regionCode);
}
}
}
}

public function supportedGlobalNetworkCallingCodes()
{
$returnList = array();
Expand Down Expand Up @@ -256,20 +228,14 @@ public function getEveryRegionHasExampleNumber($regionCode)
*/
public function testShortNumbersValidAndCorrectCost($regionCode)
{
if ($regionCode == RegionCode::PG) {
// The only short number for Papua New Guinea is 000, which fails the test, since the
// national prefix is 0. This needs to be fixed.
return;
}

$exampleShortNumber = $this->shortNumberInfo->getExampleShortNumber($regionCode);
if (!$this->shortNumberInfo->isValidShortNumber($exampleShortNumber, $regionCode)) {
if (!$this->shortNumberInfo->isValidShortNumberForRegion($exampleShortNumber, $regionCode)) {
$this->fail(
"Failed validation for string region_code: {$regionCode}, national_number: {$exampleShortNumber}"
);
}
$phoneNumber = $this->phoneNumberUtil->parse($exampleShortNumber, $regionCode);
if (!$this->shortNumberInfo->isValidShortNumberFromNumber($phoneNumber)) {
if (!$this->shortNumberInfo->isValidShortNumber($phoneNumber)) {
$this->fail("Failed validation for " . (string)$phoneNumber);
}

Expand All @@ -283,15 +249,58 @@ public function testShortNumbersValidAndCorrectCost($regionCode)
foreach ($costArray as $cost) {
$exampleShortNumber = $this->shortNumberInfo->getExampleShortNumberForCost($regionCode, $cost);
if ($exampleShortNumber != '') {
$phoneNumber = $this->phoneNumberUtil->parse($exampleShortNumber, $regionCode);
$this->assertEquals(
$cost,
$this->shortNumberInfo->getExpectedCost($phoneNumber),
$this->shortNumberInfo->getExpectedCostForRegion($exampleShortNumber, $regionCode),
"Wrong cost for " . (string)$phoneNumber
);
}
}
}

/**
* @dataProvider shortNumberRegionList
*/
public function testEmergency($regionCode)
{
$desc = $this->shortNumberInfo->getMetadataForRegion($regionCode)->getEmergency();
if ($desc->hasExampleNumber()) {
$exampleNumber = $desc->getExampleNumber();
$possibleNumberPattern = new Matcher($desc->getPossibleNumberPattern(), $exampleNumber);
if (!$possibleNumberPattern->matches() || !$this->shortNumberInfo->isEmergencyNumber(
$exampleNumber,
$regionCode
)
) {
$this->fail("Emergency example number test failed for " . $regionCode);
} elseif ($this->shortNumberInfo->getExpectedCostForRegion(
$exampleNumber,
$regionCode
) !== ShortNumberCost::TOLL_FREE
) {
$this->fail("Emergency example number not toll free for " . $regionCode);
}
}
}

/**
* @dataProvider shortNumberRegionList
*/
public function testCarrierSpecificShortNumbers($regionCode)
{
// Test the carrier-specific tag.
$desc = $this->shortNumberInfo->getMetadataForRegion($regionCode)->getCarrierSpecific();
if ($desc->hasExampleNumber()) {
$exampleNumber = $desc->getExampleNumber();
$carrierSpecificNumber = $this->phoneNumberUtil->parse($exampleNumber, $regionCode);
$exampleNumberMatcher = new Matcher($desc->getPossibleNumberPattern(), $exampleNumber);
if (!$exampleNumberMatcher->matches() ||
!$this->shortNumberInfo->isCarrierSpecific($carrierSpecificNumber)
) {
$this->fail("Carrier-specific test failed for " . $regionCode);
}
}
}
}

/* EOF */
83 changes: 82 additions & 1 deletion Tests/libphonenumber/Tests/core/PhoneNumberUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,43 @@ public function testFormatNumberForMobileDialing()
"+80012345678",
$this->phoneUtil->formatNumberForMobileDialing(self::$internationalTollFree, RegionCode::UN001, false)
);

// Test that a short number is formatted correctly for mobile dialing within the region,
// and is not diallable from outside the region.
$deShortNumber = new PhoneNumber();
$deShortNumber->setCountryCode(49)->setNationalNumber(123);
$this->assertEquals("123", $this->phoneUtil->formatNumberForMobileDialing($deShortNumber, RegionCode::DE, false));
$this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing($deShortNumber, RegionCode::IT, false));

// Test the special logic for Hungary, where the national prefix must be added before dialing
// from a mobile phone for regular length numbers, but not for short numbers.
$huRegularNumber = new PhoneNumber();
$huRegularNumber->setCountryCode(36)->setNationalNumber(301234567);
$this->assertEquals("06301234567", $this->phoneUtil->formatNumberForMobileDialing($huRegularNumber,RegionCode::HU, false));
$this->assertEquals("+36301234567", $this->phoneUtil->formatNumberForMobileDialing($huRegularNumber, RegionCode::JP, false));
$huShortNumber = new PhoneNumber();
$huShortNumber->setCountryCode(36)->setNationalNumber(104);
$this->assertEquals("104", $this->phoneUtil->formatNumberForMobileDialing($huShortNumber, RegionCode::HU,false));
$this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing($huShortNumber, RegionCode::JP, false));

// Test the special logic for NANPA countries, for which regular length phone numbers are always
// output in international format, but short numbers are in national format.
$usRegularNumber = new PhoneNumber();
$usRegularNumber->setCountryCode(1)->setNationalNumber(6502530000);;
$this->assertEquals("+16502530000", $this->phoneUtil->formatNumberForMobileDialing($usRegularNumber,RegionCode::US, false));
$this->assertEquals("+16502530000", $this->phoneUtil->formatNumberForMobileDialing($usRegularNumber,RegionCode::CA, false));
$this->assertEquals("+16502530000", $this->phoneUtil->formatNumberForMobileDialing($usRegularNumber,RegionCode::BR, false));
$usShortNumber = new PhoneNumber();
$usShortNumber->setCountryCode(1)->setNationalNumber(911);
$this->assertEquals("911", $this->phoneUtil->formatNumberForMobileDialing($usShortNumber, RegionCode::US, false));
$this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing($usShortNumber, RegionCode::CA, false));
$this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing($usShortNumber, RegionCode::BR, false));

// Test that the Australian emergency number 000 is formatted correctly.
$auNumber = new PhoneNumber();
$auNumber->setCountryCode(61)->setNationalNumber(0)->setItalianLeadingZero(true)->setNumberOfLeadingZeros(2);
$this->assertEquals("000", $this->phoneUtil->formatNumberForMobileDialing($auNumber, RegionCode::AU, false));
$this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing($auNumber, RegionCode::NZ, false));
}

public function testFormatByPattern()
Expand Down Expand Up @@ -2790,6 +2827,27 @@ public function testParseNumbersWithPlusWithNoRegion()
$this->assertEquals($nzNumberWithRawInput, $this->phoneUtil->parseAndKeepRawInput("+64 3 331 6005", null));
}

public function testParseNumberTooShortIfNationalPrefixStripped()
{
// Test that a number whose first digits happen to coincide with the national prefix does not
// get them stripped if doing so would result in a number too short to be a possible (regular
// length) phone number for that region.
$byNumber = new PhoneNumber();
$byNumber->setCountryCode(375)->setNationalNumber(8123);
$this->assertEquals($byNumber, $this->phoneUtil->parse("8123", RegionCode::BY));
$byNumber->setNationalNumber(81234);
$this->assertEquals($byNumber, $this->phoneUtil->parse("81234", RegionCode::BY));

// The prefix doesn't get stripped, since the input is a viable 6-digit number, whereas the
// result of stripping is only 5 digits.
$byNumber->setNationalNumber(812345);
$this->assertEquals($byNumber, $this->phoneUtil->parse("812345", RegionCode::BY));

// The prefix gets stripped, since only 6-digit numbers are possible.
$byNumber->setNationalNumber(123456);
$this->assertEquals($byNumber, $this->phoneUtil->parse("8123456", RegionCode::BY));
}

public function testParseExtensions()
{
$nzNumber = new PhoneNumber();
Expand Down Expand Up @@ -2935,6 +2993,29 @@ public function testParseAndKeepRaw()
$this->assertEquals($koreanNumber, $this->phoneUtil->parseAndKeepRawInput("08122123456", RegionCode::KR));
}

public function testParseItalianLeadingZeros()
{
// Test the number "011".
$oneZero = new PhoneNumber();
$oneZero->setCountryCode(61)->setNationalNumber(11)->setItalianLeadingZero(true);
$this->assertEquals($oneZero, $this->phoneUtil->parse("011", RegionCode::AU));

// Test the number "001".
$twoZeros = new PhoneNumber();
$twoZeros->setCountryCode(61)->setNationalNumber(1)->setItalianLeadingZero(true)->setNumberOfLeadingZeros(2);
$this->assertEquals($twoZeros, $this->phoneUtil->parse("001", RegionCode::AU));

// Test the number "000". This number has 2 leading zeros.
$stillTwoZeros = new PhoneNumber();
$stillTwoZeros->setCountryCode(61)->setNationalNumber(0)->setItalianLeadingZero(true)->setNumberOfLeadingZeros(2);
$this->assertEquals($stillTwoZeros, $this->phoneUtil->parse("000", RegionCode::AU));

// Test the number "0000". This number has 3 leading zeros.
$threeZeros = new PhoneNumber();
$threeZeros->setCountryCode(61)->setNationalNumber(0)->setItalianLeadingZero(true)->setNumberOfLeadingZeros(3);
$this->assertEquals($threeZeros, $this->phoneUtil->parse("0000", RegionCode::AU));
}

public function testCountryWithNoNumberDesc()
{
// Andorra is a country where we don't have PhoneNumberDesc info in the metadata.
Expand Down Expand Up @@ -2968,7 +3049,7 @@ public function testUnknownCountryCallingCode()

public function testIsNumberMatchMatches()
{
// Test simple matches where formatting is different, or leading zeroes, or country calling code
// Test simple matches where formatting is different, or leading zeros, or country calling code
// has been specified.
$this->assertEquals(
MatchType::EXACT_MATCH,
Expand Down
Loading

0 comments on commit 7e563c9

Please sign in to comment.