1: <?php
2:
3: namespace GeoIp2\Test\Database;
4:
5: use GeoIp2\Database\Reader;
6:
7: 8: 9:
10: class ReaderTest extends \PHPUnit_Framework_TestCase
11: {
12: public function databaseTypes()
13: {
14: return [['City', 'city'], ['Country', 'country']];
15: }
16:
17: 18: 19: 20: 21: 22:
23: public function testDefaultLocale($type, $method)
24: {
25: $reader = new Reader("maxmind-db/test-data/GeoIP2-$type-Test.mmdb");
26: $record = $reader->$method('81.2.69.160');
27: $this->assertSame('United Kingdom', $record->country->name);
28: $reader->close();
29: }
30:
31: 32: 33: 34: 35: 36:
37: public function testLocaleList($type, $method)
38: {
39: $reader = new Reader(
40: "maxmind-db/test-data/GeoIP2-$type-Test.mmdb",
41: ['xx', 'ru', 'pt-BR', 'es', 'en']
42: );
43: $record = $reader->$method('81.2.69.160');
44: $this->assertSame('Великобритания', $record->country->name);
45: $reader->close();
46: }
47:
48: 49: 50: 51: 52: 53:
54: public function testHasIpAddress($type, $method)
55: {
56: $reader = new Reader("maxmind-db/test-data/GeoIP2-$type-Test.mmdb");
57: $record = $reader->$method('81.2.69.160');
58: $this->assertSame('81.2.69.160', $record->traits->ipAddress);
59: $reader->close();
60: }
61:
62: 63: 64: 65: 66: 67:
68: public function testIsInEuropeanUnion($type, $method)
69: {
70: $reader = new Reader("maxmind-db/test-data/GeoIP2-$type-Test.mmdb");
71: $record = $reader->$method('81.2.69.160');
72: $this->assertTrue(
73: $record->country->isInEuropeanUnion,
74: 'country is_in_european_union is true'
75: );
76: $this->assertFalse(
77: $record->registeredCountry->isInEuropeanUnion,
78: 'registered_country is_in_european_union is false'
79: );
80: $reader->close();
81: }
82:
83: 84: 85: 86:
87: public function testUnknownAddress()
88: {
89: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
90: $reader->city('10.10.10.10');
91: $reader->close();
92: }
93:
94: 95: 96: 97:
98: public function testIncorrectDatabase()
99: {
100: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
101: $reader->country('10.10.10.10');
102: $reader->close();
103: }
104:
105: 106: 107: 108:
109: public function testIncorrectDatabaseFlat()
110: {
111: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
112: $reader->domain('10.10.10.10');
113: $reader->close();
114: }
115:
116: 117: 118: 119:
120: public function testInvalidAddress()
121: {
122: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
123: $reader->city('invalid');
124: $reader->close();
125: }
126:
127: public function testAnonymousIp()
128: {
129: $reader = new Reader('maxmind-db/test-data/GeoIP2-Anonymous-IP-Test.mmdb');
130: $ipAddress = '1.2.0.1';
131:
132: $record = $reader->anonymousIp($ipAddress);
133: $this->assertTrue($record->isAnonymous);
134: $this->assertTrue($record->isAnonymousVpn);
135: $this->assertFalse($record->isHostingProvider);
136: $this->assertFalse($record->isPublicProxy);
137: $this->assertFalse($record->isTorExitNode);
138: $this->assertSame($ipAddress, $record->ipAddress);
139: $reader->close();
140: }
141:
142: public function testAsn()
143: {
144: $reader = new Reader('maxmind-db/test-data/GeoLite2-ASN-Test.mmdb');
145:
146: $ipAddress = '1.128.0.0';
147: $record = $reader->asn($ipAddress);
148: $this->assertSame(1221, $record->autonomousSystemNumber);
149: $this->assertSame(
150: 'Telstra Pty Ltd',
151: $record->autonomousSystemOrganization
152: );
153:
154: $this->assertSame($ipAddress, $record->ipAddress);
155: $reader->close();
156: }
157:
158: public function testConnectionType()
159: {
160: $reader = new Reader('maxmind-db/test-data/GeoIP2-Connection-Type-Test.mmdb');
161: $ipAddress = '1.0.1.0';
162:
163: $record = $reader->connectionType($ipAddress);
164: $this->assertSame('Cable/DSL', $record->connectionType);
165: $this->assertSame($ipAddress, $record->ipAddress);
166: $reader->close();
167: }
168:
169: public function testDomain()
170: {
171: $reader = new Reader('maxmind-db/test-data/GeoIP2-Domain-Test.mmdb');
172:
173: $ipAddress = '1.2.0.0';
174: $record = $reader->domain($ipAddress);
175: $this->assertSame('maxmind.com', $record->domain);
176: $this->assertSame($ipAddress, $record->ipAddress);
177: $reader->close();
178: }
179:
180: public function testEnterprise()
181: {
182: $reader = new Reader('maxmind-db/test-data/GeoIP2-Enterprise-Test.mmdb');
183:
184: $ipAddress = '74.209.24.0';
185: $record = $reader->enterprise($ipAddress);
186: $this->assertSame(11, $record->city->confidence);
187: $this->assertSame(99, $record->country->confidence);
188: $this->assertSame(6252001, $record->country->geonameId);
189: $this->assertFalse($record->country->isInEuropeanUnion);
190:
191: $this->assertSame(27, $record->location->accuracyRadius);
192:
193: $this->assertFalse($record->registeredCountry->isInEuropeanUnion);
194:
195: $this->assertSame('Cable/DSL', $record->traits->connectionType);
196: $this->assertTrue($record->traits->isLegitimateProxy);
197:
198: $this->assertSame($ipAddress, $record->traits->ipAddress);
199: $reader->close();
200: }
201:
202: public function testIsp()
203: {
204: $reader = new Reader('maxmind-db/test-data/GeoIP2-ISP-Test.mmdb');
205:
206: $ipAddress = '1.128.0.0';
207: $record = $reader->isp($ipAddress);
208: $this->assertSame(1221, $record->autonomousSystemNumber);
209: $this->assertSame(
210: 'Telstra Pty Ltd',
211: $record->autonomousSystemOrganization
212: );
213:
214: $this->assertSame('Telstra Internet', $record->isp);
215: $this->assertSame('Telstra Internet', $record->organization);
216:
217: $this->assertSame($ipAddress, $record->ipAddress);
218: $reader->close();
219: }
220:
221: public function testMetadata()
222: {
223: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
224: $this->assertSame('GeoIP2-City', $reader->metadata()->databaseType);
225:
226: $reader->close();
227: }
228: }
229: