1: <?php
2:
3: namespace GeoIp2\Test\WebService;
4:
5: use GeoIp2\Database\Reader;
6:
7: class ReaderTest extends \PHPUnit_Framework_TestCase
8: {
9: public function testDefaultLocale()
10: {
11: foreach (array('City', 'Country') as $type) {
12: $reader = new Reader("maxmind-db/test-data/GeoIP2-$type-Test.mmdb");
13: $method = lcfirst($type);
14: $record = $reader->$method('81.2.69.160');
15: $this->assertEquals('United Kingdom', $record->country->name);
16: }
17: $reader->close();
18: }
19:
20: public function testLocaleList()
21: {
22:
23: foreach (array('City', 'Country') as $type) {
24: $reader = new Reader(
25: "maxmind-db/test-data/GeoIP2-$type-Test.mmdb",
26: array('xx', 'ru', 'pt-BR', 'es', 'en')
27: );
28: $method = lcfirst($type);
29:
30: $record = $reader->$method('81.2.69.160');
31: $this->assertEquals('Великобритания', $record->country->name);
32: }
33: $reader->close();
34: }
35:
36: public function testHasIpAddress()
37: {
38: foreach (array('City', 'Country') as $type) {
39: $reader = new Reader("maxmind-db/test-data/GeoIP2-$type-Test.mmdb");
40: $method = lcfirst($type);
41: $record = $reader->$method('81.2.69.160');
42: $this->assertEquals('81.2.69.160', $record->traits->ipAddress);
43: }
44: $reader->close();
45: }
46:
47: 48: 49: 50:
51: public function testUnknownAddress()
52: {
53: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
54: $reader->city('10.10.10.10');
55: $reader->close();
56: }
57:
58: 59: 60: 61:
62: public function testIncorrectDatabase()
63: {
64: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
65: $reader->country('10.10.10.10');
66: $reader->close();
67: }
68:
69: 70: 71: 72:
73: public function testIncorrectDatabaseFlat()
74: {
75: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
76: $reader->domain('10.10.10.10');
77: $reader->close();
78: }
79:
80: 81: 82: 83:
84: public function testInvalidAddress()
85: {
86: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
87: $reader->city('invalid');
88: $reader->close();
89: }
90:
91: public function testAnonymousIp()
92: {
93: $reader = new Reader('maxmind-db/test-data/GeoIP2-Anonymous-IP-Test.mmdb');
94: $ipAddress = '1.2.0.1';
95:
96: $record = $reader->anonymousIp($ipAddress);
97: $this->assertSame(true, $record->isAnonymous);
98: $this->assertSame(true, $record->isAnonymousVpn);
99: $this->assertSame(false, $record->isHostingProvider);
100: $this->assertSame(false, $record->isPublicProxy);
101: $this->assertSame(false, $record->isTorExitNode);
102: $this->assertEquals($ipAddress, $record->ipAddress);
103: $reader->close();
104: }
105:
106: public function testConnectionType()
107: {
108: $reader = new Reader('maxmind-db/test-data/GeoIP2-Connection-Type-Test.mmdb');
109: $ipAddress = '1.0.1.0';
110:
111: $record = $reader->connectionType($ipAddress);
112: $this->assertEquals('Cable/DSL', $record->connectionType);
113: $this->assertEquals($ipAddress, $record->ipAddress);
114: $reader->close();
115: }
116:
117: public function testDomain()
118: {
119: $reader = new Reader('maxmind-db/test-data/GeoIP2-Domain-Test.mmdb');
120:
121: $ipAddress = '1.2.0.0';
122: $record = $reader->domain($ipAddress);
123: $this->assertEquals('maxmind.com', $record->domain);
124: $this->assertEquals($ipAddress, $record->ipAddress);
125: $reader->close();
126: }
127:
128: public function testIsp()
129: {
130: $reader = new Reader('maxmind-db/test-data/GeoIP2-ISP-Test.mmdb');
131:
132: $ipAddress = '1.128.0.0';
133: $record = $reader->isp($ipAddress);
134: $this->assertEquals(1221, $record->autonomousSystemNumber);
135: $this->assertEquals(
136: 'Telstra Pty Ltd',
137: $record->autonomousSystemOrganization
138: );
139:
140: $this->assertEquals('Telstra Internet', $record->isp);
141: $this->assertEquals('Telstra Internet', $record->organization);
142:
143: $this->assertEquals($ipAddress, $record->ipAddress);
144: $reader->close();
145: }
146:
147: public function testMetadata()
148: {
149: $reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
150: $this->assertEquals('GeoIP2-City', $reader->metadata()->databaseType);
151:
152: $reader->close();
153: }
154: }
155: