2013-07-15 22:52:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace GeoIp2\Test\WebService;
|
|
|
|
|
|
|
|
use GeoIp2\Database\Reader;
|
|
|
|
|
|
|
|
class ReaderTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2013-10-17 19:58:21 +00:00
|
|
|
public function testDefaultLocale()
|
2013-07-15 22:52:07 +00:00
|
|
|
{
|
2013-07-15 23:10:05 +00:00
|
|
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
2013-07-16 17:11:06 +00:00
|
|
|
// Needed for PHP 5.3
|
|
|
|
$that = $this;
|
2013-07-16 16:44:30 +00:00
|
|
|
$this->checkAllMethods(
|
2013-07-16 17:11:06 +00:00
|
|
|
function ($method) use (&$that, &$reader) {
|
2013-07-16 16:44:30 +00:00
|
|
|
$record = $reader->$method('81.2.69.160');
|
2013-07-16 17:11:06 +00:00
|
|
|
$that->assertEquals('United Kingdom', $record->country->name);
|
2013-07-16 16:44:30 +00:00
|
|
|
}
|
|
|
|
);
|
2013-07-15 22:52:07 +00:00
|
|
|
$reader->close();
|
|
|
|
}
|
|
|
|
|
2013-10-17 19:58:21 +00:00
|
|
|
public function testLocaleList()
|
2013-07-15 22:52:07 +00:00
|
|
|
{
|
|
|
|
$reader = new Reader(
|
2013-07-15 23:10:05 +00:00
|
|
|
'maxmind-db/test-data/GeoIP2-City-Test.mmdb',
|
2013-07-15 22:52:07 +00:00
|
|
|
array('xx', 'ru', 'pt-BR', 'es', 'en')
|
|
|
|
);
|
2013-07-16 17:11:06 +00:00
|
|
|
$that = $this;
|
2013-07-16 16:44:30 +00:00
|
|
|
$this->checkAllMethods(
|
2013-07-16 17:11:06 +00:00
|
|
|
function ($method) use (&$that, &$reader) {
|
2013-07-16 16:44:30 +00:00
|
|
|
$record = $reader->$method('81.2.69.160');
|
2013-07-16 17:11:06 +00:00
|
|
|
$that->assertEquals('Великобритания', $record->country->name);
|
2013-07-16 16:44:30 +00:00
|
|
|
}
|
|
|
|
);
|
2013-07-15 22:52:07 +00:00
|
|
|
$reader->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasIpAddress()
|
|
|
|
{
|
2013-07-15 23:10:05 +00:00
|
|
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
2013-07-16 17:11:06 +00:00
|
|
|
$that = $this;
|
2013-07-16 16:44:30 +00:00
|
|
|
$this->checkAllMethods(
|
2013-07-16 17:11:06 +00:00
|
|
|
function ($method) use (&$that, &$reader) {
|
2013-07-16 16:44:30 +00:00
|
|
|
$record = $reader->$method('81.2.69.160');
|
2013-07-16 17:11:06 +00:00
|
|
|
$that->assertEquals('81.2.69.160', $record->traits->ipAddress);
|
2013-07-16 16:44:30 +00:00
|
|
|
}
|
|
|
|
);
|
2013-07-15 22:52:07 +00:00
|
|
|
$reader->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException GeoIp2\Exception\AddressNotFoundException
|
|
|
|
* @expectedExceptionMessage The address 10.10.10.10 is not in the database.
|
|
|
|
*/
|
|
|
|
public function testUnknownAddress()
|
|
|
|
{
|
2013-07-15 23:10:05 +00:00
|
|
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
2013-07-15 22:52:07 +00:00
|
|
|
$reader->city('10.10.10.10');
|
|
|
|
$reader->close();
|
|
|
|
}
|
2013-07-15 23:05:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
2013-10-21 18:09:27 +00:00
|
|
|
* @expectedExceptionMessage is not a valid IP address
|
2013-07-15 23:05:08 +00:00
|
|
|
*/
|
|
|
|
public function testInvalidAddress()
|
|
|
|
{
|
2013-07-15 23:10:05 +00:00
|
|
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
2013-07-15 23:05:08 +00:00
|
|
|
$reader->city('invalid');
|
|
|
|
$reader->close();
|
|
|
|
}
|
2013-07-16 16:44:30 +00:00
|
|
|
|
2014-06-17 16:29:14 +00:00
|
|
|
public function testConnectionType()
|
|
|
|
{
|
|
|
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-Connection-Type-Test.mmdb');
|
|
|
|
$ipAddress = '1.0.1.0';
|
|
|
|
|
|
|
|
$record = $reader->connectionType($ipAddress);
|
|
|
|
$this->assertEquals('Cable/DSL', $record->connectionType);
|
|
|
|
$this->assertEquals($ipAddress, $record->ipAddress);
|
|
|
|
$reader->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDomain()
|
|
|
|
{
|
|
|
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-Domain-Test.mmdb');
|
|
|
|
|
|
|
|
$ipAddress = '1.2.0.0';
|
|
|
|
$record = $reader->domain($ipAddress);
|
|
|
|
$this->assertEquals('maxmind.com', $record->domain);
|
|
|
|
$this->assertEquals($ipAddress, $record->ipAddress);
|
|
|
|
$reader->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsp()
|
|
|
|
{
|
2014-06-18 17:29:54 +00:00
|
|
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-ISP-Test.mmdb');
|
2014-06-17 16:29:14 +00:00
|
|
|
|
2014-06-18 17:29:54 +00:00
|
|
|
$ipAddress = '1.128.0.0';
|
2014-06-17 16:29:14 +00:00
|
|
|
$record = $reader->isp($ipAddress);
|
2014-06-18 17:29:54 +00:00
|
|
|
$this->assertEquals(1221, $record->autonomousSystemNumber);
|
2014-06-17 16:29:14 +00:00
|
|
|
$this->assertEquals(
|
2014-06-18 17:29:54 +00:00
|
|
|
'Telstra Pty Ltd',
|
2014-06-17 16:29:14 +00:00
|
|
|
$record->autonomousSystemOrganization
|
|
|
|
);
|
|
|
|
|
2014-06-18 17:29:54 +00:00
|
|
|
$this->assertEquals('Telstra Internet', $record->isp);
|
|
|
|
$this->assertEquals('Telstra Internet', $record->organization);
|
2014-06-17 16:29:14 +00:00
|
|
|
|
|
|
|
$this->assertEquals($ipAddress, $record->ipAddress);
|
|
|
|
$reader->close();
|
|
|
|
}
|
|
|
|
|
2013-07-16 16:44:30 +00:00
|
|
|
public function checkAllMethods($testCb)
|
|
|
|
{
|
|
|
|
foreach (array('city', 'cityIspOrg', 'country', 'omni') as $method) {
|
2013-07-16 17:34:44 +00:00
|
|
|
$testCb($method);
|
2013-07-16 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-15 22:52:07 +00:00
|
|
|
}
|