Skip to content

Commit

Permalink
Merge pull request #6 from giggsey/google_v5.8
Browse files Browse the repository at this point in the history
v5.8
  • Loading branch information
giggsey committed Sep 10, 2013
2 parents 3227272 + c3d7881 commit 25c8d82
Show file tree
Hide file tree
Showing 547 changed files with 27,902 additions and 1,977 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ $RECYCLE.BIN/
Session.vim
.netrwhist
composer.phar

composer.lock
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ A sister project also exists for [libphonenumber-geocoder](https://github.com/gi
* getExampleNumber/getExampleNumberByType - provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed.
* isValidNumber - full validation of a phone number for a region using length and prefix information.

## Installation

The library can be installed via [composer](http://getcomposer.org/). You can also use any other [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) compliant autoloader.

```json
{
"require": {
"giggsey/libphonenumber-for-php": "~5.8"
}
}
```


## Online Demo
An [online demo](http://giggsey.com/libphonenumber/) is available for both [libphonenumber-for-php](https://github.com/giggsey/libphonenumber-for-php) and [libphonenumber-geocoder](https://github.com/giggsey/libphonenumber-geocoder).
Expand Down
10 changes: 5 additions & 5 deletions Tests/PhoneNumberUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ public function testCanBeInternationallyDialled()
$this->assertTrue($this->phoneUtil->canBeInternationallyDialled(self::$internationalTollFree));
}

public function testParseNationalNumber()
public function testParseNationalNumber()
{
// National prefix attached.
$this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("033316005", RegionCode::NZ));
Expand Down Expand Up @@ -1429,10 +1429,10 @@ public function testParseNationalNumber()
// parse the output we produce when formatting the number.
$this->assertEquals(self::$jpStarNumber, $this->phoneUtil->parse("+81 *2345", RegionCode::JP));

// Test national number bigger than max 32-bit signed integer
$inNumber = new PhoneNumber();
$inNumber->setCountryCode(55)->setNationalNumber(9876543210);
$this->assertEquals($inNumber, $this->phoneUtil->parse("9876543210", RegionCode::BR));
$shortNumber = new PhoneNumber();
$shortNumber->setCountryCode(64)->setNationalNumber(12);
$this->assertEquals($shortNumber, $this->phoneUtil->parse("12", RegionCode::NZ));

}

public function testIsAlphaNumber()
Expand Down
282 changes: 282 additions & 0 deletions Tests/ShortNumberInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
<?php
/**
*
*
* @author joshuag
* @created: 04/09/13 09:32
* @project libphonenumber-for-php
*/

namespace libphonenumber\Tests;

use libphonenumber\CountryCodeToRegionCodeMapForTesting;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberUtil;
use libphonenumber\RegionCode;
use libphonenumber\ShortNumberCost;
use libphonenumber\ShortNumberInfo;

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/PhoneNumberUtilTest.php';

class ShortNumberInfoTest extends \PHPUnit_Framework_TestCase
{
private static $plusSymbol;
/**
* @var ShortNumberInfo
*/
private $shortInfo;

public function setUp()
{
self::$plusSymbol = pack('H*', 'efbc8b');

PhoneNumberUtil::resetInstance();
$this->shortInfo = ShortNumberInfo::getInstance(
PhoneNumberUtil::getInstance(
PhoneNumberUtilTest::TEST_META_DATA_FILE_PREFIX,
CountryCodeToRegionCodeMapForTesting::$countryCodeToRegionCodeMapForTesting
)
);
}

public function testIsPossibleShortNumber()
{
$possibleNumber = new PhoneNumber();
$possibleNumber->setCountryCode(33)->setNationalNumber(123456);

$this->assertTrue($this->shortInfo->isPossibleShortNumberFromNumber($possibleNumber));
$this->assertTrue($this->shortInfo->isPossibleShortNumber(123456, RegionCode::FR));

$impossibleNumber = new PhoneNumber();
$impossibleNumber->setCountryCode(33)->setNationalNumber(9);
$this->assertFalse($this->shortInfo->isPossibleShortNumberFromNumber($impossibleNumber));
$this->assertFalse($this->shortInfo->isPossibleShortNumber(9, RegionCode::FR));
}

public function testIsValidShortNumber()
{
$phoneNumberObj = new PhoneNumber();
$phoneNumberObj->setCountryCode(33)->setNationalNumber(1010);
$this->assertTrue($this->shortInfo->isValidShortNumberFromNumber($phoneNumberObj));
$this->assertTrue($this->shortInfo->isValidShortNumber(1010, RegionCode::FR));

$phoneNumberObj = new PhoneNumber();
$phoneNumberObj->setCountryCode(33)->setNationalNumber(123456);
$this->assertFalse($this->shortInfo->isValidShortNumberFromNumber($phoneNumberObj));
$this->assertFalse($this->shortInfo->isValidShortNumber(123456, RegionCode::FR));

// Note that GB and GG share the country calling code 44
$phoneNumberObj = new PhoneNumber();
$phoneNumberObj->setCountryCode(44)->setNationalNumber(18001);
$this->assertTrue($this->shortInfo->isValidShortNumberFromNumber($phoneNumberObj));
}

public function testGetExpectedCost()
{
$premiumRateNumber = new PhoneNumber();
$premiumRateNumber->setCountryCode(33)->setNationalNumber(
$this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::PREMIUM_RATE)
);
$this->assertEquals(ShortNumberCost::PREMIUM_RATE, $this->shortInfo->getExpectedCost($premiumRateNumber));

$standardRateNumber = new PhoneNumber();
$standardRateNumber->setCountryCode(33)->setNationalNumber(
$this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::STANDARD_RATE)
);
$this->assertEquals(
ShortNumberCost::STANDARD_RATE,
$this->shortInfo->getExpectedCost($standardRateNumber)
);

$tollFreeNumber = new PhoneNumber();
$tollFreeNumber->setCountryCode(33)->setNationalNumber(
$this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::TOLL_FREE)
);
$this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCost($tollFreeNumber));

$unknownCostNumber = new PhoneNumber();
$unknownCostNumber->setCountryCode(33)->setNationalNumber(12345);
$this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCost($unknownCostNumber));

// Test that an invalid number may nevertheless have a cost other than UNKNOWN_COST.
$invalidNumber = new PhoneNumber();
$invalidNumber->setCountryCode(33)->setNationalNumber(116123);
$this->assertFalse($this->shortInfo->isValidShortNumberFromNumber($invalidNumber));
$this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCost($invalidNumber));

// Test a non-existent country code.
$unknownCostNumber->clear();
$unknownCostNumber->setCountryCode(123)->setNationalNumber(911);
$this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCost($unknownCostNumber));
}

public function testGetExampleShortNumber()
{
$this->assertEquals("8711", $this->shortInfo->getExampleShortNumber(RegionCode::AM));
$this->assertEquals("1010", $this->shortInfo->getExampleShortNumber(RegionCode::FR));
$this->assertEquals("", $this->shortInfo->getExampleShortNumber(RegionCode::UN001));
$this->assertEquals("", $this->shortInfo->getExampleShortNumber(null));
}

public function testGetExampleShortNumberForCost()
{
$this->assertEquals(
"3010",
$this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::TOLL_FREE)
);
$this->assertEquals(
"118777",
$this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::STANDARD_RATE)
);
$this->assertEquals(
"3200",
$this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::PREMIUM_RATE)
);
$this->assertEquals(
"",
$this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::UNKNOWN_COST)
);
}

public function testConnectsToEmergencyNumber_US()
{
$this->assertTrue($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::US));
$this->assertTrue($this->shortInfo->connectsToEmergencyNumber("112", RegionCode::US));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("999", RegionCode::US));
}

public function testConnectsToEmergencyNumberLongNumber_US()
{
$this->assertTrue($this->shortInfo->connectsToEmergencyNumber("9116666666", RegionCode::US));
$this->assertTrue($this->shortInfo->connectsToEmergencyNumber("1126666666", RegionCode::US));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9996666666", RegionCode::US));
}

public function testConnectsToEmergencyNumberWithFormatting_US()
{
$this->assertTrue($this->shortInfo->connectsToEmergencyNumber("9-1-1", RegionCode::US));
$this->assertTrue($this->shortInfo->connectsToEmergencyNumber("1-1-2", RegionCode::US));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9-9-9", RegionCode::US));
}

public function testConnectsToEmergencyNumberWithPlusSign_US()
{
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("+911", RegionCode::US));
$this->assertFalse(
$this->shortInfo->connectsToEmergencyNumber(self::$plusSymbol . "911", RegionCode::US)
);
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber(" +911", RegionCode::US));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("+112", RegionCode::US));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("+999", RegionCode::US));
}

public function testConnectsToEmergencyNumber_BR()
{
$this->assertTrue($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::BR));
$this->assertTrue($this->shortInfo->connectsToEmergencyNumber("190", RegionCode::BR));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("999", RegionCode::BR));
}

public function testConnectsToEmergencyNumberLongNumber_BR()
{
// Brazilian emergency numbers don't work when additional digits are appended.
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9111", RegionCode::BR));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("1900", RegionCode::BR));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9996", RegionCode::BR));
}

public function testConnectsToEmergencyNumber_CL()
{
$this->assertTrue($this->shortInfo->connectsToEmergencyNumber('131', RegionCode::CL));
$this->assertTrue($this->shortInfo->connectsToEmergencyNumber('133', RegionCode::CL));
}

public function testConnectsToEmergencyNumberLongNumber_CL()
{
// Chilean emergency numbers don't work when additional digits are appended.
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber('1313', RegionCode::CL));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber('1330', RegionCode::CL));
}

public function testConnectsToEmergencyNumber_AO()
{
// Angola doesn't have any metadata for emergency numbers in the test metadata.
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::AO));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("222123456", RegionCode::BR));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("923123456", RegionCode::BR));
}

public function testConnectsToEmergencyNumber_ZW()
{
// Zimbabwe doesn't have any metadata in the test metadata.
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::ZW));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("01312345", RegionCode::ZW));
$this->assertFalse($this->shortInfo->connectsToEmergencyNumber("0711234567", RegionCode::ZW));
}

public function testIsEmergencyNumber_US()
{
$this->assertTrue($this->shortInfo->isEmergencyNumber("911", RegionCode::US));
$this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::US));
$this->assertFalse($this->shortInfo->isEmergencyNumber("999", RegionCode::US));
}

public function testIsEmergencyNumberLongNumber_US()
{
$this->assertFalse($this->shortInfo->isEmergencyNumber("9116666666", RegionCode::US));
$this->assertFalse($this->shortInfo->isEmergencyNumber("1126666666", RegionCode::US));
$this->assertFalse($this->shortInfo->isEmergencyNumber("9996666666", RegionCode::US));
}

public function testIsEmergencyNumberWithFormatting_US()
{
$this->assertTrue($this->shortInfo->isEmergencyNumber("9-1-1", RegionCode::US));
$this->assertTrue($this->shortInfo->isEmergencyNumber("*911", RegionCode::US));
$this->assertTrue($this->shortInfo->isEmergencyNumber("1-1-2", RegionCode::US));
$this->assertTrue($this->shortInfo->isEmergencyNumber("*112", RegionCode::US));
$this->assertFalse($this->shortInfo->isEmergencyNumber("9-9-9", RegionCode::US));
$this->assertFalse($this->shortInfo->isEmergencyNumber("*999", RegionCode::US));
}

public function testIsEmergencyNumberWithPlusSign_US()
{
$this->assertFalse($this->shortInfo->isEmergencyNumber("+911", RegionCode::US));
$this->assertFalse($this->shortInfo->isEmergencyNumber(self::$plusSymbol . "911", RegionCode::US));
$this->assertFalse($this->shortInfo->isEmergencyNumber(" +911", RegionCode::US));
$this->assertFalse($this->shortInfo->isEmergencyNumber("+112", RegionCode::US));
$this->assertFalse($this->shortInfo->isEmergencyNumber("+999", RegionCode::US));
}

public function testIsEmergencyNumber_BR()
{
$this->assertTrue($this->shortInfo->isEmergencyNumber("911", RegionCode::BR));
$this->assertTrue($this->shortInfo->isEmergencyNumber("190", RegionCode::BR));
$this->assertFalse($this->shortInfo->isEmergencyNumber("999", RegionCode::BR));
}

public function testIsEmergencyNumberLongNumber_BR()
{
$this->assertFalse($this->shortInfo->isEmergencyNumber("9111", RegionCode::BR));
$this->assertFalse($this->shortInfo->isEmergencyNumber("1900", RegionCode::BR));
$this->assertFalse($this->shortInfo->isEmergencyNumber("9996", RegionCode::BR));
}

public function testIsEmergencyNumber_AO()
{
// Angola doesn't have any metadata for emergency numbers in the test metadata.
$this->assertFalse($this->shortInfo->isEmergencyNumber("911", RegionCode::AO));
$this->assertFalse($this->shortInfo->isEmergencyNumber("222123456", RegionCode::AO));
$this->assertFalse($this->shortInfo->isEmergencyNumber("923123456", RegionCode::AO));
}

public function testIsEmergencyNumber_ZW()
{
// Zimbabwe doesn't have any metadata in the test metadata.
$this->assertFalse($this->shortInfo->isEmergencyNumber("911", RegionCode::ZW));
$this->assertFalse($this->shortInfo->isEmergencyNumber("01312345", RegionCode::ZW));
$this->assertFalse($this->shortInfo->isEmergencyNumber("0711234567", RegionCode::ZW));
}
}

/* EOF */
Loading

0 comments on commit 25c8d82

Please sign in to comment.