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