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\Exception; |
19
|
|
|
use Cmfcmf\OpenWeatherMap\Tests\TestHttpClient; |
20
|
|
|
use Cache\Adapter\PHPArray\ArrayCachePool; |
21
|
|
|
use Http\Factory\Guzzle\RequestFactory; |
22
|
|
|
use Http\Adapter\Guzzle6\Client as GuzzleAdapter; |
23
|
|
|
use Psr\SimpleCache\CacheInterface; |
24
|
|
|
|
25
|
|
|
class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $apiKey; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var OpenWeatherMap |
34
|
|
|
*/ |
35
|
|
|
protected $owm; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var OpenWeatherMap |
39
|
|
|
*/ |
40
|
|
|
protected $openWeather; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var CacheInterface |
44
|
|
|
*/ |
45
|
|
|
protected $cache; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var TestHttpClient |
49
|
|
|
*/ |
50
|
|
|
protected $httpClient; |
51
|
|
|
|
52
|
|
|
protected function setUp() |
53
|
|
|
{ |
54
|
|
|
$ini = parse_ini_file(__DIR__.'/../Examples/ApiKey.ini'); |
55
|
|
|
$this->apiKey = $ini['api_key']; |
56
|
|
|
$this->httpClient = new TestHttpClient(); |
57
|
|
|
$this->owm = new OpenWeatherMap($this->apiKey, $this->httpClient, new RequestFactory()); |
58
|
|
|
$this->openWeather = new OpenWeatherMap($this->apiKey, GuzzleAdapter::createWithConfig([]), new RequestFactory()); |
59
|
|
|
$this->cache = new ArrayCachePool(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testApiKeyNotNull() |
63
|
|
|
{ |
64
|
|
|
$weather = $this->owm; |
65
|
|
|
$apiKey = $weather->getApiKey(); |
66
|
|
|
|
67
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testSetApiKey() |
71
|
|
|
{ |
72
|
|
|
$weather = $this->owm; |
73
|
|
|
$weather->setApiKey($this->apiKey); |
74
|
|
|
$apiKey = $weather->getApiKey(); |
75
|
|
|
|
76
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testGetApiKey() |
80
|
|
|
{ |
81
|
|
|
$weather = $this->owm; |
82
|
|
|
$apiKey = $weather->getApiKey(); |
83
|
|
|
|
84
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @expectedException \Cmfcmf\OpenWeatherMap\Exception |
89
|
|
|
*/ |
90
|
|
|
public function testInvalidData() |
91
|
|
|
{ |
92
|
|
|
$this->httpClient->returnErrorForNextRequest(500); |
93
|
|
|
$this->owm->getWeather('Berlin', 'imperial', 'en', ''); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testGetWeather() |
97
|
|
|
{ |
98
|
|
|
$currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', ''); |
99
|
|
|
|
100
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testGetWeatherGroup() |
104
|
|
|
{ |
105
|
|
|
$currentWeather = $this->owm->getWeatherGroup(array('2950159'), 'imperial', 'en', ''); |
106
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather); |
107
|
|
|
|
108
|
|
|
$currentWeather = $this->owm->getWeatherGroup('2950159', 'imperial', 'en', ''); |
|
|
|
|
109
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testGetWeatherForecast() |
113
|
|
|
{ |
114
|
|
|
$days = 1; |
115
|
|
|
$defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
116
|
|
|
|
117
|
|
|
$days = 16; |
118
|
|
|
$maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
119
|
|
|
|
120
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay); |
121
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testGetCurrentUVIndex() |
125
|
|
|
{ |
126
|
|
|
$owm = $this->openWeather; |
127
|
|
|
$result = $owm->getCurrentUVIndex(40.7, -74.2); |
128
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testGetForecastUVIndex() |
132
|
|
|
{ |
133
|
|
|
$owm = $this->openWeather; |
134
|
|
|
|
135
|
|
|
try { |
136
|
|
|
$result = $owm->getForecastUVIndex(40.7, -74.2, 5); |
137
|
|
|
} catch (Exception $e) { |
138
|
|
|
// OWM might not actually have data for the timespan. |
139
|
|
|
$this->assertSame('An error occurred: not found', $e->getMessage()); |
140
|
|
|
} |
141
|
|
|
$this->assertContainsOnlyInstancesOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testGetHistoryUVIndex() |
145
|
|
|
{ |
146
|
|
|
$owm = $this->openWeather; |
147
|
|
|
|
148
|
|
|
try { |
149
|
|
|
$start = new \DateTime('1969-08-15'); |
150
|
|
|
$end = new \DateTime('1969-08-18'); |
151
|
|
|
$result = $owm->getHistoricUVIndex(40.7, -74.2, $start, $end); |
152
|
|
|
} catch (Exception $e) { |
153
|
|
|
// OWM might not actually have data for the timespan. |
154
|
|
|
$this->assertSame('An error occurred: not found', $e->getMessage()); |
155
|
|
|
} |
156
|
|
|
$this->assertContainsOnlyInstancesOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testGetDailyWeatherForecast() |
160
|
|
|
{ |
161
|
|
|
$days = 16; |
162
|
|
|
$dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
163
|
|
|
|
164
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testGetWeatherHistory() |
168
|
|
|
{ |
169
|
|
|
$this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testWasCached() |
173
|
|
|
{ |
174
|
|
|
$weather = $this->owm; |
175
|
|
|
$result = $weather->wasCached(); |
176
|
|
|
|
177
|
|
|
$this->assertFalse($result); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testCached() |
181
|
|
|
{ |
182
|
|
|
$weather = new OpenWeatherMap($this->apiKey, new TestHttpClient(), new RequestFactory(), $this->cache, 600); |
|
|
|
|
183
|
|
|
$currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml'); |
184
|
|
|
$this->assertFalse($weather->wasCached()); |
185
|
|
|
$cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml'); |
186
|
|
|
$this->assertTrue($weather->wasCached()); |
187
|
|
|
|
188
|
|
|
$this->assertInternalType('string', $currWeatherData); |
189
|
|
|
$this->assertInternalType('string', $cachedWeatherData); |
190
|
|
|
$this->assertSame($currWeatherData, $cachedWeatherData); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testBuildQueryUrlParameter() |
194
|
|
|
{ |
195
|
|
|
$weather = $this->owm; |
196
|
|
|
$queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', ''); |
197
|
|
|
$queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', ''); |
198
|
|
|
|
199
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray); |
200
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
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: