Skip to content

Commit 40a9eb9

Browse files
committed
fix MaxMindBinary provider tests and doc
1 parent c9f4243 commit 40a9eb9

File tree

4 files changed

+72
-16
lines changed

4 files changed

+72
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Provider | Name | IPv4? | IPv6? | Terms | Notes
4646
[IpInfoDB](http://ipinfodb.com/) | `ip_info_db` | yes | no | requires API key. | city precision
4747
Geoip | `geoip` | | | | wrapper around the [PHP extension](http://php.net/manual/en/book.geoip.php) which must be installed
4848
[MaxMind](https://www.maxmind.com/) web service | `maxmind` | yes | yes | requires Omni API key | City/ISP/Org and Omni services, IPv6 on country level
49-
MaxMind Binary file | `maxmind_binary` | yes | yes | | needs locally installed database files
49+
MaxMind Binary file | `maxmind_binary` | yes | no | | needs locally installed database files
5050

5151
**Important:** the [Geocoder
5252
Extra](https://github.com/geocoder-php/geocoder-extra) library contains even

src/Geocoder/Provider/AbstractProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,16 @@ protected function returnResults(array $data = [])
111111

112112
return $this->factory->createFromArray($data);
113113
}
114+
115+
/**
116+
* @param array $results
117+
*
118+
* @return array
119+
*/
120+
protected function fixEncoding(array $results)
121+
{
122+
return array_map(function ($value) {
123+
return is_string($value) ? utf8_encode($value) : $value;
124+
}, $results);
125+
}
114126
}

src/Geocoder/Provider/MaxMindBinary.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public function __construct($datFile, $openFlag = null)
6060

6161
$this->datFile = $datFile;
6262
$this->openFlag = null === $openFlag ? GEOIP_STANDARD : $openFlag;
63+
64+
parent::__construct();
6365
}
6466

6567
/**
@@ -68,7 +70,12 @@ public function __construct($datFile, $openFlag = null)
6870
public function geocode($address)
6971
{
7072
if (false === filter_var($address, FILTER_VALIDATE_IP)) {
71-
throw new UnsupportedOperation('The MaxMindBinary does not support street addresses.');
73+
throw new UnsupportedOperation('The MaxMindBinary provider does not support street addresses.');
74+
}
75+
76+
// This API does not support IPv6
77+
if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
78+
throw new UnsupportedOperation('The MaxMindBinary provider does not support IPv6 addresses.');
7279
}
7380

7481
$geoIp = geoip_open($this->datFile, $this->openFlag);
@@ -81,14 +88,14 @@ public function geocode($address)
8188
}
8289

8390
return $this->returnResults([
84-
array_merge($this->getDefaults(), [
91+
$this->fixEncoding(array_merge($this->getDefaults(), [
8592
'countryCode' => $geoIpRecord->country_code,
8693
'country' => $geoIpRecord->country_name,
8794
'region' => $geoIpRecord->region,
8895
'locality' => $geoIpRecord->city,
8996
'latitude' => $geoIpRecord->latitude,
9097
'longitude' => $geoIpRecord->longitude,
91-
])
98+
]))
9299
]);
93100
}
94101

tests/Geocoder/Tests/Provider/MaxMindBinaryTest.php

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,10 @@ public function testThrowIfNotExistBinaryFileGiven()
4141
new MaxMindBinary('not_exist.dat');
4242
}
4343

44-
/**
45-
* @dataProvider provideIps
46-
*/
47-
public function testLocationResultContainsExpectedFields($ip)
44+
public function testLocationResultContainsExpectedFieldsForAnAmericanIp()
4845
{
4946
$provider = new MaxMindBinary($this->binaryFile);
50-
$results = $provider->geocode($ip);
47+
$results = $provider->geocode('24.24.24.24');
5148

5249
$this->assertInternalType('array', $results);
5350
$this->assertCount(1, $results);
@@ -56,20 +53,49 @@ public function testLocationResultContainsExpectedFields($ip)
5653
$result = $results[0];
5754
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
5855

59-
$this->assertNull($result->getLatitude());
60-
$this->assertNull($result->getLongitude());
56+
$this->assertEquals('43.089200000000005', $result->getLatitude(), '', 0.001);
57+
$this->assertEquals('-76.025000000000006', $result->getLongitude(), '', 0.001);
6158
$this->assertFalse($result->getBounds()->isDefined());
6259
$this->assertNull($result->getStreetNumber());
6360
$this->assertNull($result->getStreetName());
6461
$this->assertNull($result->getPostalCode());
65-
$this->assertNotNull($result->getLocality());
62+
$this->assertEquals('East Syracuse', $result->getLocality());
6663
$this->assertNull($result->getSubLocality());
6764
$this->assertNull($result->getCounty()->getName());
6865
$this->assertNull($result->getCounty()->getCode());
69-
$this->assertNull($result->getRegion()->getName());
66+
$this->assertEquals('NY', $result->getRegion()->getName());
7067
$this->assertNull($result->getRegion()->getCode());
71-
$this->assertNotNull($result->getCountry()->getName());
72-
$this->assertNull($result->getCountry()->getCode());
68+
$this->assertEquals('United States', $result->getCountry()->getName());
69+
$this->assertEquals('US', $result->getCountry()->getCode());
70+
$this->assertNull($result->getTimezone());
71+
}
72+
73+
public function testLocationResultContainsExpectedFieldsForASpanishIp()
74+
{
75+
$provider = new MaxMindBinary($this->binaryFile);
76+
$results = $provider->geocode('80.24.24.24');
77+
78+
$this->assertInternalType('array', $results);
79+
$this->assertCount(1, $results);
80+
81+
/** @var \Geocoder\Model\Address $result */
82+
$result = $results[0];
83+
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
84+
85+
$this->assertEquals('41.543299999999988', $result->getLatitude(), '', 0.001);
86+
$this->assertEquals('2.1093999999999937', $result->getLongitude(), '', 0.001);
87+
$this->assertFalse($result->getBounds()->isDefined());
88+
$this->assertNull($result->getStreetNumber());
89+
$this->assertNull($result->getStreetName());
90+
$this->assertNull($result->getPostalCode());
91+
$this->assertEquals('Sabadell', $result->getLocality());
92+
$this->assertNull($result->getSubLocality());
93+
$this->assertNull($result->getCounty()->getName());
94+
$this->assertNull($result->getCounty()->getCode());
95+
$this->assertEquals('56', $result->getRegion()->getName());
96+
$this->assertNull($result->getRegion()->getCode());
97+
$this->assertEquals('Spain', $result->getCountry()->getName());
98+
$this->assertEquals('ES', $result->getCountry()->getCode());
7399
$this->assertNull($result->getTimezone());
74100
}
75101

@@ -122,7 +148,18 @@ public function testThrowIfIpAddressCouldNotBeLocated()
122148

123149
/**
124150
* @expectedException \Geocoder\Exception\UnsupportedOperation
125-
* @expectedExceptionMessage The MaxMindBinary does not support street addresses.
151+
* @expectedExceptionMessage The MaxMindBinary provider does not support IPv6 addresses.
152+
*/
153+
public function testThrowIfIpAddressIsNotIpV4()
154+
{
155+
$provider = new MaxMindBinary($this->binaryFile);
156+
157+
$provider->geocode('2002:5018:1818:0:0:0:0:0');
158+
}
159+
160+
/**
161+
* @expectedException \Geocoder\Exception\UnsupportedOperation
162+
* @expectedExceptionMessage The MaxMindBinary provider does not support street addresses.
126163
*/
127164
public function testThrowIfInvalidIpAddressGiven()
128165
{

0 commit comments

Comments
 (0)