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: public function testUnknownAddress()
67: {
68: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
69: $reader->city('10.10.10.10');
70: $reader->close();
71: }
72:
73: 74: 75: 76:
77: public function testIncorrectDatabase()
78: {
79: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
80: $reader->country('10.10.10.10');
81: $reader->close();
82: }
83:
84: 85: 86: 87:
88: public function testIncorrectDatabaseFlat()
89: {
90: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
91: $reader->domain('10.10.10.10');
92: $reader->close();
93: }
94:
95: 96: 97: 98:
99: public function testInvalidAddress()
100: {
101: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
102: $reader->city('invalid');
103: $reader->close();
104: }
105:
106: public function testAnonymousIp()
107: {
108: $reader = new Reader('maxmind-db/test-data/GeoIP2-Anonymous-IP-Test.mmdb');
109: $ipAddress = '1.2.0.1';
110:
111: $record = $reader->anonymousIp($ipAddress);
112: $this->assertSame(true, $record->isAnonymous);
113: $this->assertSame(true, $record->isAnonymousVpn);
114: $this->assertSame(false, $record->isHostingProvider);
115: $this->assertSame(false, $record->isPublicProxy);
116: $this->assertSame(false, $record->isTorExitNode);
117: $this->assertSame($ipAddress, $record->ipAddress);
118: $reader->close();
119: }
120:
121: public function testAsn()
122: {
123: $reader = new Reader('maxmind-db/test-data/GeoLite2-ASN-Test.mmdb');
124:
125: $ipAddress = '1.128.0.0';
126: $record = $reader->asn($ipAddress);
127: $this->assertSame(1221, $record->autonomousSystemNumber);
128: $this->assertSame(
129: 'Telstra Pty Ltd',
130: $record->autonomousSystemOrganization
131: );
132:
133: $this->assertSame($ipAddress, $record->ipAddress);
134: $reader->close();
135: }
136:
137: public function testConnectionType()
138: {
139: $reader = new Reader('maxmind-db/test-data/GeoIP2-Connection-Type-Test.mmdb');
140: $ipAddress = '1.0.1.0';
141:
142: $record = $reader->connectionType($ipAddress);
143: $this->assertSame('Cable/DSL', $record->connectionType);
144: $this->assertSame($ipAddress, $record->ipAddress);
145: $reader->close();
146: }
147:
148: public function testDomain()
149: {
150: $reader = new Reader('maxmind-db/test-data/GeoIP2-Domain-Test.mmdb');
151:
152: $ipAddress = '1.2.0.0';
153: $record = $reader->domain($ipAddress);
154: $this->assertSame('maxmind.com', $record->domain);
155: $this->assertSame($ipAddress, $record->ipAddress);
156: $reader->close();
157: }
158:
159: public function testEnterprise()
160: {
161: $reader = new Reader('maxmind-db/test-data/GeoIP2-Enterprise-Test.mmdb');
162:
163: $ipAddress = '74.209.24.0';
164: $record = $reader->enterprise($ipAddress);
165: $this->assertSame(11, $record->city->confidence);
166: $this->assertSame(99, $record->country->confidence);
167: $this->assertSame(6252001, $record->country->geonameId);
168:
169: $this->assertSame(27, $record->location->accuracyRadius);
170:
171: $this->assertSame('Cable/DSL', $record->traits->connectionType);
172: $this->assertSame(true, $record->traits->isLegitimateProxy);
173:
174: $this->assertSame($ipAddress, $record->traits->ipAddress);
175: $reader->close();
176: }
177:
178: public function testIsp()
179: {
180: $reader = new Reader('maxmind-db/test-data/GeoIP2-ISP-Test.mmdb');
181:
182: $ipAddress = '1.128.0.0';
183: $record = $reader->isp($ipAddress);
184: $this->assertSame(1221, $record->autonomousSystemNumber);
185: $this->assertSame(
186: 'Telstra Pty Ltd',
187: $record->autonomousSystemOrganization
188: );
189:
190: $this->assertSame('Telstra Internet', $record->isp);
191: $this->assertSame('Telstra Internet', $record->organization);
192:
193: $this->assertSame($ipAddress, $record->ipAddress);
194: $reader->close();
195: }
196:
197: public function testMetadata()
198: {
199: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
200: $this->assertSame('GeoIP2-City', $reader->metadata()->databaseType);
201:
202: $reader->close();
203: }
204: }
205: