Updated code to follow the PSR-2 style guidelines.
This commit is contained in:
parent
95813b00a9
commit
ed6166f934
|
@ -6,7 +6,12 @@ php:
|
||||||
- 5.4
|
- 5.4
|
||||||
- 5.5
|
- 5.5
|
||||||
|
|
||||||
script: phpunit
|
before_install:
|
||||||
|
- pear install pear/PHP_CodeSniffer
|
||||||
|
|
||||||
|
script:
|
||||||
|
- phpcs --standard=PSR2 src/
|
||||||
|
- phpunit
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
email:
|
email:
|
||||||
|
|
4
README
4
README
|
@ -58,6 +58,10 @@ SUPPORT
|
||||||
to the client API please see http://www.maxmind.com/en/support for
|
to the client API please see http://www.maxmind.com/en/support for
|
||||||
details.
|
details.
|
||||||
|
|
||||||
|
CONTRIBUTING
|
||||||
|
Patches and pull requests are encouraged. All code should follow the
|
||||||
|
PSR-2 style guidelines. Please include unit tests whenever possible.
|
||||||
|
|
||||||
AUTHOR
|
AUTHOR
|
||||||
Gregory Oschwald <goschwald@maxmind.com>
|
Gregory Oschwald <goschwald@maxmind.com>
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,13 @@ class HttpException extends \Exception
|
||||||
{
|
{
|
||||||
public $code;
|
public $code;
|
||||||
|
|
||||||
public function __construct($message, $code, $uri,
|
public function __construct(
|
||||||
Exception $previous = null)
|
$message,
|
||||||
{
|
$code,
|
||||||
|
$uri,
|
||||||
|
Exception $previous = null
|
||||||
|
) {
|
||||||
$this->code = $code;
|
$this->code = $code;
|
||||||
parent::__construct($message, null, $previous);
|
parent::__construct($message, null, $previous);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -6,11 +6,14 @@ class WebserviceException extends HttpException
|
||||||
{
|
{
|
||||||
public $httpStatus;
|
public $httpStatus;
|
||||||
|
|
||||||
public function __construct($message, $code, $httpStatus, $uri,
|
public function __construct(
|
||||||
Exception $previous = null)
|
$message,
|
||||||
{
|
$code,
|
||||||
|
$httpStatus,
|
||||||
|
$uri,
|
||||||
|
Exception $previous = null
|
||||||
|
) {
|
||||||
$this->httpStatus = $httpStatus;
|
$this->httpStatus = $httpStatus;
|
||||||
parent::__construct($message, $code, $uri, $previous);
|
parent::__construct($message, $code, $uri, $previous);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@ class City extends Country
|
||||||
protected $city;
|
protected $city;
|
||||||
protected $location;
|
protected $location;
|
||||||
protected $postal;
|
protected $postal;
|
||||||
protected $subdivisions = Array();
|
protected $subdivisions = array();
|
||||||
|
|
||||||
public function __construct($raw, $languages)
|
public function __construct($raw, $languages)
|
||||||
{
|
{
|
||||||
|
@ -20,12 +20,33 @@ class City extends Country
|
||||||
$this->createSubdivisions($raw, $languages);
|
$this->createSubdivisions($raw, $languages);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createSubdivisions($raw, $languages) {
|
private function createSubdivisions($raw, $languages)
|
||||||
if(!isset($raw['subdivisions'])) return;
|
{
|
||||||
|
if (!isset($raw['subdivisions'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($raw['subdivisions'] as $sub) {
|
foreach ($raw['subdivisions'] as $sub) {
|
||||||
array_push($this->subdivisions,
|
array_push(
|
||||||
new \GeoIP2\Record\Subdivision($sub, $languages));
|
$this->subdivisions,
|
||||||
|
new \GeoIP2\Record\Subdivision($sub, $languages)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __get($attr)
|
||||||
|
{
|
||||||
|
if ($attr == 'mostSpecificSubdivision') {
|
||||||
|
return $this->$attr();
|
||||||
|
} else {
|
||||||
|
return parent::__get($attr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function mostSpecificSubdivision()
|
||||||
|
{
|
||||||
|
return empty($this->subdivisions)?
|
||||||
|
new \GeoIP2\Record\Subdivision(array(), $this->languages):
|
||||||
|
end($this->subdivisions);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -4,5 +4,4 @@ namespace GeoIP2\Model;
|
||||||
|
|
||||||
class CityIspOrg extends City
|
class CityIspOrg extends City
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
|
@ -6,33 +6,47 @@ class Country
|
||||||
{
|
{
|
||||||
private $continent;
|
private $continent;
|
||||||
private $country;
|
private $country;
|
||||||
|
private $languages;
|
||||||
private $registeredCountry;
|
private $registeredCountry;
|
||||||
private $representedCountry;
|
private $representedCountry;
|
||||||
private $traits;
|
private $traits;
|
||||||
private $raw;
|
private $raw;
|
||||||
|
|
||||||
public function __construct($raw, $languages) {
|
public function __construct($raw, $languages)
|
||||||
|
{
|
||||||
$this->raw = $raw;
|
$this->raw = $raw;
|
||||||
|
|
||||||
$this->continent = new \GeoIP2\Record\Continent($this->get('continent'),
|
$this->continent = new \GeoIP2\Record\Continent(
|
||||||
$languages);
|
$this->get('continent'),
|
||||||
$this->country = new \GeoIP2\Record\Country($this->get('country'),
|
$languages
|
||||||
$languages);
|
);
|
||||||
$this->registeredCountry =
|
$this->country = new \GeoIP2\Record\Country(
|
||||||
new \GeoIP2\Record\Country($this->get('registered_country'), $languages);
|
$this->get('country'),
|
||||||
$this->representedCountry =
|
$languages
|
||||||
new \GeoIP2\Record\RepresentedCountry($this->get('represented_country'),
|
);
|
||||||
$languages);
|
$this->registeredCountry = new \GeoIP2\Record\Country(
|
||||||
|
$this->get('registered_country'),
|
||||||
|
$languages
|
||||||
|
);
|
||||||
|
$this->representedCountry = new \GeoIP2\Record\RepresentedCountry(
|
||||||
|
$this->get('represented_country'),
|
||||||
|
$languages
|
||||||
|
);
|
||||||
$this->traits = new \GeoIP2\Record\Traits($this->get('traits'));
|
$this->traits = new \GeoIP2\Record\Traits($this->get('traits'));
|
||||||
|
|
||||||
|
$this->languages = $languages;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function get($field) {
|
protected function get($field)
|
||||||
return isset($this->raw[$field]) ? $this->raw[$field] : Array();
|
{
|
||||||
|
return isset($this->raw[$field]) ? $this->raw[$field] : array();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __get ($attr)
|
public function __get ($attr)
|
||||||
{
|
{
|
||||||
if ($attr != "instance" && isset($this->$attr)) return $this->$attr;
|
if ($attr != "instance" && isset($this->$attr)) {
|
||||||
|
return $this->$attr;
|
||||||
|
}
|
||||||
|
|
||||||
throw new \RuntimeException("Unknown attribute: $attr");
|
throw new \RuntimeException("Unknown attribute: $attr");
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,5 +4,4 @@ namespace GeoIP2\Model;
|
||||||
|
|
||||||
class Omni extends CityIspOrg
|
class Omni extends CityIspOrg
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,14 @@ abstract class AbstractPlaceRecord extends AbstractRecord
|
||||||
{
|
{
|
||||||
private $languages;
|
private $languages;
|
||||||
|
|
||||||
public function __construct($record, $languages){
|
public function __construct($record, $languages)
|
||||||
|
{
|
||||||
$this->languages = $languages;
|
$this->languages = $languages;
|
||||||
parent::__construct($record);
|
parent::__construct($record);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __get($attr) {
|
public function __get($attr)
|
||||||
|
{
|
||||||
if ($attr == 'name') {
|
if ($attr == 'name') {
|
||||||
return $this->name();
|
return $this->name();
|
||||||
} else {
|
} else {
|
||||||
|
@ -19,10 +21,12 @@ abstract class AbstractPlaceRecord extends AbstractRecord
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function name()
|
||||||
private function name() {
|
{
|
||||||
foreach ($this->languages as $language) {
|
foreach ($this->languages as $language) {
|
||||||
if (isset($this->names[$language])) return $this->names[$language];
|
if (isset($this->names[$language])) {
|
||||||
|
return $this->names[$language];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,11 +6,13 @@ abstract class AbstractRecord
|
||||||
{
|
{
|
||||||
private $record;
|
private $record;
|
||||||
|
|
||||||
public function __construct($record) {
|
public function __construct($record)
|
||||||
|
{
|
||||||
$this->record = $record;
|
$this->record = $record;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __get($attr) {
|
public function __get($attr)
|
||||||
|
{
|
||||||
$valid = in_array($attr, $this->validAttributes);
|
$valid = in_array($attr, $this->validAttributes);
|
||||||
// XXX - kind of ugly but greatly reduces boilerplate code
|
// XXX - kind of ugly but greatly reduces boilerplate code
|
||||||
$key = strtolower(preg_replace('/([A-Z])/', '_\1', $attr));
|
$key = strtolower(preg_replace('/([A-Z])/', '_\1', $attr));
|
||||||
|
|
|
@ -4,5 +4,5 @@ namespace GeoIP2\Record;
|
||||||
|
|
||||||
class City extends AbstractPlaceRecord
|
class City extends AbstractPlaceRecord
|
||||||
{
|
{
|
||||||
protected $validAttributes = Array('confidence', 'geonameId', 'names');
|
protected $validAttributes = array('confidence', 'geonameId', 'names');
|
||||||
}
|
}
|
|
@ -4,7 +4,9 @@ namespace GeoIP2\Record;
|
||||||
|
|
||||||
class Continent extends AbstractPlaceRecord
|
class Continent extends AbstractPlaceRecord
|
||||||
{
|
{
|
||||||
protected $validAttributes = Array('continentCode',
|
protected $validAttributes = array(
|
||||||
|
'continentCode',
|
||||||
'geonameId',
|
'geonameId',
|
||||||
'names');
|
'names'
|
||||||
|
);
|
||||||
}
|
}
|
|
@ -4,9 +4,10 @@ namespace GeoIP2\Record;
|
||||||
|
|
||||||
class Country extends AbstractPlaceRecord
|
class Country extends AbstractPlaceRecord
|
||||||
{
|
{
|
||||||
|
protected $validAttributes = array(
|
||||||
protected $validAttributes = Array('confidence',
|
'confidence',
|
||||||
'geonameId',
|
'geonameId',
|
||||||
'isoCode',
|
'isoCode',
|
||||||
'names');
|
'names'
|
||||||
|
);
|
||||||
}
|
}
|
|
@ -4,11 +4,13 @@ namespace GeoIP2\Record;
|
||||||
|
|
||||||
class Location extends AbstractRecord
|
class Location extends AbstractRecord
|
||||||
{
|
{
|
||||||
protected $validAttributes = Array('accuracyRadius',
|
protected $validAttributes = array(
|
||||||
|
'accuracyRadius',
|
||||||
'latitude',
|
'latitude',
|
||||||
'longitude',
|
'longitude',
|
||||||
'metroCode',
|
'metroCode',
|
||||||
'postalCode',
|
'postalCode',
|
||||||
'postalConfidence',
|
'postalConfidence',
|
||||||
'timeZone');
|
'timeZone'
|
||||||
|
);
|
||||||
}
|
}
|
|
@ -4,5 +4,5 @@ namespace GeoIP2\Record;
|
||||||
|
|
||||||
class Postal extends AbstractRecord
|
class Postal extends AbstractRecord
|
||||||
{
|
{
|
||||||
protected $validAttributes = Array('code', 'confidence');
|
protected $validAttributes = array('code', 'confidence');
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,11 @@ namespace GeoIP2\Record;
|
||||||
|
|
||||||
class RepresentedCountry extends Country
|
class RepresentedCountry extends Country
|
||||||
{
|
{
|
||||||
protected $validAttributes = Array('confidence',
|
protected $validAttributes = array(
|
||||||
|
'confidence',
|
||||||
'geonameId',
|
'geonameId',
|
||||||
'isoCode',
|
'isoCode',
|
||||||
'names',
|
'namespace',
|
||||||
'type');
|
'type'
|
||||||
|
);
|
||||||
}
|
}
|
|
@ -4,8 +4,10 @@ namespace GeoIP2\Record;
|
||||||
|
|
||||||
class Subdivision extends AbstractPlaceRecord
|
class Subdivision extends AbstractPlaceRecord
|
||||||
{
|
{
|
||||||
protected $validAttributes = Array('confidence',
|
protected $validAttributes = array(
|
||||||
|
'confidence',
|
||||||
'geonameId',
|
'geonameId',
|
||||||
'isoCode',
|
'isoCode',
|
||||||
'names');
|
'names'
|
||||||
|
);
|
||||||
}
|
}
|
|
@ -4,7 +4,8 @@ namespace GeoIP2\Record;
|
||||||
|
|
||||||
class Traits extends AbstractRecord
|
class Traits extends AbstractRecord
|
||||||
{
|
{
|
||||||
protected $validAttributes = Array('autonomousSystemNumber',
|
protected $validAttributes = array(
|
||||||
|
'autonomousSystemNumber',
|
||||||
'autonomousSystemOrganization',
|
'autonomousSystemOrganization',
|
||||||
'domain',
|
'domain',
|
||||||
'isAnonymousProxy',
|
'isAnonymousProxy',
|
||||||
|
@ -12,6 +13,6 @@ class Traits extends AbstractRecord
|
||||||
'isp',
|
'isp',
|
||||||
'ipAddress',
|
'ipAddress',
|
||||||
'organization',
|
'organization',
|
||||||
'userType');
|
'userType'
|
||||||
|
);
|
||||||
}
|
}
|
|
@ -23,9 +23,12 @@ class Client
|
||||||
private $baseUri = 'https://geoip.maxmind.com/geoip/v2.0';
|
private $baseUri = 'https://geoip.maxmind.com/geoip/v2.0';
|
||||||
private $guzzleClient;
|
private $guzzleClient;
|
||||||
|
|
||||||
public function __construct($userId, $licenseKey, $languages=array('en'),
|
public function __construct(
|
||||||
$guzzleClient = null)
|
$userId,
|
||||||
{
|
$licenseKey,
|
||||||
|
$languages = array('en'),
|
||||||
|
$guzzleClient = null
|
||||||
|
) {
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
$this->licenseKey = $licenseKey;
|
$this->licenseKey = $licenseKey;
|
||||||
$this->languages = $languages;
|
$this->languages = $languages;
|
||||||
|
@ -57,7 +60,8 @@ class Client
|
||||||
{
|
{
|
||||||
$uri = implode('/', array($this->baseUri, $path, $ipAddress));
|
$uri = implode('/', array($this->baseUri, $path, $ipAddress));
|
||||||
|
|
||||||
$client = $this->guzzleClient ? $this->guzzleClient : new GuzzleClient();
|
$client = $this->guzzleClient ?
|
||||||
|
$this->guzzleClient : new GuzzleClient();
|
||||||
$request = $client->get($uri, array('Accept' => 'application/json'));
|
$request = $client->get($uri, array('Accept' => 'application/json'));
|
||||||
$request->setAuth($this->userId, $this->licenseKey);
|
$request->setAuth($this->userId, $this->licenseKey);
|
||||||
$ua = $request->getHeader('User-Agent');
|
$ua = $request->getHeader('User-Agent');
|
||||||
|
@ -67,11 +71,9 @@ class Client
|
||||||
$response = null;
|
$response = null;
|
||||||
try {
|
try {
|
||||||
$response = $request->send();
|
$response = $request->send();
|
||||||
}
|
} catch (ClientErrorResponseException $e) {
|
||||||
catch (ClientErrorResponseException $e) {
|
|
||||||
$this->handle4xx($e->getResponse(), $uri);
|
$this->handle4xx($e->getResponse(), $uri);
|
||||||
}
|
} catch (ServerErrorResponseException $e) {
|
||||||
catch (ServerErrorResponseException $e) {
|
|
||||||
$this->handle5xx($e->getResponse(), $uri);
|
$this->handle5xx($e->getResponse(), $uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,8 +81,7 @@ class Client
|
||||||
$body = $this->handleSuccess($response, $uri);
|
$body = $this->handleSuccess($response, $uri);
|
||||||
$class = "GeoIP2\\Model\\" . $class;
|
$class = "GeoIP2\\Model\\" . $class;
|
||||||
return new $class($body, $this->languages);
|
return new $class($body, $this->languages);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$this->handleNon200($response, $uri);
|
$this->handleNon200($response, $uri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,14 +89,19 @@ class Client
|
||||||
private function handleSuccess($response, $uri)
|
private function handleSuccess($response, $uri)
|
||||||
{
|
{
|
||||||
if ($response->getContentLength() == 0) {
|
if ($response->getContentLength() == 0) {
|
||||||
throw new GenericException("Received a 200 response for $uri but did not receive a HTTP body.");
|
throw new GenericException(
|
||||||
|
"Received a 200 response for $uri but did not " .
|
||||||
|
"receive a HTTP body."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $response->json();
|
return $response->json();
|
||||||
}
|
} catch (RuntimeException $e) {
|
||||||
catch (RuntimeException $e) {
|
throw new GenericException(
|
||||||
throw new GenericException("Received a 200 response for $uri but could not decode the response as JSON: " . $e->getMessage());
|
"Received a 200 response for $uri but could not decode " .
|
||||||
|
"the response as JSON: " . $e->getMessage()
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,40 +117,64 @@ class Client
|
||||||
try {
|
try {
|
||||||
$body = $response->json();
|
$body = $response->json();
|
||||||
if (!isset($body['code']) || !isset($body['error'])) {
|
if (!isset($body['code']) || !isset($body['error'])) {
|
||||||
throw new GenericException('Response contains JSON but it does not specify code or error keys: ' . $response->getBody());
|
throw new GenericException(
|
||||||
|
'Response contains JSON but it does not specify ' .
|
||||||
|
'code or error keys: ' . $response->getBody()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
} catch (RuntimeException $e) {
|
||||||
|
throw new HttpException(
|
||||||
|
"Received a $status error for $uri but it did not " .
|
||||||
|
"include the expected JSON body: " .
|
||||||
|
$e->getMessage(),
|
||||||
|
$status,
|
||||||
|
$uri
|
||||||
|
);
|
||||||
}
|
}
|
||||||
catch (RuntimeException $e){
|
} else {
|
||||||
throw new HttpException("Received a $status error for $uri but it did not include the expected JSON body: " . $e->getMessage(), $status, $uri);
|
throw new HttpException(
|
||||||
|
"Received a $status error for $uri with the " .
|
||||||
|
"following body: " . $response->getBody(),
|
||||||
|
$status,
|
||||||
|
$uri
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
throw new HttpException(
|
||||||
throw new HttpException("Received a $status error for $uri with the following body: " . $response->getBody(),
|
"Received a $status error for $uri with no body",
|
||||||
$status, $uri);
|
$status,
|
||||||
}
|
$uri
|
||||||
}
|
);
|
||||||
else {
|
|
||||||
throw new HttpException("Received a $status error for $uri with no body",
|
|
||||||
$status, $uri);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new WebserviceException($body['error'], $body['code'], $status, $uri);
|
throw new WebserviceException(
|
||||||
|
$body['error'],
|
||||||
|
$body['code'],
|
||||||
|
$status,
|
||||||
|
$uri
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function handle5xx($response, $uri)
|
private function handle5xx($response, $uri)
|
||||||
{
|
{
|
||||||
$status = $response->getStatusCode();
|
$status = $response->getStatusCode();
|
||||||
|
|
||||||
throw new HttpException("Received a server error ($status) for $uri",
|
throw new HttpException(
|
||||||
$status,$uri);
|
"Received a server error ($status) for $uri",
|
||||||
|
$status,
|
||||||
|
$uri
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function handleNon200($response, $uri)
|
private function handleNon200($response, $uri)
|
||||||
{
|
{
|
||||||
$status = $response->getStatusCode();
|
$status = $response->getStatusCode();
|
||||||
|
|
||||||
throw new HttpException("Received a very surprising HTTP status " .
|
throw new HttpException(
|
||||||
|
"Received a very surprising HTTP status " .
|
||||||
"($status) for $uri",
|
"($status) for $uri",
|
||||||
$status, $uri);
|
$status,
|
||||||
|
$uri
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,38 +30,66 @@ class CountryTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
private $model;
|
private $model;
|
||||||
|
|
||||||
public function setUp () {
|
public function setUp ()
|
||||||
|
{
|
||||||
$this->model = new Country($this->raw, ['en']);
|
$this->model = new Country($this->raw, ['en']);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testObjects () {
|
public function testObjects ()
|
||||||
$this->assertInstanceOf('GeoIP2\Model\Country', $this->model,
|
{
|
||||||
'minimal GeoIP2::Model::Country object');
|
$this->assertInstanceOf(
|
||||||
$this->assertInstanceOf('GeoIP2\Record\Continent', $this->model->continent);
|
'GeoIP2\Model\Country',
|
||||||
$this->assertInstanceOf('GeoIP2\Record\Country', $this->model->country);
|
$this->model,
|
||||||
$this->assertInstanceOf('GeoIP2\Record\Country', $this->model->registeredCountry);
|
'minimal GeoIP2::Model::Country object'
|
||||||
$this->assertInstanceOf('GeoIP2\Record\RepresentedCountry',
|
);
|
||||||
$this->model->representedCountry);
|
$this->assertInstanceOf(
|
||||||
$this->assertInstanceOf('GeoIP2\Record\Traits', $this->model->traits);
|
'GeoIP2\Record\Continent',
|
||||||
|
$this->model->continent
|
||||||
|
);
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'GeoIP2\Record\Country',
|
||||||
|
$this->model->country
|
||||||
|
);
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'GeoIP2\Record\Country',
|
||||||
|
$this->model->registeredCountry
|
||||||
|
);
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'GeoIP2\Record\RepresentedCountry',
|
||||||
|
$this->model->representedCountry
|
||||||
|
);
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'GeoIP2\Record\Traits',
|
||||||
|
$this->model->traits
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testValues() {
|
public function testValues()
|
||||||
|
{
|
||||||
|
|
||||||
$this->assertEquals(42, $this->model->continent->geonameId,
|
$this->assertEquals(
|
||||||
'continent geoname_id is 42');
|
42,
|
||||||
|
$this->model->continent->geonameId,
|
||||||
|
'continent geoname_id is 42'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals('NA', $this->model->continent->continentCode,
|
$this->assertEquals(
|
||||||
'continent continent_code is NA');
|
'NA',
|
||||||
|
$this->model->continent->continentCode,
|
||||||
|
'continent continent_code is NA'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(array( 'en' => 'North America' ),
|
$this->assertEquals(
|
||||||
|
array( 'en' => 'North America' ),
|
||||||
$this->model->continent->names,
|
$this->model->continent->names,
|
||||||
'continent names');
|
'continent names'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'North America',
|
'North America',
|
||||||
$this->model->continent->name,
|
$this->model->continent->name,
|
||||||
'continent name is North America');
|
'continent name is North America'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
1,
|
1,
|
||||||
|
@ -78,7 +106,7 @@ class CountryTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array( 'en' => 'United States of America' ),
|
array( 'en' => 'United States of America' ),
|
||||||
$this->model->country->names,
|
$this->model->country->names,
|
||||||
'country names'
|
'country name'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
|
@ -93,11 +121,14 @@ class CountryTest extends \PHPUnit_Framework_TestCase
|
||||||
'country confidence is undef'
|
'country confidence is undef'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(2, $this->model->registeredCountry->geonameId,
|
$this->assertEquals(
|
||||||
|
2,
|
||||||
|
$this->model->registeredCountry->geonameId,
|
||||||
'registered_country geoname_id is 2'
|
'registered_country geoname_id is 2'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals('CA',
|
$this->assertEquals(
|
||||||
|
'CA',
|
||||||
$this->model->registeredCountry->isoCode,
|
$this->model->registeredCountry->isoCode,
|
||||||
'registered_country iso_code is CA'
|
'registered_country iso_code is CA'
|
||||||
);
|
);
|
||||||
|
@ -115,8 +146,9 @@ class CountryTest extends \PHPUnit_Framework_TestCase
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach (array( 'isAnonymousProxy', 'isSatelliteProvider' ) as $meth) {
|
foreach (array( 'isAnonymousProxy', 'isSatelliteProvider' ) as $meth) {
|
||||||
|
$this->assertEquals(
|
||||||
$this->assertEquals(0, $this->model->traits->$meth,
|
0,
|
||||||
|
$this->model->traits->$meth,
|
||||||
"traits $meth returns 0 by default"
|
"traits $meth returns 0 by default"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
private function getResponse($ip) {
|
private function getResponse($ip)
|
||||||
|
{
|
||||||
$responses = array(
|
$responses = array(
|
||||||
'1.2.3.4' => $this->response(
|
'1.2.3.4' => $this->response(
|
||||||
'country',
|
'country',
|
||||||
|
@ -43,7 +44,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||||
'1.2.3.5' => $this->response('country', 200),
|
'1.2.3.5' => $this->response('country', 200),
|
||||||
'2.2.3.5' => $this->response('country', 200, 'bad body'),
|
'2.2.3.5' => $this->response('country', 200, 'bad body'),
|
||||||
'1.2.3.6'=> $this->response(
|
'1.2.3.6'=> $this->response(
|
||||||
'error', 400,
|
'error',
|
||||||
|
400,
|
||||||
array(
|
array(
|
||||||
'code' => 'IP_ADDRESS_INVALID',
|
'code' => 'IP_ADDRESS_INVALID',
|
||||||
'error' => 'The value "1.2.3" is not a valid ip address'
|
'error' => 'The value "1.2.3" is not a valid ip address'
|
||||||
|
@ -83,44 +85,71 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||||
return $responses[$ip];
|
return $responses[$ip];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCountry() {
|
public function testCountry()
|
||||||
$country = $this->client($this->getResponse('1.2.3.4'))->country('1.2.3.4' );
|
{
|
||||||
|
$country = $this->client($this->getResponse('1.2.3.4'))
|
||||||
|
->country('1.2.3.4');
|
||||||
|
|
||||||
$this->assertInstanceOf('GeoIP2\Model\Country', $country);
|
$this->assertInstanceOf('GeoIP2\Model\Country', $country);
|
||||||
|
|
||||||
$this->assertEquals(42, $country->continent->geonameId,
|
$this->assertEquals(
|
||||||
'continent geoname_id is 42');
|
42,
|
||||||
|
$country->continent->geonameId,
|
||||||
|
'continent geoname_id is 42'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals('NA', $country->continent->continentCode,
|
$this->assertEquals(
|
||||||
'continent continent_code is NA');
|
'NA',
|
||||||
|
$country->continent->continentCode,
|
||||||
|
'continent continent_code is NA'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(array('en' => 'North America'),
|
$this->assertEquals(
|
||||||
$country->continent->names, 'continent names');
|
array('en' => 'North America'),
|
||||||
|
$country->continent->names,
|
||||||
|
'continent names'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals('North America', $country->continent->name,
|
$this->assertEquals(
|
||||||
'continent name is North America');
|
'North America',
|
||||||
|
$country->continent->name,
|
||||||
|
'continent name is North America'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(1, $country->country->geonameId,
|
$this->assertEquals(
|
||||||
'country geoname_id is 1');
|
1,
|
||||||
|
$country->country->geonameId,
|
||||||
|
'country geoname_id is 1'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals('US', $country->country->isoCode,
|
$this->assertEquals(
|
||||||
'country iso_code is US');
|
'US',
|
||||||
|
$country->country->isoCode,
|
||||||
|
'country iso_code is US'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(array( 'en' => 'United States of America' ),
|
$this->assertEquals(
|
||||||
$country->country->names, 'country names');
|
array( 'en' => 'United States of America' ),
|
||||||
|
$country->country->names,
|
||||||
|
'country names'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals('United States of America',
|
$this->assertEquals(
|
||||||
|
'United States of America',
|
||||||
$country->country->name,
|
$country->country->name,
|
||||||
'country name is United States of America');
|
'country name is United States of America'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMe()
|
public function testMe()
|
||||||
{
|
{
|
||||||
$client = $this->client($this->getResponse('me'));
|
$client = $this->client($this->getResponse('me'));
|
||||||
|
|
||||||
$this->assertInstanceOf('GeoIP2\Model\CityIspOrg',
|
$this->assertInstanceOf(
|
||||||
|
'GeoIP2\Model\CityIspOrg',
|
||||||
$client->cityIspOrg('me'),
|
$client->cityIspOrg('me'),
|
||||||
'can set ip parameter to me');
|
'can set ip parameter to me'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -169,7 +198,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||||
$client = $this->client($this->getResponse('1.2.3.7'));
|
$client = $this->client($this->getResponse('1.2.3.7'));
|
||||||
|
|
||||||
$client->country('1.2.3.7');
|
$client->country('1.2.3.7');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -231,67 +259,86 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||||
public function test406Exception()
|
public function test406Exception()
|
||||||
{
|
{
|
||||||
$client = $this->client($this->getResponse('1.2.3.12'));
|
$client = $this->client($this->getResponse('1.2.3.12'));
|
||||||
|
|
||||||
$client->country('1.2.3.12');
|
$client->country('1.2.3.12');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testParams() {
|
public function testParams()
|
||||||
|
{
|
||||||
$plugin = new MockPlugin();
|
$plugin = new MockPlugin();
|
||||||
$plugin->addResponse($this->getResponse('1.2.3.4'));
|
$plugin->addResponse($this->getResponse('1.2.3.4'));
|
||||||
$guzzleClient = new GuzzleClient();
|
$guzzleClient = new GuzzleClient();
|
||||||
$guzzleClient->addSubscriber($plugin);
|
$guzzleClient->addSubscriber($plugin);
|
||||||
|
|
||||||
$client = new Client(42, 'abcdef123456', array('en'),
|
$client = new Client(
|
||||||
$guzzleClient);
|
42,
|
||||||
|
'abcdef123456',
|
||||||
|
array('en'),
|
||||||
|
$guzzleClient
|
||||||
|
);
|
||||||
$client->country('1.2.3.4');
|
$client->country('1.2.3.4');
|
||||||
|
|
||||||
$request = $plugin->getReceivedRequests()[0];
|
$request = $plugin->getReceivedRequests()[0];
|
||||||
|
|
||||||
$this->assertEquals('https://geoip.maxmind.com/geoip/v2.0/country/1.2.3.4',
|
$this->assertEquals(
|
||||||
|
'https://geoip.maxmind.com/geoip/v2.0/country/1.2.3.4',
|
||||||
$request->getUrl(),
|
$request->getUrl(),
|
||||||
'got expected URI for Country request');
|
'got expected URI for Country request'
|
||||||
$this->assertEquals('GET', $request->getMethod(), 'request is a GET');
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'GET',
|
||||||
|
$request->getMethod(),
|
||||||
|
'request is a GET'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals('application/json', $request->getHeader('Accept'),
|
$this->assertEquals(
|
||||||
'request sets Accept header to application/json');
|
'application/json',
|
||||||
|
$request->getHeader('Accept'),
|
||||||
|
'request sets Accept header to application/json'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertStringMatchesFormat('GeoIP2 PHP API (Guzzle%s)',
|
$this->assertStringMatchesFormat(
|
||||||
|
'GeoIP2 PHP API (Guzzle%s)',
|
||||||
$request->getHeader('User-Agent') . '',
|
$request->getHeader('User-Agent') . '',
|
||||||
'request sets Accept header to application/json');
|
'request sets Accept header to application/json'
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function client($response, $languages=array('en')) {
|
private function client($response, $languages = array('en'))
|
||||||
|
{
|
||||||
$plugin = new MockPlugin();
|
$plugin = new MockPlugin();
|
||||||
$plugin->addResponse($response);
|
$plugin->addResponse($response);
|
||||||
$guzzleClient = new GuzzleClient();
|
$guzzleClient = new GuzzleClient();
|
||||||
$guzzleClient->addSubscriber($plugin);
|
$guzzleClient->addSubscriber($plugin);
|
||||||
|
|
||||||
$client = new Client(42, 'abcdef123456', $languages,
|
$client = new Client(
|
||||||
$guzzleClient);
|
42,
|
||||||
|
'abcdef123456',
|
||||||
|
$languages,
|
||||||
|
$guzzleClient
|
||||||
|
);
|
||||||
|
|
||||||
return $client;
|
return $client;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function response($endpoint, $status, $body=null,
|
private function response(
|
||||||
$bad=null, $contentType=null)
|
$endpoint,
|
||||||
{
|
$status,
|
||||||
$headers = Array();
|
$body = null,
|
||||||
|
$bad = null,
|
||||||
|
$contentType = null
|
||||||
|
) {
|
||||||
|
$headers = array();
|
||||||
if ($contentType) {
|
if ($contentType) {
|
||||||
$headers['Content-Type'] = $contentType;
|
$headers['Content-Type'] = $contentType;
|
||||||
}
|
} elseif ($status == 200 || ( $status >= 400 && $status < 500 )) {
|
||||||
elseif ( $status == 200 || ( $status >= 400 && $status < 500 ) ) {
|
|
||||||
$headers['Content-Type'] = 'application/vnd.maxmind.com-'
|
$headers['Content-Type'] = 'application/vnd.maxmind.com-'
|
||||||
. $endpoint . '+json; charset=UTF-8; version=1.0;';
|
. $endpoint . '+json; charset=UTF-8; version=1.0;';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($bad) {
|
if ($bad) {
|
||||||
$body = '{ invalid: }';
|
$body = '{ invalid: }';
|
||||||
}
|
} elseif (is_array($body)) {
|
||||||
elseif (is_array($body)) {
|
|
||||||
$body = json_encode($body);
|
$body = json_encode($body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,4 +5,3 @@ if (!$loader = @include __DIR__.'/../vendor/autoload.php') {
|
||||||
}
|
}
|
||||||
|
|
||||||
$loader->add('GeoIP2\Test', __DIR__);
|
$loader->add('GeoIP2\Test', __DIR__);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user