Skip to content

Commit

Permalink
Add concrete assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Feb 10, 2015
1 parent 11c9b33 commit cebdcca
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
47 changes: 37 additions & 10 deletions src/main/php/com/maxmind/geoip/Location.class.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
<?php namespace com\maxmind\geoip;

use util\TimeZone;
use util\Objects;

class Location extends \lang\Object {
private $map;
private $lat, $long, $attr;
public static $UNKNOWN;

static function __static() {
self::$UNKNOWN= newinstance(__CLASS__, [[]], '{
self::$UNKNOWN= newinstance(__CLASS__, [0.0, 0.0, []], '{
static function __static() { }
public function toString() { return "com.maxmind.geoip.Location(UNKNOWN)"; }
}');
}

/** @param [:var] $map */
public function __construct($map) { $this->map= $map; }
/**
* Creates a new location
*
* @param double $lat Latitude
* @param double $long Longitude
* @param [:var] $attr Further attributes
*/
public function __construct($lat, $long, $attr) {
unset($attr['latitude'], $attr['longitude']);

$this->lat= $lat;
$this->long= $long;
$this->attr= $attr;
}

/** @return double */
public function latitude() { return $this->map['latitude']; }
public function latitude() { return $this->lat; }

/** @return double */
public function longitude() { return $this->map['longitude']; }
public function longitude() { return $this->long; }

/** @return util.TimeZone */
public function timeZone() { return isset($this->map['time_zone']) ? new TimeZone($this->map['time_zone']) : null; }
public function timeZone() { return isset($this->attr['time_zone']) ? new TimeZone($this->attr['time_zone']) : null; }

/**
* Gets a specific attribute, or NULL if the attribute does not exist
Expand All @@ -32,7 +45,7 @@ public function timeZone() { return isset($this->map['time_zone']) ? new TimeZon
* @return string
*/
public function attribute($name) {
return isset($this->map[$name]) ? $this->map[$name] : null;
return isset($this->attr[$name]) ? $this->attr[$name] : null;
}

/**
Expand All @@ -41,7 +54,21 @@ public function attribute($name) {
* @return string
*/
public function toString() {
$tz= isset($this->map['time_zone']) ? '; tz= '.$this->map['time_zone'] : '';
return $this->getClassName().'('.$this->map['latitude'].','.$this->map['longitude'].$tz.')';
$tz= isset($this->attr['time_zone']) ? '; tz= '.$this->attr['time_zone'] : '';
return $this->getClassName().'('.$this->lat.','.$this->long.$tz.')';
}

/**
* Test whether a given value is equal to this location instance.
*
* @param var $cmp
* @return bool
*/
public function equals($cmp) {
return $cmp instanceof self && (
abs($this->lat - $cmp->lat) < 0.00001 &&
abs($this->long - $cmp->long) < 0.00001 &&
Objects::equal($this->attr, $cmp->attr)
);
}
}
2 changes: 1 addition & 1 deletion src/main/php/com/maxmind/geoip/Record.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function continent() {
/** @return com.maxmind.geoip.Location */
public function location() {
return isset($this->map['location'])
? new Location($this->map['location'])
? new Location($this->map['location']['latitude'], $this->map['location']['longitude'], $this->map['location'])
: Location::$UNKNOWN
;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace com\maxmind\geoip\unittest;

use com\maxmind\geoip\GeoIpDatabase;
use com\maxmind\geoip\Location;
use lang\ClassLoader;
use lang\IllegalArgumentException;

Expand All @@ -26,10 +27,22 @@ public function lookup_v4_localhost() {
$this->assertEquals(null, $reader->lookup('127.0.0.1'));
}

#[@test, @values(['89.160.20.128', '216.160.83.56'])]
public function lookup_v4($addr) {
$reader= GeoIpDatabase::open($this->fixture);
$this->assertNotEquals(null, $reader->lookup($addr));
#[@test]
public function lookup_89_160_20_128_slash_121() {
$record= GeoIpDatabase::open($this->fixture)->lookup('89.160.20.128');
$this->assertEquals(
['Linköping', 'Sweden', new Location(58.4167, 15.6167, ['time_zone' => 'Europe/Stockholm'])],
[$record->city()->name(), $record->country()->name(), $record->location()]
);
}

#[@test]
public function lookup_216_160_83_56_slash_125() {
$record= GeoIpDatabase::open($this->fixture)->lookup('216.160.83.56');
$this->assertEquals(
['Milton', 'United States', new Location(47.2513, -122.3149, ['time_zone' => 'America/Los_Angeles', 'metro_code' => 819])],
[$record->city()->name(), $record->country()->name(), $record->location()]
);
}

#[@test]
Expand All @@ -38,10 +51,22 @@ public function lookup_v6_localhost() {
$this->assertEquals(null, $reader->lookup('::1'));
}

#[@test, @values(['2001:256::', '2a02:da80::'])]
public function lookup_v6($addr) {
$reader= GeoIpDatabase::open($this->fixture);
$this->assertNotEquals(null, $reader->lookup($addr));
#[@test]
public function lookup_2001_256_slash_32() {
$record= GeoIpDatabase::open($this->fixture)->lookup('2001:256::');
$this->assertEquals(
[null, "People's Republic of China", new Location(35, 105, [])],
[$record->city()->name(), $record->country()->name(), $record->location()]
);
}

#[@test]
public function lookup_2a02_da80_slash_29() {
$record= GeoIpDatabase::open($this->fixture)->lookup('2a02:da80::');
$this->assertEquals(
[null, 'Austria', new Location(47.33333, 13.33333, ['time_zone' => 'Europe/Vienna'])],
[$record->city()->name(), $record->country()->name(), $record->location()]
);
}

#[@test, @expect(IllegalArgumentException::class), @values([null, '', 'not.an.ip', '::not-v6'])]
Expand Down

0 comments on commit cebdcca

Please sign in to comment.