1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\GeoTimeZone; |
4
|
|
|
|
5
|
|
|
use GeoTimeZone\Calculator; |
6
|
|
|
use PHPUnit\Runner\Exception; |
7
|
|
|
use Tests\AbstractUnitTestCase; |
8
|
|
|
|
9
|
|
|
class CalculatorTest extends AbstractUnitTestCase |
10
|
|
|
{ |
11
|
|
|
const DATA_DIRECTORY = "/../../data/"; |
12
|
|
|
|
13
|
|
|
protected $calculator; |
14
|
|
|
|
15
|
|
|
protected function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->calculator = new Calculator(__DIR__ . self::DATA_DIRECTORY); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function getDataLocalDate() |
21
|
|
|
{ |
22
|
|
|
return array( |
23
|
|
|
'Testing Lisbon' => array( |
24
|
|
|
'latitude' => 41.142700, |
25
|
|
|
'longitude' => -8.612150, |
26
|
|
|
'timestamp' => 1458844434, |
27
|
|
|
'expectedTimeZone' => 'Europe/Lisbon', |
28
|
|
|
'$expectedOffset' => 0 |
29
|
|
|
), |
30
|
|
|
'Testing Madrid' => array( |
31
|
|
|
'latitude' => 39.452800, |
32
|
|
|
'longitude' => -0.347038, |
33
|
|
|
'timestamp' => 1469387760, |
34
|
|
|
'expectedTimeZone' => 'Europe/Madrid', |
35
|
|
|
'$expectedOffset' => 7200 |
36
|
|
|
), |
37
|
|
|
'Testing Caracas' => array( |
38
|
|
|
'latitude' => 7.811258, |
39
|
|
|
'longitude' => -72.199897, |
40
|
|
|
'timestamp' => 1412482901, |
41
|
|
|
'expectedTimeZone' => 'America/Caracas', |
42
|
|
|
'$expectedOffset' => -16200 |
43
|
|
|
), |
44
|
|
|
'Testing Praga' => array( |
45
|
|
|
'latitude' => 50.087257, |
46
|
|
|
'longitude' => 14.636790, |
47
|
|
|
'timestamp' => 1506408879, |
48
|
|
|
'expectedTimeZone' => 'Europe/Prague', |
49
|
|
|
'$expectedOffset' => 7200 |
50
|
|
|
), |
51
|
|
|
'Testing Berlin Limit' => array( |
52
|
|
|
'latitude' => 48.518129, |
53
|
|
|
'longitude' => 13.730860, |
54
|
|
|
'timestamp' => 1506408879, |
55
|
|
|
'expectedTimeZone' => 'Europe/Vienna', |
56
|
|
|
'$expectedOffset' => 7200 |
57
|
|
|
), |
58
|
|
|
'Testing Roma Limit' => array( |
59
|
|
|
'latitude' => 46.840306, |
60
|
|
|
'longitude' => 12.301866, |
61
|
|
|
'timestamp' => 1506408879, |
62
|
|
|
'expectedTimeZone' => 'Europe/Rome', |
63
|
|
|
'$expectedOffset' => 7200 |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getDataCorrectTimestamp() |
69
|
|
|
{ |
70
|
|
|
return array( |
71
|
|
|
'Testing Lisbon' => array( |
72
|
|
|
'latitude' => 41.142700, |
73
|
|
|
'longitude' => -8.612150, |
74
|
|
|
'timestamp' => 1458844434, |
75
|
|
|
'expectedTimestamp' => 1458844434, |
76
|
|
|
), |
77
|
|
|
'Testing Madrid' => array( |
78
|
|
|
'latitude' => 39.452800, |
79
|
|
|
'longitude' => -0.347038, |
80
|
|
|
'timestamp' => 1469387760, |
81
|
|
|
'expectedTimestamp' => 1469380560, |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getDataTimeZoneName() |
87
|
|
|
{ |
88
|
|
|
return array( |
89
|
|
|
'Testing Lisbon' => array( |
90
|
|
|
'latitude' => 41.142700, |
91
|
|
|
'longitude' => -8.612150, |
92
|
|
|
'expectedTimeZone' => 'Europe/Lisbon', |
93
|
|
|
), |
94
|
|
|
'Testing Madrid' => array( |
95
|
|
|
'latitude' => 39.452800, |
96
|
|
|
'longitude' => -0.347038, |
97
|
|
|
'expectedTimeZone' => 'Europe/Madrid', |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getNoTimezone() |
103
|
|
|
{ |
104
|
|
|
return array( |
105
|
|
|
'Testing None Timezone' => array( |
106
|
|
|
'latitude' => -1, |
107
|
|
|
'longitude' => -1, |
108
|
|
|
'timestamp' => 0, |
109
|
|
|
'expectedException' => "ERROR: TimeZone not found", |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getDataWrongLatitude() |
115
|
|
|
{ |
116
|
|
|
return array( |
117
|
|
|
'Testing Wrong Latitude' => array( |
118
|
|
|
'latitude' => 10000000, |
119
|
|
|
'longitude' => -8.612150, |
120
|
|
|
'expectedException' => "Invalid latitude: 10000000", |
121
|
|
|
), |
122
|
|
|
'Testing Null Latitude' => array( |
123
|
|
|
'latitude' => null, |
124
|
|
|
'longitude' => -8.612150, |
125
|
|
|
'expectedException' => "Invalid latitude: ", |
126
|
|
|
) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getDataWrongLongitude() |
131
|
|
|
{ |
132
|
|
|
return array( |
133
|
|
|
'Testing Wrong Longitude' => array( |
134
|
|
|
'latitude' => 41.142700, |
135
|
|
|
'longitude' => 10000000, |
136
|
|
|
'expectedException' => "Invalid longitude: 10000000", |
137
|
|
|
|
138
|
|
|
), |
139
|
|
|
'Testing Null Longitude' => array( |
140
|
|
|
'latitude' => 41.142700, |
141
|
|
|
'longitude' => null, |
142
|
|
|
'expectedException' => "Invalid longitude: ", |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getDataMaxLatitude() |
148
|
|
|
{ |
149
|
|
|
return array( |
150
|
|
|
'Testing Positive Max Latitude' => array( |
151
|
|
|
'latitude' => 90.0, |
152
|
|
|
'longitude' => -8.612150, |
153
|
|
|
'adjustedLatitude' => 89.9999, |
154
|
|
|
'expectedException' => "ERROR: TimeZone not found", |
155
|
|
|
), |
156
|
|
|
'Testing Negative Max Latitude' => array( |
157
|
|
|
'latitude' => -90.0, |
158
|
|
|
'longitude' => -8.612150, |
159
|
|
|
'adjustedLatitude' => -89.9999, |
160
|
|
|
'expectedException' => "ERROR: TimeZone not found", |
161
|
|
|
) |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getDataMaxLongitude() |
166
|
|
|
{ |
167
|
|
|
return array( |
168
|
|
|
'Testing Positive Max Longitude' => array( |
169
|
|
|
'latitude' => -8.612150, |
170
|
|
|
'longitude' => 180.0, |
171
|
|
|
'adjustedLongitude' => 179.9999, |
172
|
|
|
'expectedException' => "ERROR: TimeZone not found", |
173
|
|
|
), |
174
|
|
|
'Testing Negative Max Longitude' => array( |
175
|
|
|
'latitude' => -8.612150, |
176
|
|
|
'longitude' => -180.0, |
177
|
|
|
'adjustedLongitude' => -179.9999, |
178
|
|
|
'expectedException' => "ERROR: TimeZone not found", |
179
|
|
|
) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @dataProvider getDataWrongLatitude |
185
|
|
|
* @param $latitude |
186
|
|
|
* @param $longitude |
187
|
|
|
* @param $expectedException |
188
|
|
|
*/ |
189
|
|
|
public function testGetTimeZoneNameWithWrongLatitude($latitude, $longitude, $expectedException) |
190
|
|
|
{ |
191
|
|
|
try { |
192
|
|
|
$this->expectException(\ErrorException::class); |
193
|
|
|
$this->expectExceptionMessage($expectedException); |
194
|
|
|
$timeZone = $this->calculator->getTimeZoneName($latitude, $longitude); |
|
|
|
|
195
|
|
|
} catch (Exception $error) { |
|
|
|
|
196
|
|
|
echo $error->getMessage(); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @dataProvider getDataWrongLongitude |
202
|
|
|
* @param $latitude |
203
|
|
|
* @param $longitude |
204
|
|
|
* @param $expectedException |
205
|
|
|
*/ |
206
|
|
|
public function testGetTimeZoneNameWithWrongLongitude($latitude, $longitude, $expectedException) |
207
|
|
|
{ |
208
|
|
|
try { |
209
|
|
|
$this->expectException(\ErrorException::class); |
210
|
|
|
$this->expectExceptionMessage($expectedException); |
211
|
|
|
$timeZone = $this->calculator->getTimeZoneName($latitude, $longitude); |
|
|
|
|
212
|
|
|
} catch (Exception $error) { |
|
|
|
|
213
|
|
|
echo $error->getMessage(); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @dataProvider getDataMaxLatitude |
219
|
|
|
* @param $latitude |
220
|
|
|
* @param $longitude |
221
|
|
|
* @param $adjustedLatitude |
222
|
|
|
* @param $expectedException |
223
|
|
|
*/ |
224
|
|
|
public function testGetTimeZoneNameWithMaxLatitude($latitude, $longitude, $adjustedLatitude, $expectedException) |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
try { |
227
|
|
|
$this->expectException(\ErrorException::class); |
228
|
|
|
$this->expectExceptionMessage($expectedException); |
229
|
|
|
$timeZone = $this->calculator->getTimeZoneName($latitude, $longitude); |
|
|
|
|
230
|
|
|
} catch (Exception $error) { |
|
|
|
|
231
|
|
|
echo $error->getMessage(); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @dataProvider getDataMaxLongitude |
237
|
|
|
* @param $latitude |
238
|
|
|
* @param $longitude |
239
|
|
|
* @param $adjustedLongitude |
240
|
|
|
* @param $expectedException |
241
|
|
|
*/ |
242
|
|
|
public function testGetTimeZoneNameWithMaxLongitude($latitude, $longitude, $adjustedLongitude, $expectedException) |
|
|
|
|
243
|
|
|
{ |
244
|
|
|
try { |
245
|
|
|
$this->expectException(\ErrorException::class); |
246
|
|
|
$this->expectExceptionMessage($expectedException); |
247
|
|
|
$timeZone = $this->calculator->getTimeZoneName($latitude, $longitude); |
|
|
|
|
248
|
|
|
} catch (Exception $error) { |
|
|
|
|
249
|
|
|
echo $error->getMessage(); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @dataProvider getDataMaxLatitude |
255
|
|
|
* @param $latitude |
256
|
|
|
* @param $longitude |
257
|
|
|
* @param $adjustedLatitude |
258
|
|
|
* @param $expectedException |
259
|
|
|
*/ |
260
|
|
|
public function testAdjustLatitudeWithMaxLatitude($latitude, $longitude, $adjustedLatitude, $expectedException) |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
$method = $this->getPrivateMethod(get_class($this->calculator), 'adjustLatitude'); |
263
|
|
|
$latitudeToTest = $method->invokeArgs($this->calculator, array($latitude)); |
264
|
|
|
$this->assertEquals($adjustedLatitude, $latitudeToTest); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @dataProvider getDataMaxLongitude |
269
|
|
|
* @param $latitude |
270
|
|
|
* @param $longitude |
271
|
|
|
* @param $adjustedLongitude |
272
|
|
|
* @param $expectedException |
273
|
|
|
*/ |
274
|
|
|
public function testAdjustMaxLongitudeWithMaxLongitude($latitude, $longitude, $adjustedLongitude, |
|
|
|
|
275
|
|
|
$expectedException) |
|
|
|
|
276
|
|
|
{ |
277
|
|
|
$method = $this->getPrivateMethod(get_class($this->calculator), 'adjustLongitude'); |
278
|
|
|
$longitudeToTest = $method->invokeArgs($this->calculator, array($longitude)); |
279
|
|
|
$this->assertEquals($adjustedLongitude, $longitudeToTest); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @dataProvider getDataLocalDate |
284
|
|
|
* @param $latitude |
285
|
|
|
* @param $longitude |
286
|
|
|
* @param $timestamp |
287
|
|
|
* @param $expectedTimeZone |
288
|
|
|
* @param $expectedOffset |
289
|
|
|
*/ |
290
|
|
|
public function testGetLocalDate($latitude, $longitude, $timestamp, $expectedTimeZone, $expectedOffset) |
291
|
|
|
{ |
292
|
|
|
$localDate = $this->calculator->getLocalDate($latitude, $longitude, $timestamp); |
293
|
|
|
$this->assertInstanceOf('DateTime', $localDate); |
294
|
|
|
echo $localDate->getTimezone()->getName() . "\n"; |
295
|
|
|
$this->assertEquals( |
296
|
|
|
$localDate->getTimezone()->getName(), |
297
|
|
|
$expectedTimeZone |
298
|
|
|
); |
299
|
|
|
$this->assertEquals( |
300
|
|
|
$localDate->getOffset(), |
301
|
|
|
$expectedOffset |
302
|
|
|
); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @dataProvider getNoTimezone |
307
|
|
|
* @param $latitude |
308
|
|
|
* @param $longitude |
309
|
|
|
* @param $timestamp |
310
|
|
|
* @param $expectedException |
311
|
|
|
*/ |
312
|
|
|
public function testGetLocalDateException($latitude, $longitude, $timestamp, $expectedException) |
313
|
|
|
{ |
314
|
|
|
try { |
315
|
|
|
$this->expectException(\ErrorException::class); |
316
|
|
|
$this->expectExceptionMessage($expectedException); |
317
|
|
|
$localDate = $this->calculator->getLocalDate($latitude, $longitude, $timestamp); |
|
|
|
|
318
|
|
|
} catch (Exception $error) { |
|
|
|
|
319
|
|
|
echo $error->getMessage(); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @dataProvider getDataCorrectTimestamp |
325
|
|
|
* @param $latitude |
326
|
|
|
* @param $longitude |
327
|
|
|
* @param $timestamp |
328
|
|
|
* @param $expectedTimestamp |
329
|
|
|
*/ |
330
|
|
|
public function testGetCorrectTimestamp($latitude, $longitude, $timestamp, $expectedTimestamp) |
331
|
|
|
{ |
332
|
|
|
$correctTimestamp = $this->calculator->getCorrectTimestamp($latitude, $longitude, $timestamp); |
333
|
|
|
$this->assertEquals($correctTimestamp, $expectedTimestamp); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @dataProvider getNoTimezone |
338
|
|
|
* @param $latitude |
339
|
|
|
* @param $longitude |
340
|
|
|
* @param $timestamp |
341
|
|
|
* @param $expectedException |
342
|
|
|
*/ |
343
|
|
|
public function testGetCorrectTimestampException($latitude, $longitude, $timestamp, $expectedException) |
344
|
|
|
{ |
345
|
|
|
try { |
346
|
|
|
$this->expectException(\ErrorException::class); |
347
|
|
|
$this->expectExceptionMessage($expectedException); |
348
|
|
|
$correctTimestamp = $this->calculator->getCorrectTimestamp($latitude, $longitude, $timestamp); |
|
|
|
|
349
|
|
|
} catch (Exception $error) { |
|
|
|
|
350
|
|
|
echo $error->getMessage(); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @dataProvider getDataTimeZoneName |
356
|
|
|
* @param $latitude |
357
|
|
|
* @param $longitude |
358
|
|
|
* @param $expectedTimeZone |
359
|
|
|
*/ |
360
|
|
|
public function testGetTimeZoneName($latitude, $longitude, $expectedTimeZone) |
361
|
|
|
{ |
362
|
|
|
try { |
363
|
|
|
$timeZoneName = $this->calculator->getTimeZoneName($latitude, $longitude); |
364
|
|
|
$this->assertEquals($timeZoneName, $expectedTimeZone); |
365
|
|
|
} catch (Exception $error) { |
|
|
|
|
366
|
|
|
echo $error->getMessage(); |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @dataProvider getNoTimezone |
372
|
|
|
* @param $latitude |
373
|
|
|
* @param $longitude |
374
|
|
|
* @param $timestamp |
375
|
|
|
* @param $expectedException |
376
|
|
|
*/ |
377
|
|
|
public function testGetTimeZoneNameException($latitude, $longitude, $timestamp, $expectedException) |
|
|
|
|
378
|
|
|
{ |
379
|
|
|
try { |
380
|
|
|
$this->expectException(\ErrorException::class); |
381
|
|
|
$this->expectExceptionMessage($expectedException); |
382
|
|
|
$timeZoneName = $this->calculator->getTimeZoneName($latitude, $longitude); |
|
|
|
|
383
|
|
|
} catch (Exception $error) { |
|
|
|
|
384
|
|
|
echo $error->getMessage(); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.