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_satellite_provider' => true,
69: 'isp' => 'Comcast',
70: 'organization' => 'Blorg',
71: 'user_type' => 'college',
72: ],
73: ];
74:
75: $model = new Insights($raw, ['en']);
76:
77: $this->assertInstanceOf(
78: 'GeoIp2\Model\Insights',
79: $model,
80: 'GeoIp2\Model\Insights object'
81: );
82:
83: $this->assertInstanceOf(
84: 'GeoIp2\Record\City',
85: $model->city,
86: '$model->city'
87: );
88:
89: $this->assertInstanceOf(
90: 'GeoIp2\Record\Continent',
91: $model->continent,
92: '$model->continent'
93: );
94:
95: $this->assertInstanceOf(
96: 'GeoIp2\Record\Country',
97: $model->country,
98: '$model->country'
99: );
100:
101: $this->assertInstanceOf(
102: 'GeoIp2\Record\Location',
103: $model->location,
104: '$model->location'
105: );
106:
107: $this->assertSame(
108: 24626,
109: $model->location->averageIncome,
110: '$model->location->averageIncome is 24626'
111: );
112:
113: $this->assertSame(
114: 1341,
115: $model->location->populationDensity,
116: '$model->location->populationDensity is 1341'
117: );
118:
119: $this->assertInstanceOf(
120: 'GeoIp2\Record\Country',
121: $model->registeredCountry,
122: '$model->registeredCountry'
123: );
124:
125: $this->assertInstanceOf(
126: 'GeoIp2\Record\RepresentedCountry',
127: $model->representedCountry,
128: '$model->representedCountry'
129: );
130:
131: $subdivisions = $model->subdivisions;
132: foreach ($subdivisions as $subdiv) {
133: $this->assertInstanceOf('GeoIp2\Record\Subdivision', $subdiv);
134: }
135:
136: $this->assertInstanceOf(
137: 'GeoIp2\Record\Subdivision',
138: $model->mostSpecificSubdivision,
139: '$model->mostSpecificSubdivision'
140: );
141:
142: $this->assertInstanceOf(
143: 'GeoIp2\Record\Traits',
144: $model->traits,
145: '$model->traits'
146: );
147:
148: $this->assertSame(
149: true,
150: $model->traits->isSatelliteProvider,
151: '$model->traits->isSatelliteProvider is true'
152: );
153:
154: $this->assertSame(
155: false,
156: $model->traits->isAnonymousProxy,
157: '$model->traits->isAnonymousProxy is false'
158: );
159:
160: $this->assertSame(
161: 22,
162: $model->maxmind->queriesRemaining,
163: 'queriesRemaining is correct'
164: );
165:
166: $this->assertSame(
167: $raw,
168: $model->raw,
169: 'raw method returns raw input'
170: );
171: }
172:
173: public function testEmptyObjects()
174: {
175: $raw = ['traits' => ['ip_address' => '5.6.7.8']];
176:
177: $model = new Insights($raw, ['en']);
178:
179: $this->assertInstanceOf(
180: 'GeoIp2\Model\Insights',
181: $model,
182: 'GeoIp2\Model\Insights object with no data except traits.ipAddress'
183: );
184:
185: $this->assertInstanceOf(
186: 'GeoIp2\Record\City',
187: $model->city,
188: '$model->city'
189: );
190:
191: $this->assertInstanceOf(
192: 'GeoIp2\Record\Continent',
193: $model->continent,
194: '$model->continent'
195: );
196:
197: $this->assertInstanceOf(
198: 'GeoIp2\Record\Country',
199: $model->country,
200: '$model->country'
201: );
202:
203: $this->assertInstanceOf(
204: 'GeoIp2\Record\Location',
205: $model->location,
206: '$model->location'
207: );
208:
209: $this->assertInstanceOf(
210: 'GeoIp2\Record\Country',
211: $model->registeredCountry,
212: '$model->registeredCountry'
213: );
214:
215: $this->assertInstanceOf(
216: 'GeoIp2\Record\RepresentedCountry',
217: $model->representedCountry,
218: '$model->representedCountry'
219: );
220:
221: $this->assertCount(
222: 0,
223: $model->subdivisions,
224: '$model->subdivisions returns an empty list'
225: );
226:
227: $this->assertInstanceOf(
228: 'GeoIp2\Record\Subdivision',
229: $model->mostSpecificSubdivision,
230: '$model->mostSpecificSubdivision'
231: );
232:
233: $this->assertTrue(
234: isset($model->mostSpecificSubdivision),
235: 'mostSpecificSubdivision is set'
236: );
237:
238: $this->assertInstanceOf(
239: 'GeoIp2\Record\Traits',
240: $model->traits,
241: '$model->traits'
242: );
243:
244: $this->assertSame(
245: $raw,
246: $model->raw,
247: 'raw method returns raw input with no added empty values'
248: );
249: }
250:
251: public function testUnknown()
252: {
253: $raw = [
254: 'new_top_level' => ['foo' => 42],
255: 'city' => [
256: 'confidence' => 76,
257: 'geoname_id_id' => 9876,
258: 'names' => ['en' => 'Minneapolis'],
259: 'population' => 50,
260: ],
261: 'traits' => ['ip_address' => '5.6.7.8'],
262: ];
263:
264:
265: $model = new Insights($raw, ['en']);
266:
267: $this->assertInstanceOf(
268: 'GeoIp2\Model\Insights',
269: $model,
270: 'no exception when Insights model gets raw data with unknown keys'
271: );
272:
273: $this->assertSame(
274: $raw,
275: $model->raw,
276: 'raw method returns raw input'
277: );
278: }
279:
280: public function testMostSpecificSubdivisionWithNoSubdivisions()
281: {
282: $model = new Insights([], ['en']);
283:
284: $this->assertTrue(
285: isset($model->mostSpecificSubdivision),
286: 'mostSpecificSubdivision is set even on an empty response'
287: );
288: }
289: }
290: