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