1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright Zikula Foundation 2014 - Zikula Application Framework |
4
|
|
|
* |
5
|
|
|
* This work is contributed to the Zikula Foundation under one or more |
6
|
|
|
* Contributor Agreements and licensed to You under the following license: |
7
|
|
|
* |
8
|
|
|
* @license GNU/LGPv3 (or at your option any later version). |
9
|
|
|
* @package OpenWeatherMap-PHP-Api |
10
|
|
|
* |
11
|
|
|
* Please see the NOTICE file distributed with this source code for further |
12
|
|
|
* information regarding copyright and licensing. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap; |
16
|
|
|
|
17
|
|
|
use \Cmfcmf\OpenWeatherMap; |
18
|
|
|
use Cmfcmf\OpenWeatherMap\Tests\TestFetcher; |
19
|
|
|
use \Cmfcmf\OpenWeatherMap\WeatherHistory; |
20
|
|
|
use \Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap\ExampleCacheTest; |
21
|
|
|
|
22
|
|
|
class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $apiKey; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var OpenWeatherMap |
31
|
|
|
*/ |
32
|
|
|
protected $owm; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var OpenWeatherMap |
36
|
|
|
*/ |
37
|
|
|
protected $openWeather; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ExampleCacheTest |
41
|
|
|
*/ |
42
|
|
|
protected $cache; |
43
|
|
|
|
44
|
|
|
protected function setUp() |
45
|
|
|
{ |
46
|
|
|
$ini = parse_ini_file(__DIR__.'/../Examples/ApiKey.ini'); |
47
|
|
|
$myApiKey = $ini['api_key']; |
48
|
|
|
$this->apiKey = $myApiKey; |
49
|
|
|
$this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600); |
50
|
|
|
$this->openWeather = new OpenWeatherMap($this->apiKey, null, false, 600); |
51
|
|
|
$this->cache = new ExampleCacheTest(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function tearDown() |
55
|
|
|
{ |
56
|
|
|
$fileList = glob(__DIR__.'/temps/OpenWeatherMapPHPAPI/*'); |
57
|
|
|
foreach ($fileList as $fileName) { |
58
|
|
|
@unlink($fileName); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
@rmdir(__DIR__.'/temps/OpenWeatherMapPHPAPI'); |
|
|
|
|
62
|
|
|
@rmdir(__DIR__.'/temps'); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testApiKeyIsEmpty() |
66
|
|
|
{ |
67
|
|
|
$expectApiKey = ''; |
68
|
|
|
$weather = new OpenWeatherMap($expectApiKey, null, false, 600); |
69
|
|
|
$apiKey = $weather->getApiKey(); |
70
|
|
|
|
71
|
|
|
$this->assertSame($expectApiKey, $apiKey); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testApiKeyNotNull() |
75
|
|
|
{ |
76
|
|
|
$weather = $this->owm; |
77
|
|
|
$apiKey = $weather->getApiKey(); |
78
|
|
|
|
79
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testSecondIsZero() |
83
|
|
|
{ |
84
|
|
|
$weather = new OpenWeatherMap($this->apiKey, null, false, 0); |
85
|
|
|
$apiKey = $weather->getApiKey(); |
86
|
|
|
|
87
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testSetApiKey() |
91
|
|
|
{ |
92
|
|
|
$weather = $this->owm; |
93
|
|
|
$weather->setApiKey($this->apiKey); |
94
|
|
|
$apiKey = $weather->getApiKey(); |
95
|
|
|
|
96
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testGetApiKey() |
100
|
|
|
{ |
101
|
|
|
$weather = $this->owm; |
102
|
|
|
$apiKey = $weather->getApiKey(); |
103
|
|
|
|
104
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGetWeather() |
108
|
|
|
{ |
109
|
|
|
$currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', ''); |
110
|
|
|
|
111
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testGetWeatherGroup() |
115
|
|
|
{ |
116
|
|
|
$currentWeather = $this->owm->getWeatherGroup('2950159', 'imperial', 'en', ''); |
|
|
|
|
117
|
|
|
|
118
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testGetWeatherForecast() |
122
|
|
|
{ |
123
|
|
|
$days = 1; |
124
|
|
|
$defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
125
|
|
|
|
126
|
|
|
$days = 16; |
127
|
|
|
$maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
128
|
|
|
|
129
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay); |
130
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testGetUvi() |
134
|
|
|
{ |
135
|
|
|
$weather = $this->openWeather; |
136
|
|
|
$locationArray = array('40.7', '-74.2'); |
137
|
|
|
$result = $weather->getUvi($locationArray); |
138
|
|
|
|
139
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentUvi', $result); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testGetUviHistory() |
143
|
|
|
{ |
144
|
|
|
$weather = $this->openWeather; |
145
|
|
|
$year = date('Y') . 'Z'; |
146
|
|
|
$locationArray = array('40.7', '-74.2', $year); |
147
|
|
|
$result = $weather->getUviHistory($locationArray); |
148
|
|
|
|
149
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentUvi', $result); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testGetUviHistoryWithNull() |
153
|
|
|
{ |
154
|
|
|
$weather = $this->openWeather; |
155
|
|
|
$locationArray = array('40.7', '-74.2', 'invalid-date-format'); |
156
|
|
|
$result = $weather->getUviHistory($locationArray); |
157
|
|
|
|
158
|
|
|
$this->assertNull($result->uvi); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testGetDailyWeatherForecast() |
162
|
|
|
{ |
163
|
|
|
$days = 16; |
164
|
|
|
$dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
165
|
|
|
|
166
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testGetWeatherHistory() |
170
|
|
|
{ |
171
|
|
|
// @TODO!! |
172
|
|
|
$this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testWasCached() |
176
|
|
|
{ |
177
|
|
|
$weather = $this->owm; |
178
|
|
|
$result = $weather->wasCached(); |
179
|
|
|
|
180
|
|
|
$this->assertFalse($result); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testCached() |
184
|
|
|
{ |
185
|
|
|
$cache = $this->cache; |
186
|
|
|
$cache->setTempPath(__DIR__.'/temps'); |
187
|
|
|
$weather = new OpenWeatherMap($this->apiKey, new TestFetcher(), $cache, 600); |
|
|
|
|
188
|
|
|
$currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml'); |
189
|
|
|
$cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml'); |
190
|
|
|
|
191
|
|
|
$this->assertInternalType('string', $currWeatherData); |
192
|
|
|
$this->assertInternalType('string', $cachedWeatherData); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testBuildQueryUrlParameter() |
196
|
|
|
{ |
197
|
|
|
$weather = $this->owm; |
198
|
|
|
$queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', ''); |
199
|
|
|
$queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', ''); |
200
|
|
|
|
201
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray); |
202
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testAbstractCache() |
206
|
|
|
{ |
207
|
|
|
$sut = $this->getMockForAbstractClass('\Cmfcmf\OpenWeatherMap\AbstractCache'); |
208
|
|
|
$this->assertNull($sut->setSeconds(10)); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: