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