1: <?php
2:
3: namespace GeoIp2\Test\Model;
4:
5: use GeoIp2\Model\Country;
6:
7: 8: 9:
10: class CountryTest extends \PHPUnit_Framework_TestCase
11: {
12: private $raw = [
13: 'continent' => [
14: 'code' => 'NA',
15: 'geoname_id' => 42,
16: 'names' => ['en' => 'North America'],
17: ],
18: 'country' => [
19: 'geoname_id' => 1,
20: 'iso_code' => 'US',
21: 'names' => ['en' => 'United States of America'],
22: ],
23: 'registered_country' => [
24: 'geoname_id' => 2,
25: 'is_in_european_union' => true,
26: 'iso_code' => 'DE',
27: 'names' => ['en' => 'Germany'],
28: ],
29: 'traits' => [
30: 'ip_address' => '1.2.3.4',
31: ],
32: ];
33:
34: private $model;
35:
36: public function setUp()
37: {
38: $this->model = new Country($this->raw, ['en']);
39: }
40:
41: public function testObjects()
42: {
43: $this->assertInstanceOf(
44: 'GeoIp2\Model\Country',
45: $this->model,
46: 'minimal GeoIp2::Model::Country object'
47: );
48: $this->assertInstanceOf(
49: 'GeoIp2\Record\Continent',
50: $this->model->continent
51: );
52: $this->assertInstanceOf(
53: 'GeoIp2\Record\Country',
54: $this->model->country
55: );
56: $this->assertInstanceOf(
57: 'GeoIp2\Record\Country',
58: $this->model->registeredCountry
59: );
60: $this->assertInstanceOf(
61: 'GeoIp2\Record\RepresentedCountry',
62: $this->model->representedCountry
63: );
64: $this->assertInstanceOf(
65: 'GeoIp2\Record\Traits',
66: $this->model->traits
67: );
68: }
69:
70: public function testValues()
71: {
72: $this->assertSame(
73: 42,
74: $this->model->continent->geonameId,
75: 'continent geoname_id is 42'
76: );
77:
78: $this->assertSame(
79: 'NA',
80: $this->model->continent->code,
81: 'continent code is NA'
82: );
83:
84: $this->assertSame(
85: ['en' => 'North America'],
86: $this->model->continent->names,
87: 'continent names'
88: );
89:
90: $this->assertSame(
91: 'North America',
92: $this->model->continent->name,
93: 'continent name is North America'
94: );
95:
96: $this->assertSame(
97: 1,
98: $this->model->country->geonameId,
99: 'country geoname_id is 1'
100: );
101:
102: $this->assertFalse(
103: $this->model->country->isInEuropeanUnion,
104: 'country is_in_european_union is false'
105: );
106:
107: $this->assertSame(
108: 'US',
109: $this->model->country->isoCode,
110: 'country iso_code is US'
111: );
112:
113: $this->assertSame(
114: ['en' => 'United States of America'],
115: $this->model->country->names,
116: 'country name'
117: );
118:
119: $this->assertSame(
120: $this->model->country->name,
121: 'United States of America',
122: 'country name is United States of America'
123: );
124:
125: $this->assertNull(
126: $this->model->country->confidence,
127: 'country confidence is undef'
128: );
129:
130: $this->assertSame(
131: 2,
132: $this->model->registeredCountry->geonameId,
133: 'registered_country geoname_id is 2'
134: );
135:
136: $this->assertTrue(
137: $this->model->registeredCountry->isInEuropeanUnion,
138: 'registered_country is_in_european_union is true'
139: );
140:
141: $this->assertSame(
142: 'DE',
143: $this->model->registeredCountry->isoCode,
144: 'registered_country iso_code is Germany'
145: );
146:
147: $this->assertSame(
148: ['en' => 'Germany'],
149: $this->model->registeredCountry->names,
150: 'registered_country names'
151: );
152:
153: $this->assertSame(
154: 'Germany',
155: $this->model->registeredCountry->name,
156: 'registered_country name is Germany'
157: );
158:
159: foreach (['isAnonymousProxy', 'isSatelliteProvider'] as $meth) {
160: $this->assertFalse(
161: $this->model->traits->$meth,
162: "traits $meth returns 0 by default"
163: );
164: }
165:
166: $this->assertSame(
167: $this->raw,
168: $this->model->raw,
169: 'raw method returns raw input'
170: );
171: }
172:
173: public function testJsonSerialize()
174: {
175: $this->assertSame(
176: $this->raw,
177: $this->model->jsonSerialize(),
178: 'jsonSerialize returns initial array'
179: );
180:
181: $this->assertSame(
182: $this->raw['country'],
183: $this->model->country->jsonSerialize(),
184: 'jsonSerialize returns initial array for the record'
185: );
186:
187: if (version_compare(PHP_VERSION, '5.4.0', '<')) {
188: $this->markTestSkipped('Requires PHP 5.4+.');
189: }
190:
191: $this->assertSame(
192: json_encode($this->raw),
193: json_encode($this->model),
194: 'json_encode can be called on the model object directly'
195: );
196:
197: $this->assertSame(
198: json_encode($this->raw['country']),
199: json_encode($this->model->country),
200: 'json_encode can be called on the record object directly'
201: );
202: }
203:
204: public function testIsSet()
205: {
206: $this->assertTrue(isset($this->model->traits), 'traits is set');
207: $this->assertFalse(isset($this->model->unknown), 'unknown is not set');
208:
209: $this->assertTrue(
210: isset($this->model->traits->ipAddress),
211: 'ip_address is set'
212: );
213: $this->assertFalse(
214: isset($this->model->traits->unknown),
215: 'unknown trait is not set'
216: );
217: }
218:
219: 220: 221: 222:
223: public function testUnknownRecord()
224: {
225: $this->model->unknownRecord;
226: }
227:
228: 229: 230: 231:
232: public function testUnknownTrait()
233: {
234: $this->model->traits->unknown;
235: }
236: }
237: