Skip to content

Commit 75da612

Browse files
committed
Minor tweaks
1 parent b265fa2 commit 75da612

File tree

2 files changed

+36
-41
lines changed

2 files changed

+36
-41
lines changed

src/Geocoder/Provider/MaxMindBinaryProvider.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MaxMindBinaryProvider extends AbstractProvider implements ProviderInterfac
3131
/**
3232
* @param string $datFile
3333
* @param int|null $openFlag
34-
*
34+
*
3535
* @throws RuntimeException if maxmind's lib not installed.
3636
* @throws InvalidArgumentException if dat file is not correct.
3737
*/
@@ -40,18 +40,20 @@ public function __construct($datFile, $openFlag = null)
4040
if (false === function_exists('geoip_open')) {
4141
throw new RuntimeException('The MaxMindBinaryProvider requires maxmind\'s lib to be installed and loaded. Have you included geoip.inc file?');
4242
}
43+
4344
if (false === function_exists('GeoIP_record_by_addr')) {
4445
throw new RuntimeException('The MaxMindBinaryProvider requires maxmind\'s lib to be installed and loaded. Have you included geoipcity.inc file?');
4546
}
46-
47+
4748
if (false === is_file($datFile)) {
48-
throw new InvalidArgumentException(sprintf('Given MaxMind dat file %s is not exist.', $this->datFile));
49+
throw new InvalidArgumentException(sprintf('Given MaxMind dat file "%s" does not exist.', $this->datFile));
4950
}
51+
5052
if (false === is_readable($datFile)) {
51-
throw new InvalidArgumentException(sprintf('Given MaxMind dat file %s is not readable.', $this->datFile));
53+
throw new InvalidArgumentException(sprintf('Given MaxMind dat file "%s" does not readable.', $this->datFile));
5254
}
53-
$this->datFile = $datFile;
54-
55+
56+
$this->datFile = $datFile;
5557
$this->openFlag = null === $openFlag ? GEOIP_STANDARD : $openFlag;
5658
}
5759

@@ -64,23 +66,22 @@ public function getGeocodedData($address)
6466
throw new UnsupportedException('The MaxMindBinaryProvider does not support street addresses.');
6567
}
6668

67-
$geoIp = geoip_open($this->datFile, $this->openFlag);
68-
69+
$geoIp = geoip_open($this->datFile, $this->openFlag);
6970
$geoIpRecord = GeoIP_record_by_addr($geoIp, $address);
7071

7172
geoip_close($geoIp);
72-
73+
7374
if (false === $geoIpRecord instanceof \geoiprecord) {
7475
throw new NoResultException(sprintf('No results found for IP address %s', $address));
7576
}
76-
77+
7778
return array_merge($this->getDefaults(), array(
7879
'countryCode' => $geoIpRecord->country_code,
79-
'country' => $geoIpRecord->country_name,
80-
'region' => $geoIpRecord->region,
81-
'city' => $geoIpRecord->city,
82-
'latitude' => $geoIpRecord->latitude,
83-
'longitude' => $geoIpRecord->longitude,
80+
'country' => $geoIpRecord->country_name,
81+
'region' => $geoIpRecord->region,
82+
'city' => $geoIpRecord->city,
83+
'latitude' => $geoIpRecord->latitude,
84+
'longitude' => $geoIpRecord->longitude,
8485
));
8586
}
8687

@@ -99,4 +100,4 @@ public function getName()
99100
{
100101
return 'maxmind_binary';
101102
}
102-
}
103+
}

tests/Geocoder/Tests/Provider/MaxMindBinaryProviderTest.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
<?php
22
namespace Geocoder\Tests\Provider;
33

4+
use Geocoder\Tests\TestCase;
45
use Geocoder\Provider\MaxMindBinaryProvider;
56

6-
class MaxMindBinaryProviderTest extends \PHPUnit_Framework_TestCase
7+
class MaxMindBinaryProviderTest extends TestCase
78
{
9+
private $binaryFile;
10+
11+
public function setUp()
12+
{
13+
$this->binaryFile = __DIR__ . '/fixtures/GeoLiteCity.dat';
14+
}
15+
816
public static function setUpBeforeClass()
917
{
1018
if (false == function_exists('geoip_open')) {
1119
throw new \PHPUnit_Framework_SkippedTestError('The maxmind\'s official lib required to run these tests.');
1220
}
21+
1322
if (false == function_exists('GeoIP_record_by_addr')) {
1423
throw new \PHPUnit_Framework_SkippedTestError('The maxmind\'s official lib required to run these tests.');
1524
}
@@ -37,11 +46,8 @@ public function testThrowIfNotExistBinaryFileGiven()
3746
*/
3847
public function testLocationResultContainsExpectedFields($ip)
3948
{
40-
$binaryFile = __DIR__.'/fixtures/GeoLiteCity.dat';
41-
42-
$provider = new MaxMindBinaryProvider($binaryFile);
43-
44-
$result = $provider->getGeocodedData($ip);
49+
$provider = new MaxMindBinaryProvider($this->binaryFile);
50+
$result = $provider->getGeocodedData($ip);
4551

4652
$this->assertInternalType('array', $result);
4753

@@ -67,11 +73,8 @@ public function testLocationResultContainsExpectedFields($ip)
6773
*/
6874
public function testFindLocationByIp($ip, $expectedCity, $expectedCountry)
6975
{
70-
$binaryFile = __DIR__.'/fixtures/GeoLiteCity.dat';
71-
72-
$provider = new MaxMindBinaryProvider($binaryFile);
73-
74-
$result = $provider->getGeocodedData($ip);
76+
$provider = new MaxMindBinaryProvider($this->binaryFile);
77+
$result = $provider->getGeocodedData($ip);
7578

7679
$this->assertInternalType('array', $result);
7780

@@ -84,9 +87,7 @@ public function testFindLocationByIp($ip, $expectedCity, $expectedCountry)
8487

8588
public function testGetName()
8689
{
87-
$binaryFile = __DIR__.'/fixtures/GeoLiteCity.dat';
88-
89-
$provider = new MaxMindBinaryProvider($binaryFile);
90+
$provider = new MaxMindBinaryProvider($this->binaryFile);
9091

9192
$this->assertEquals('maxmind_binary', $provider->getName());
9293
}
@@ -97,9 +98,7 @@ public function testGetName()
9798
*/
9899
public function testThrowIfIpAddressCouldNotBeLocated()
99100
{
100-
$binaryFile = __DIR__.'/fixtures/GeoLiteCity.dat';
101-
102-
$provider = new MaxMindBinaryProvider($binaryFile);
101+
$provider = new MaxMindBinaryProvider($this->binaryFile);
103102

104103
$provider->getGeocodedData('127.0.0.1');
105104
}
@@ -110,9 +109,7 @@ public function testThrowIfIpAddressCouldNotBeLocated()
110109
*/
111110
public function testThrowIfInvalidIpAddressGiven()
112111
{
113-
$binaryFile = __DIR__.'/fixtures/GeoLiteCity.dat';
114-
115-
$provider = new MaxMindBinaryProvider($binaryFile);
112+
$provider = new MaxMindBinaryProvider($this->binaryFile);
116113

117114
$provider->getGeocodedData('invalidIp');
118115
}
@@ -123,11 +120,8 @@ public function testThrowIfInvalidIpAddressGiven()
123120
*/
124121
public function testThrowOnReversedDataMethodUsage()
125122
{
126-
$binaryFile = __DIR__.'/fixtures/GeoLiteCity.dat';
127-
128-
$provider = new MaxMindBinaryProvider($binaryFile);
123+
$provider = new MaxMindBinaryProvider($this->binaryFile);
129124

130125
$provider->getReversedData(array());
131126
}
132-
133-
}
127+
}

0 commit comments

Comments
 (0)