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