Tests for the reader
This commit is contained in:
parent
3e0fc2a837
commit
59a51ef34d
|
@ -14,7 +14,7 @@
|
|||
],
|
||||
"require": {
|
||||
"guzzle/guzzle": "3.*",
|
||||
"maxmind-db/reader": "0.*",
|
||||
"maxmind-db/reader": "dev-master",
|
||||
"php": ">=5.3.1"
|
||||
},
|
||||
"require-dev": {
|
||||
|
|
|
@ -7,6 +7,7 @@ use GeoIp2\Model\City;
|
|||
use GeoIp2\Model\CityIspOrg;
|
||||
use GeoIp2\Model\Country;
|
||||
use GeoIp2\Model\Omni;
|
||||
use MaxMind\Db\Reader as DbReader;
|
||||
|
||||
/**
|
||||
* Instances of this class provide a reader for the GeoIP2 database format.
|
||||
|
@ -53,7 +54,7 @@ class Reader
|
|||
$filename,
|
||||
$languages = array('en')
|
||||
) {
|
||||
$this->dbReader = new MaxMind\Db\Reader($filename);
|
||||
$this->dbReader = new DbReader($filename);
|
||||
$this->languages = $languages;
|
||||
}
|
||||
|
||||
|
@ -71,7 +72,7 @@ class Reader
|
|||
*/
|
||||
public function city($ipAddress)
|
||||
{
|
||||
return $this->responseFor('City', $ipAddress);
|
||||
return $this->modelFor('City', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -88,7 +89,7 @@ class Reader
|
|||
*/
|
||||
public function country($ipAddress)
|
||||
{
|
||||
return $this->responseFor('Country', $ipAddress);
|
||||
return $this->modelFor('Country', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -105,7 +106,7 @@ class Reader
|
|||
*/
|
||||
public function cityIspOrg($ipAddress)
|
||||
{
|
||||
return $this->responseFor('CityIspOrg', $ipAddress);
|
||||
return $this->modelFor('CityIspOrg', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -122,19 +123,25 @@ class Reader
|
|||
*/
|
||||
public function omni($ipAddress)
|
||||
{
|
||||
return $this->responseFor('Omni', $ipAddress);
|
||||
return $this->modelFor('Omni', $ipAddress);
|
||||
}
|
||||
|
||||
private function responseFor($class, $ipAddress)
|
||||
private function modelFor($class, $ipAddress)
|
||||
{
|
||||
$record = $this->dbReader->get($ipAddress);
|
||||
if ($record === null) {
|
||||
throw new AddressNotFoundException(
|
||||
"$ipAddress was not found in the database."
|
||||
"The address $ipAddress is not in the database."
|
||||
);
|
||||
}
|
||||
$record['traits']['ip_address'] = $ipAddress;
|
||||
$class = "GeoIp2\\Model\\" . $class;
|
||||
|
||||
return new $class($record, $this->languages);
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
$this->dbReader->close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,6 @@ namespace GeoIp2\Exception;
|
|||
/**
|
||||
* This class represents a generic error.
|
||||
*/
|
||||
class AddressNotFoundException extends \GeoIp2Exception
|
||||
class AddressNotFoundException extends GeoIp2Exception
|
||||
{
|
||||
}
|
||||
|
|
46
tests/GeoIp2/Test/Database/ReaderTest.php
Normal file
46
tests/GeoIp2/Test/Database/ReaderTest.php
Normal file
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace GeoIp2\Test\WebService;
|
||||
|
||||
use GeoIp2\Database\Reader;
|
||||
|
||||
class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testDefaultLanguage()
|
||||
{
|
||||
$reader = new Reader('maxmind-db/test-data/GeoIP2-City.mmdb');
|
||||
$city = $reader->city('81.2.69.160');
|
||||
$this->assertEquals('London', $city->city->name);
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
public function testLanguageList()
|
||||
{
|
||||
$reader = new Reader(
|
||||
'maxmind-db/test-data/GeoIP2-City.mmdb',
|
||||
array('xx', 'ru', 'pt-BR', 'es', 'en')
|
||||
);
|
||||
$omni = $reader->omni('81.2.69.160');
|
||||
$this->assertEquals('Лондон', $omni->city->name);
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
public function testHasIpAddress()
|
||||
{
|
||||
$reader = new Reader('maxmind-db/test-data/GeoIP2-City.mmdb');
|
||||
$cio = $reader->cityIspOrg('81.2.69.160');
|
||||
$this->assertEquals('81.2.69.160', $cio->traits->ipAddress);
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException GeoIp2\Exception\AddressNotFoundException
|
||||
* @expectedExceptionMessage The address 10.10.10.10 is not in the database.
|
||||
*/
|
||||
public function testUnknownAddress()
|
||||
{
|
||||
$reader = new Reader('maxmind-db/test-data/GeoIP2-City.mmdb');
|
||||
$reader->city('10.10.10.10');
|
||||
$reader->close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user