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