1+ <?php
2+ namespace Geocoder \Tests \Provider ;
3+
4+ use Geocoder \Provider \MaxMindBinaryProvider ;
5+
6+ class MaxMindBinaryProviderTest extends \PHPUnit_Framework_TestCase
7+ {
8+ public static function setUpBeforeClass ()
9+ {
10+ if (false == function_exists ('geoip_open ' )) {
11+ throw new \PHPUnit_Framework_SkippedTestError ('The maxmind \'s official lib required to run these tests. ' );
12+ }
13+ if (false == function_exists ('GeoIP_record_by_addr ' )) {
14+ throw new \PHPUnit_Framework_SkippedTestError ('The maxmind \'s official lib required to run these tests. ' );
15+ }
16+ }
17+
18+ public static function provideIps ()
19+ {
20+ return array (
21+ '24.24.24.24 ' => array ('24.24.24.24 ' , 'East Syracuse ' , 'United States ' ),
22+ '80.24.24.24 ' => array ('80.24.24.24 ' , 'Sabadell ' , 'Spain ' ),
23+ );
24+ }
25+
26+ /**
27+ * @expectedException \Geocoder\Exception\InvalidArgumentException
28+ * @expectedExceptionMessage Given MaxMind dat file is not exist.
29+ */
30+ public function testThrowIfNotExistBinaryFileGiven ()
31+ {
32+ new MaxMindBinaryProvider ('not_exist.dat ' );
33+ }
34+
35+ /**
36+ * @dataProvider provideIps
37+ */
38+ public function testLocationResultContainsExpectedFields ($ ip )
39+ {
40+ $ binaryFile = __DIR__ .'/fixtures/GeoLiteCity.dat ' ;
41+
42+ $ provider = new MaxMindBinaryProvider ($ binaryFile );
43+
44+ $ result = $ provider ->getGeocodedData ($ ip );
45+
46+ $ this ->assertInternalType ('array ' , $ result );
47+
48+ $ this ->assertArrayHasKey ('country ' , $ result );
49+ $ this ->assertArrayHasKey ('countryCode ' , $ result );
50+ $ this ->assertArrayHasKey ('regionCode ' , $ result );
51+ $ this ->assertArrayHasKey ('city ' , $ result );
52+ $ this ->assertArrayHasKey ('latitude ' , $ result );
53+ $ this ->assertArrayHasKey ('longitude ' , $ result );
54+ $ this ->assertArrayHasKey ('zipcode ' , $ result );
55+ $ this ->assertArrayHasKey ('bounds ' , $ result );
56+ $ this ->assertArrayHasKey ('streetNumber ' , $ result );
57+ $ this ->assertArrayHasKey ('streetName ' , $ result );
58+ $ this ->assertArrayHasKey ('cityDistrict ' , $ result );
59+ $ this ->assertArrayHasKey ('county ' , $ result );
60+ $ this ->assertArrayHasKey ('countyCode ' , $ result );
61+ $ this ->assertArrayHasKey ('region ' , $ result );
62+ $ this ->assertArrayHasKey ('timezone ' , $ result );
63+ }
64+
65+ /**
66+ * @dataProvider provideIps
67+ */
68+ public function testFindLocationByIp ($ ip , $ expectedCity , $ expectedCountry )
69+ {
70+ $ binaryFile = __DIR__ .'/fixtures/GeoLiteCity.dat ' ;
71+
72+ $ provider = new MaxMindBinaryProvider ($ binaryFile );
73+
74+ $ result = $ provider ->getGeocodedData ($ ip );
75+
76+ $ this ->assertInternalType ('array ' , $ result );
77+
78+ $ this ->assertArrayHasKey ('city ' , $ result );
79+ $ this ->assertEquals ($ expectedCity , $ result ['city ' ]);
80+
81+ $ this ->assertArrayHasKey ('country ' , $ result );
82+ $ this ->assertEquals ($ expectedCountry , $ result ['country ' ]);
83+ }
84+
85+ public function testGetName ()
86+ {
87+ $ binaryFile = __DIR__ .'/fixtures/GeoLiteCity.dat ' ;
88+
89+ $ provider = new MaxMindBinaryProvider ($ binaryFile );
90+
91+ $ this ->assertEquals ('maxmind_binary ' , $ provider ->getName ());
92+ }
93+
94+ /**
95+ * @expectedException \Geocoder\Exception\NoResultException
96+ * @expectedExceptionMessage No results found for IP address 127.0.0.1
97+ */
98+ public function testThrowIfIpAddressCouldNotBeLocated ()
99+ {
100+ $ binaryFile = __DIR__ .'/fixtures/GeoLiteCity.dat ' ;
101+
102+ $ provider = new MaxMindBinaryProvider ($ binaryFile );
103+
104+ $ provider ->getGeocodedData ('127.0.0.1 ' );
105+ }
106+
107+ /**
108+ * @expectedException \Geocoder\Exception\UnsupportedException
109+ * @expectedExceptionMessage The MaxMindBinaryProvider does not support street addresses.
110+ */
111+ public function testThrowIfInvalidIpAddressGiven ()
112+ {
113+ $ binaryFile = __DIR__ .'/fixtures/GeoLiteCity.dat ' ;
114+
115+ $ provider = new MaxMindBinaryProvider ($ binaryFile );
116+
117+ $ provider ->getGeocodedData ('invalidIp ' );
118+ }
119+
120+ /**
121+ * @expectedException \Geocoder\Exception\UnsupportedException
122+ * @expectedExceptionMessage The MaxMindBinaryProvider is not able to do reverse geocoding.
123+ */
124+ public function testThrowOnReversedDataMethodUsage ()
125+ {
126+ $ binaryFile = __DIR__ .'/fixtures/GeoLiteCity.dat ' ;
127+
128+ $ provider = new MaxMindBinaryProvider ($ binaryFile );
129+
130+ $ provider ->getReversedData (array ());
131+ }
132+
133+ }
0 commit comments