|
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
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getDataCorrectTimestamp() |
|
41
|
|
|
{ |
|
42
|
|
|
return array( |
|
43
|
|
|
'Testing Lisbon' => array( |
|
44
|
|
|
'latitude' => 41.142700, |
|
45
|
|
|
'longitude' => -8.612150, |
|
46
|
|
|
'timestamp' => 1458844434, |
|
47
|
|
|
'expectedTimestamp' => 1458844434, |
|
48
|
|
|
), |
|
49
|
|
|
'Testing Madrid' => array( |
|
50
|
|
|
'latitude' => 39.452800, |
|
51
|
|
|
'longitude' => -0.347038, |
|
52
|
|
|
'timestamp' => 1469387760, |
|
53
|
|
|
'expectedTimestamp' => 1469380560, |
|
54
|
|
|
) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getDataTimeZoneName() |
|
59
|
|
|
{ |
|
60
|
|
|
return array( |
|
61
|
|
|
'Testing Lisbon' => array( |
|
62
|
|
|
'latitude' => 41.142700, |
|
63
|
|
|
'longitude' => -8.612150, |
|
64
|
|
|
'expectedTimeZone' => 'Europe/Lisbon', |
|
65
|
|
|
), |
|
66
|
|
|
'Testing Madrid' => array( |
|
67
|
|
|
'latitude' => 39.452800, |
|
68
|
|
|
'longitude' => -0.347038, |
|
69
|
|
|
'expectedTimeZone' => 'Europe/Madrid', |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getDataWrongLatitude() |
|
75
|
|
|
{ |
|
76
|
|
|
return array( |
|
77
|
|
|
'Testing Wrong Latitude' => array( |
|
78
|
|
|
'latitude' => 10000000, |
|
79
|
|
|
'longitude' => -8.612150 |
|
80
|
|
|
), |
|
81
|
|
|
'Testing Null Latitude' => array( |
|
82
|
|
|
'latitude' => null, |
|
83
|
|
|
'longitude' => -8.612150 |
|
84
|
|
|
) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getDataWrongLongitude() |
|
89
|
|
|
{ |
|
90
|
|
|
return array( |
|
91
|
|
|
'Testing Wrong Longitude' => array( |
|
92
|
|
|
'latitude' => 41.142700, |
|
93
|
|
|
'longitude' => 10000000, |
|
94
|
|
|
|
|
95
|
|
|
), |
|
96
|
|
|
'Testing Null Longitude' => array( |
|
97
|
|
|
'latitude' => 41.142700, |
|
98
|
|
|
'longitude' => null, |
|
99
|
|
|
) |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getDataMaxLatitude() |
|
104
|
|
|
{ |
|
105
|
|
|
return array( |
|
106
|
|
|
'Testing Positive Max Latitude' => array( |
|
107
|
|
|
'latitude' => 90.0, |
|
108
|
|
|
'longitude' => -8.612150, |
|
109
|
|
|
'adjustedLatitude' => 89.9999 |
|
110
|
|
|
), |
|
111
|
|
|
'Testing Negative Max Latitude' => array( |
|
112
|
|
|
'latitude' => -90.0, |
|
113
|
|
|
'longitude' => -8.612150, |
|
114
|
|
|
'adjustedLatitude' => -89.9999 |
|
115
|
|
|
) |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getDataMaxLongitude() |
|
120
|
|
|
{ |
|
121
|
|
|
return array( |
|
122
|
|
|
'Testing Positive Max Longitude' => array( |
|
123
|
|
|
'latitude' => -8.612150, |
|
124
|
|
|
'longitude' => 180.0, |
|
125
|
|
|
'adjustedLongitude' => 179.9999 |
|
126
|
|
|
), |
|
127
|
|
|
'Testing Negative Max Longitude' => array( |
|
128
|
|
|
'latitude' => -8.612150, |
|
129
|
|
|
'longitude' => -180.0, |
|
130
|
|
|
'adjustedLongitude' => -179.9999 |
|
131
|
|
|
) |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @dataProvider getDataWrongLatitude |
|
137
|
|
|
* @param $latitude |
|
138
|
|
|
* @param $longitude |
|
139
|
|
|
*/ |
|
140
|
|
|
public function testGetTimeZoneNameWithWrongLatitude($latitude, $longitude) |
|
141
|
|
|
{ |
|
142
|
|
|
try { |
|
143
|
|
|
$timeZone = $this->calculator->getTimeZoneName($latitude, $longitude); |
|
144
|
|
|
$this->assertEquals($timeZone, "none"); |
|
145
|
|
|
} catch (Exception $error) { |
|
146
|
|
|
$this->expectException(\ErrorException::class); |
|
147
|
|
|
$this->expectExceptionMessage("Invalid latitude: {$latitude}"); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @dataProvider getDataWrongLongitude |
|
153
|
|
|
* @param $latitude |
|
154
|
|
|
* @param $longitude |
|
155
|
|
|
*/ |
|
156
|
|
|
public function testGetTimeZoneNameWithWrongLongitude($latitude, $longitude) |
|
157
|
|
|
{ |
|
158
|
|
|
try { |
|
159
|
|
|
$timeZone = $this->calculator->getTimeZoneName($latitude, $longitude); |
|
160
|
|
|
$this->assertEquals($timeZone, "none"); |
|
161
|
|
|
} catch (Exception $error) { |
|
162
|
|
|
$this->expectException(\ErrorException::class); |
|
163
|
|
|
$this->expectExceptionMessage("Invalid longitude: {$longitude}"); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @dataProvider getDataMaxLatitude |
|
169
|
|
|
* @param $latitude |
|
170
|
|
|
* @param $longitude |
|
171
|
|
|
*/ |
|
172
|
|
|
public function testGetTimeZoneNameWithMaxLatitude($latitude, $longitude) |
|
173
|
|
|
{ |
|
174
|
|
|
$timeZone = $this->calculator->getTimeZoneName($latitude, $longitude); |
|
175
|
|
|
$this->assertTrue(is_string($timeZone)); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @dataProvider getDataMaxLongitude |
|
180
|
|
|
* @param $latitude |
|
181
|
|
|
* @param $longitude |
|
182
|
|
|
*/ |
|
183
|
|
|
public function testGetTimeZoneNameWithMaxLongitude($latitude, $longitude) |
|
184
|
|
|
{ |
|
185
|
|
|
$timeZone = $this->calculator->getTimeZoneName($latitude, $longitude); |
|
186
|
|
|
$this->assertTrue(is_string($timeZone)); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @dataProvider getDataMaxLatitude |
|
191
|
|
|
* @param $latitude |
|
192
|
|
|
* @param $longitude |
|
193
|
|
|
* @param $adjustedLatitude |
|
194
|
|
|
*/ |
|
195
|
|
|
public function testAdjustLatitudeWithMaxLatitude($latitude, $longitude, $adjustedLatitude) |
|
|
|
|
|
|
196
|
|
|
{ |
|
197
|
|
|
$method = $this->getPrivateMethod(get_class($this->calculator), 'adjustLatitude'); |
|
198
|
|
|
$latitudeToTest = $method->invokeArgs($this->calculator, array($latitude)); |
|
199
|
|
|
$this->assertEquals($adjustedLatitude, $latitudeToTest); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @dataProvider getDataMaxLongitude |
|
204
|
|
|
* @param $latitude |
|
205
|
|
|
* @param $longitude |
|
206
|
|
|
* @param $adjustedLongitude |
|
207
|
|
|
*/ |
|
208
|
|
|
public function testAdjustMaxLongitudeWithMaxLongitude($latitude, $longitude, $adjustedLongitude) |
|
|
|
|
|
|
209
|
|
|
{ |
|
210
|
|
|
$method = $this->getPrivateMethod(get_class($this->calculator), 'adjustLongitude'); |
|
211
|
|
|
$longitudeToTest = $method->invokeArgs($this->calculator, array($longitude)); |
|
212
|
|
|
$this->assertEquals($adjustedLongitude, $longitudeToTest); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @dataProvider getDataLocalDate |
|
217
|
|
|
* @param $latitude |
|
218
|
|
|
* @param $longitude |
|
219
|
|
|
* @param $timestamp |
|
220
|
|
|
* @param $expectedTimeZone |
|
221
|
|
|
* @param $expectedOffset |
|
222
|
|
|
*/ |
|
223
|
|
|
public function testGetLocalDate($latitude, $longitude, $timestamp, $expectedTimeZone, $expectedOffset) |
|
224
|
|
|
{ |
|
225
|
|
|
$localDate = $this->calculator->getLocalDate($latitude, $longitude, $timestamp); |
|
226
|
|
|
|
|
227
|
|
|
$this->assertInstanceOf('DateTime', $localDate); |
|
228
|
|
|
$this->assertEquals( |
|
229
|
|
|
$localDate->getTimezone()->getName(), |
|
230
|
|
|
$expectedTimeZone |
|
231
|
|
|
); |
|
232
|
|
|
$this->assertEquals( |
|
233
|
|
|
$localDate->getOffset(), |
|
234
|
|
|
$expectedOffset |
|
235
|
|
|
); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @dataProvider getDataCorrectTimestamp |
|
240
|
|
|
* @param $latitude |
|
241
|
|
|
* @param $longitude |
|
242
|
|
|
* @param $timestamp |
|
243
|
|
|
* @param $expectedTimestamp |
|
244
|
|
|
*/ |
|
245
|
|
|
public function testGetCorrectTimestamp($latitude, $longitude, $timestamp, $expectedTimestamp) |
|
246
|
|
|
{ |
|
247
|
|
|
$correctTimestamp = $this->calculator->getCorrectTimestamp($latitude, $longitude, $timestamp); |
|
248
|
|
|
$this->assertEquals($correctTimestamp, $expectedTimestamp); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @dataProvider getDataTimeZoneName |
|
253
|
|
|
* @param $latitude |
|
254
|
|
|
* @param $longitude |
|
255
|
|
|
* @param $expectedTimeZone |
|
256
|
|
|
*/ |
|
257
|
|
|
public function testGetTimeZoneName($latitude, $longitude, $expectedTimeZone) |
|
258
|
|
|
{ |
|
259
|
|
|
$timeZoneName = $this->calculator->getTimeZoneName($latitude, $longitude); |
|
260
|
|
|
$this->assertEquals($timeZoneName, $expectedTimeZone); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.