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