Skip to content

Commit d46ca18

Browse files
author
Jens Wiese
committed
Change access of GeoIP2 database-reader, due to problems with JSON-encoded geodata.
1 parent 40a7b4a commit d46ca18

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ It requires the [database file](http://dev.maxmind.com/geoip/geoip2/geolite2/),
270270

271271
This provider will only work with the corresponding `GeoIP2DatabaseAdapter`.
272272

273+
**Usage:**
274+
275+
$adapter = new \Geocoder\HttpAdapter\GeoIP2DatabaseAdapter('/path/to/database');
276+
$provider = new \Geocoder\Provider\GeoIP2DatabaseProvider($adapter);
277+
$geocoder = new \Geocoder\Geocoder($provider);
278+
279+
$result = $geocoder->geocode('74.200.247.59');
280+
273281
### GeonamesProvider ###
274282

275283
The `GeonamesProvider` named `geonames` is able to geocode and reverse geocode **places**.

src/Geocoder/HttpAdapter/GeoIP2DatabaseAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function getContent($url)
131131

132132
switch ($this->dbType) {
133133
case self::GEOIP2_CITY:
134-
$result = $this->getDbReader()->city($ipAddress);
134+
$result = $this->getDbReader()->city($ipAddress)->jsonSerialize();
135135
break;
136136
default:
137137
throw new UnsupportedException(
@@ -165,4 +165,4 @@ protected function getDbReader()
165165

166166
return $this->dbReader;
167167
}
168-
}
168+
}

tests/Geocoder/Tests/HttpAdapter/GeoIP2DatabaseAdapterTest.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Geocoder\Tests\TestCase;
1515
use Geocoder\Exception\RuntimeException;
1616
use GeoIp2\Database\Reader;
17-
use GeoIp2\Model\City;
1817
use org\bovigo\vfs\vfsStream;
1918
use org\bovigo\vfs\vfsStreamFile;
2019

@@ -94,12 +93,15 @@ public function testAddressPassedToReaderMustBeIpAddress()
9493

9594
public function testIpAddressIsPassedCorrectToReader()
9695
{
97-
$url = 'file://database?127.0.0.1 ';
9896
$dbReader = $this->getDbReaderMock();
99-
$dbReader->expects($this->once())->method('city')->with('127.0.0.1');
97+
$dbReader
98+
->expects($this->once())
99+
->method('city')->with('127.0.0.1')
100+
->will($this->returnValue($this->getCityModelMock()));
101+
100102
$this->adapter->setDbReader($dbReader);
101103

102-
$this->adapter->getContent($url);
104+
$this->adapter->getContent('file://database?127.0.0.1');
103105
}
104106

105107
public function testSettingLocaleIsCorrect()
@@ -125,10 +127,14 @@ public function testUsingNonExistantDatabaseTypesLeadsToException()
125127

126128
public function testReaderResponseIsJsonEncoded()
127129
{
128-
$city = new City(array('city' => 'Hamburg'));
130+
$cityModel = $this->getCityModelMock();
129131

130132
$dbReader = $this->getDbReaderMock();
131-
$dbReader->expects($this->once())->method('city')->will($this->returnValue($city));
133+
$dbReader
134+
->expects($this->any())
135+
->method('city')
136+
->will($this->returnValue($cityModel));
137+
132138
$this->adapter->setDbReader($dbReader);
133139

134140
$result = $this->adapter->getContent('file://database?127.0.0.1');
@@ -148,6 +154,36 @@ protected function getDbReaderMock()
148154
return $mock;
149155
}
150156

157+
/**
158+
* @return \PHPUnit_Framework_MockObject_MockObject
159+
*/
160+
protected function getCityModelMock()
161+
{
162+
$mock = $this->getMockBuilder('\GeoIp2\Model\City')->disableOriginalConstructor()->getMock();
163+
$mock
164+
->expects($this->any())
165+
->method('jsonSerialize')
166+
->will($this->returnValue(
167+
array(
168+
'city' => array(
169+
'geoname_id' => 2911298,
170+
'names' => array(
171+
'de' => 'Hamburg',
172+
'en' => 'Hamburg',
173+
'es' => 'Hamburgo',
174+
'fr' => 'Hambourg',
175+
'ja' => 'ハンブルク',
176+
'pt-BR' => 'Hamburgo',
177+
'ru' => 'Гамбург',
178+
'zh-CN' => '汉堡市',
179+
)
180+
)
181+
)
182+
));
183+
184+
return $mock;
185+
}
186+
151187
/**
152188
* Returns virtual db-file
153189
*

0 commit comments

Comments
 (0)