1: <?php
2:
3: namespace GeoIp2\Test\Model;
4:
5: use GeoIp2\Model\Insights;
6:
7: 8: 9:
10: class InsightsTest extends \PHPUnit_Framework_TestCase
11: {
12: public function testFull()
13: {
14: $raw = [
15: 'city' => [
16: 'confidence' => 76,
17: 'geoname_id' => 9876,
18: 'names' => ['en' => 'Minneapolis'],
19: ],
20: 'continent' => [
21: 'code' => 'NA',
22: 'geoname_id' => 42,
23: 'names' => ['en' => 'North America'],
24: ],
25: 'country' => [
26: 'confidence' => 99,
27: 'geoname_id' => 1,
28: 'iso_code' => 'US',
29: 'names' => ['en' => 'United States of America'],
30: ],
31: 'location' => [
32: 'average_income' => 24626,
33: 'accuracy_radius' => 1500,
34: 'latitude' => 44.98,
35: 'longitude' => 93.2636,
36: 'metro_code' => 765,
37: 'population_density' => 1341,
38: 'postal_code' => '55401',
39: 'postal_confidence' => 33,
40: 'time_zone' => 'America/Chicago',
41: ],
42: 'maxmind' => [
43: 'queries_remaining' => 22,
44: ],
45: 'registered_country' => [
46: 'geoname_id' => 2,
47: 'iso_code' => 'CA',
48: 'names' => ['en' => 'Canada'],
49: ],
50: 'represented_country' => [
51: 'geoname_id' => 3,
52: 'iso_code' => 'GB',
53: 'names' => ['en' => 'United Kingdom'],
54: ],
55: 'subdivisions' => [
56: [
57: 'confidence' => 88,
58: 'geoname_id' => 574635,
59: 'iso_code' => 'MN',
60: 'names' => ['en' => 'Minnesota'],
61: ],
62: ],
63: 'traits' => [
64: 'autonomous_system_number' => 1234,
65: 'autonomous_system_organization' => 'AS Organization',
66: 'domain' => 'example.com',
67: 'ip_address' => '1.2.3.4',
68: 'is_anonymous' => true,
69: 'is_anonymous_vpn' => true,
70: 'is_hosting_provider' => true,
71: 'is_public_proxy' => true,
72: 'is_satellite_provider' => true,
73: 'is_tor_exit_node' => true,
74: 'isp' => 'Comcast',
75: 'organization' => 'Blorg',
76: 'user_type' => 'college',
77: ],
78: ];
79:
80: $model = new Insights($raw, ['en']);
81:
82: $this->assertInstanceOf(
83: 'GeoIp2\Model\Insights',
84: $model,
85: 'GeoIp2\Model\Insights object'
86: );
87:
88: $this->assertInstanceOf(
89: 'GeoIp2\Record\City',
90: $model->city,
91: '$model->city'
92: );
93:
94: $this->assertInstanceOf(
95: 'GeoIp2\Record\Continent',
96: $model->continent,
97: '$model->continent'
98: );
99:
100: $this->assertInstanceOf(
101: 'GeoIp2\Record\Country',
102: $model->country,
103: '$model->country'
104: );
105:
106: $this->assertInstanceOf(
107: 'GeoIp2\Record\Location',
108: $model->location,
109: '$model->location'
110: );
111:
112: $this->assertSame(
113: 24626,
114: $model->location->averageIncome,
115: '$model->location->averageIncome is 24626'
116: );
117:
118: $this->assertSame(
119: 1341,
120: $model->location->populationDensity,
121: '$model->location->populationDensity is 1341'
122: );
123:
124: $this->assertInstanceOf(
125: 'GeoIp2\Record\Country',
126: $model->registeredCountry,
127: '$model->registeredCountry'
128: );
129:
130: $this->assertInstanceOf(
131: 'GeoIp2\Record\RepresentedCountry',
132: $model->representedCountry,
133: '$model->representedCountry'
134: );
135:
136: $subdivisions = $model->subdivisions;
137: foreach ($subdivisions as $subdiv) {
138: $this->assertInstanceOf('GeoIp2\Record\Subdivision', $subdiv);
139: }
140:
141: $this->assertInstanceOf(
142: 'GeoIp2\Record\Subdivision',
143: $model->mostSpecificSubdivision,
144: '$model->mostSpecificSubdivision'
145: );
146:
147: $this->assertInstanceOf(
148: 'GeoIp2\Record\Traits',
149: $model->traits,
150: '$model->traits'
151: );
152:
153: $this->assertTrue(
154: $model->traits->isAnonymous,
155: '$model->traits->isAnonymous is true'
156: );
157:
158: $this->assertTrue(
159: $model->traits->isHostingProvider,
160: '$model->traits->isHostingProvider is true'
161: );
162:
163: $this->assertTrue(
164: $model->traits->isPublicProxy,
165: '$model->traits->isPublicProxy is true'
166: );
167:
168: $this->assertTrue(
169: $model->traits->isSatelliteProvider,
170: '$model->traits->isSatelliteProvider is true'
171: );
172:
173: $this->assertTrue(
174: $model->traits->isTorExitNode,
175: '$model->traits->isTorExitNode is true'
176: );
177:
178: $this->assertFalse(
179: $model->traits->isAnonymousProxy,
180: '$model->traits->isAnonymousProxy is false'
181: );
182:
183: $this->assertSame(
184: 22,
185: $model->maxmind->queriesRemaining,
186: 'queriesRemaining is correct'
187: );
188:
189: $this->assertSame(
190: $raw,
191: $model->raw,
192: 'raw method returns raw input'
193: );
194: }
195:
196: public function testEmptyObjects()
197: {
198: $raw = ['traits' => ['ip_address' => '5.6.7.8']];
199:
200: $model = new Insights($raw, ['en']);
201:
202: $this->assertInstanceOf(
203: 'GeoIp2\Model\Insights',
204: $model,
205: 'GeoIp2\Model\Insights object with no data except traits.ipAddress'
206: );
207:
208: $this->assertInstanceOf(
209: 'GeoIp2\Record\City',
210: $model->city,
211: '$model->city'
212: );
213:
214: $this->assertInstanceOf(
215: 'GeoIp2\Record\Continent',
216: $model->continent,
217: '$model->continent'
218: );
219:
220: $this->assertInstanceOf(
221: 'GeoIp2\Record\Country',
222: $model->country,
223: '$model->country'
224: );
225:
226: $this->assertInstanceOf(
227: 'GeoIp2\Record\Location',
228: $model->location,
229: '$model->location'
230: );
231:
232: $this->assertInstanceOf(
233: 'GeoIp2\Record\Country',
234: $model->registeredCountry,
235: '$model->registeredCountry'
236: );
237:
238: $this->assertInstanceOf(
239: 'GeoIp2\Record\RepresentedCountry',
240: $model->representedCountry,
241: '$model->representedCountry'
242: );
243:
244: $this->assertCount(
245: 0,
246: $model->subdivisions,
247: '$model->subdivisions returns an empty list'
248: );
249:
250: $this->assertInstanceOf(
251: 'GeoIp2\Record\Subdivision',
252: $model->mostSpecificSubdivision,
253: '$model->mostSpecificSubdivision'
254: );
255:
256: $this->assertTrue(
257: isset($model->mostSpecificSubdivision),
258: 'mostSpecificSubdivision is set'
259: );
260:
261: $this->assertInstanceOf(
262: 'GeoIp2\Record\Traits',
263: $model->traits,
264: '$model->traits'
265: );
266:
267: $this->assertSame(
268: $raw,
269: $model->raw,
270: 'raw method returns raw input with no added empty values'
271: );
272: }
273:
274: public function testUnknown()
275: {
276: $raw = [
277: 'new_top_level' => ['foo' => 42],
278: 'city' => [
279: 'confidence' => 76,
280: 'geoname_id_id' => 9876,
281: 'names' => ['en' => 'Minneapolis'],
282: 'population' => 50,
283: ],
284: 'traits' => ['ip_address' => '5.6.7.8'],
285: ];
286:
287:
288: $model = new Insights($raw, ['en']);
289:
290: $this->assertInstanceOf(
291: 'GeoIp2\Model\Insights',
292: $model,
293: 'no exception when Insights model gets raw data with unknown keys'
294: );
295:
296: $this->assertSame(
297: $raw,
298: $model->raw,
299: 'raw method returns raw input'
300: );
301: }
302:
303: public function testMostSpecificSubdivisionWithNoSubdivisions()
304: {
305: $model = new Insights([], ['en']);
306:
307: $this->assertTrue(
308: isset($model->mostSpecificSubdivision),
309: 'mostSpecificSubdivision is set even on an empty response'
310: );
311: }
312: }
313: