Skip to content

Commit

Permalink
Tests for the reader
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Jul 15, 2013
1 parent 3e0fc2a commit 59a51ef
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"guzzle/guzzle": "3.*",
"maxmind-db/reader": "0.*",
"maxmind-db/reader": "dev-master",
"php": ">=5.3.1"
},
"require-dev": {
Expand Down
21 changes: 14 additions & 7 deletions src/GeoIp2/Database/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.
Expand Down Expand Up @@ -53,7 +54,7 @@ public function __construct(
$filename,
$languages = array('en')
) {
$this->dbReader = new MaxMind\Db\Reader($filename);
$this->dbReader = new DbReader($filename);
$this->languages = $languages;
}

Expand All @@ -71,7 +72,7 @@ public function __construct(
*/
public function city($ipAddress)
{
return $this->responseFor('City', $ipAddress);
return $this->modelFor('City', $ipAddress);
}

/**
Expand All @@ -88,7 +89,7 @@ public function city($ipAddress)
*/
public function country($ipAddress)
{
return $this->responseFor('Country', $ipAddress);
return $this->modelFor('Country', $ipAddress);
}

/**
Expand All @@ -105,7 +106,7 @@ public function country($ipAddress)
*/
public function cityIspOrg($ipAddress)
{
return $this->responseFor('CityIspOrg', $ipAddress);
return $this->modelFor('CityIspOrg', $ipAddress);
}

/**
Expand All @@ -122,19 +123,25 @@ public function cityIspOrg($ipAddress)
*/
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();
}
}
2 changes: 1 addition & 1 deletion src/GeoIp2/Exception/AddressNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
/**
* This class represents a generic error.
*/
class AddressNotFoundException extends \GeoIp2Exception
class AddressNotFoundException extends GeoIp2Exception
{
}
46 changes: 46 additions & 0 deletions tests/GeoIp2/Test/Database/ReaderTest.php
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 59a51ef

Please sign in to comment.