1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* |
8
|
|
|
* Please see the LICENSE file distributed with this source code for further |
9
|
|
|
* information regarding copyright and licensing. |
10
|
|
|
* |
11
|
|
|
* Please visit the following links to read about the usage policies and the license of |
12
|
|
|
* OpenWeatherMap data before using this library: |
13
|
|
|
* |
14
|
|
|
* @see https://OpenWeatherMap.org/price |
15
|
|
|
* @see https://OpenWeatherMap.org/terms |
16
|
|
|
* @see https://OpenWeatherMap.org/appid |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Cmfcmf\OpenWeatherMap\Tests; |
20
|
|
|
|
21
|
|
|
use Cmfcmf\OpenWeatherMap; |
22
|
|
|
use Cmfcmf\OpenWeatherMap\Exception; |
23
|
|
|
use Cmfcmf\OpenWeatherMap\Tests\TestHttpClient; |
24
|
|
|
use Cache\Adapter\PHPArray\ArrayCachePool; |
25
|
|
|
use Http\Factory\Guzzle\RequestFactory; |
26
|
|
|
use Http\Adapter\Guzzle6\Client as GuzzleAdapter; |
27
|
|
|
use Psr\SimpleCache\CacheInterface; |
28
|
|
|
|
29
|
|
|
class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $apiKey; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var OpenWeatherMap |
38
|
|
|
*/ |
39
|
|
|
protected $owm; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var OpenWeatherMap |
43
|
|
|
*/ |
44
|
|
|
protected $openWeather; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var CacheInterface |
48
|
|
|
*/ |
49
|
|
|
protected $cache; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var TestHttpClient |
53
|
|
|
*/ |
54
|
|
|
protected $httpClient; |
55
|
|
|
|
56
|
|
|
protected function setUp() |
57
|
|
|
{ |
58
|
|
|
$ini = parse_ini_file(__DIR__.'/../Examples/ApiKey.ini'); |
59
|
|
|
$this->apiKey = $ini['api_key']; |
60
|
|
|
$this->httpClient = new TestHttpClient(); |
61
|
|
|
$this->owm = new OpenWeatherMap($this->apiKey, $this->httpClient, new RequestFactory()); |
62
|
|
|
$this->openWeather = new OpenWeatherMap($this->apiKey, GuzzleAdapter::createWithConfig([]), new RequestFactory()); |
63
|
|
|
$this->cache = new ArrayCachePool(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testApiKeyNotNull() |
67
|
|
|
{ |
68
|
|
|
$weather = $this->owm; |
69
|
|
|
$apiKey = $weather->getApiKey(); |
70
|
|
|
|
71
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testSetApiKey() |
75
|
|
|
{ |
76
|
|
|
$weather = $this->owm; |
77
|
|
|
$weather->setApiKey($this->apiKey); |
78
|
|
|
$apiKey = $weather->getApiKey(); |
79
|
|
|
|
80
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testGetApiKey() |
84
|
|
|
{ |
85
|
|
|
$weather = $this->owm; |
86
|
|
|
$apiKey = $weather->getApiKey(); |
87
|
|
|
|
88
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @expectedException \Cmfcmf\OpenWeatherMap\Exception |
93
|
|
|
*/ |
94
|
|
|
public function testInvalidData() |
95
|
|
|
{ |
96
|
|
|
$this->httpClient->returnErrorForNextRequest(500); |
97
|
|
|
$this->owm->getWeather('Berlin', 'imperial', 'en', ''); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGetWeather() |
101
|
|
|
{ |
102
|
|
|
$currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', ''); |
103
|
|
|
|
104
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGetWeatherGroup() |
108
|
|
|
{ |
109
|
|
|
$currentWeather = $this->owm->getWeatherGroup(array('2950159'), 'imperial', 'en', ''); |
110
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather); |
111
|
|
|
|
112
|
|
|
$currentWeather = $this->owm->getWeatherGroup('2950159', 'imperial', 'en', ''); |
|
|
|
|
113
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testGetWeatherForecast() |
117
|
|
|
{ |
118
|
|
|
$days = 1; |
119
|
|
|
$defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
120
|
|
|
|
121
|
|
|
$days = 16; |
122
|
|
|
$maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
123
|
|
|
|
124
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay); |
125
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGetCurrentUVIndex() |
129
|
|
|
{ |
130
|
|
|
$owm = $this->openWeather; |
131
|
|
|
$result = $owm->getCurrentUVIndex(40.7, -74.2); |
132
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testGetForecastUVIndex() |
136
|
|
|
{ |
137
|
|
|
$owm = $this->openWeather; |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
$result = $owm->getForecastUVIndex(40.7, -74.2, 5); |
141
|
|
|
} catch (Exception $e) { |
142
|
|
|
// OWM might not actually have data for the timespan. |
143
|
|
|
$this->assertSame('An error occurred: not found', $e->getMessage()); |
144
|
|
|
} |
145
|
|
|
$this->assertContainsOnlyInstancesOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testGetHistoryUVIndex() |
149
|
|
|
{ |
150
|
|
|
$owm = $this->openWeather; |
151
|
|
|
|
152
|
|
|
try { |
153
|
|
|
$start = new \DateTime('1969-08-15'); |
154
|
|
|
$end = new \DateTime('1969-08-18'); |
155
|
|
|
$result = $owm->getHistoricUVIndex(40.7, -74.2, $start, $end); |
156
|
|
|
} catch (Exception $e) { |
157
|
|
|
// OWM might not actually have data for the timespan. |
158
|
|
|
$this->assertSame('An error occurred: not found', $e->getMessage()); |
159
|
|
|
} |
160
|
|
|
$this->assertContainsOnlyInstancesOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testGetDailyWeatherForecast() |
164
|
|
|
{ |
165
|
|
|
$days = 16; |
166
|
|
|
$dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
167
|
|
|
|
168
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testGetAirPollution() |
172
|
|
|
{ |
173
|
|
|
$airPollutionCurrent = $this->owm->getAirPollution("CO", "40", "-74", "current"); |
174
|
|
|
$this->assertInstanceOf(OpenWeatherMap\AirPollution\COAirPollution::class, $airPollutionCurrent); |
175
|
|
|
$airPollutionPast = $this->owm->getAirPollution("CO", "40", "-74", "2016Z"); |
176
|
|
|
$this->assertInstanceOf(OpenWeatherMap\AirPollution\COAirPollution::class, $airPollutionPast); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testWasCached() |
180
|
|
|
{ |
181
|
|
|
$weather = $this->owm; |
182
|
|
|
$result = $weather->wasCached(); |
183
|
|
|
|
184
|
|
|
$this->assertFalse($result); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testCached() |
188
|
|
|
{ |
189
|
|
|
$weather = new OpenWeatherMap($this->apiKey, new TestHttpClient(), new RequestFactory(), $this->cache, 600); |
|
|
|
|
190
|
|
|
$currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml'); |
191
|
|
|
$this->assertFalse($weather->wasCached()); |
192
|
|
|
$cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml'); |
193
|
|
|
$this->assertTrue($weather->wasCached()); |
194
|
|
|
|
195
|
|
|
$this->assertInternalType('string', $currWeatherData); |
196
|
|
|
$this->assertInternalType('string', $cachedWeatherData); |
197
|
|
|
$this->assertSame($currWeatherData, $cachedWeatherData); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testBuildQueryUrlParameter() |
201
|
|
|
{ |
202
|
|
|
$weather = $this->owm; |
203
|
|
|
$queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', ''); |
204
|
|
|
$queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', ''); |
205
|
|
|
|
206
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray); |
207
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: