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