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 ExampleCacheTest |
36
|
|
|
*/ |
37
|
|
|
protected $cache; |
38
|
|
|
|
39
|
|
|
protected function setUp() |
40
|
|
|
{ |
41
|
|
|
$this->apiKey = 'unicorn-rainbow'; |
42
|
|
|
$this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600); |
43
|
|
|
$this->cache = new ExampleCacheTest(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function tearDown() |
47
|
|
|
{ |
48
|
|
|
$fileList = glob(__DIR__.'/temps/OpenWeatherMapPHPAPI/*'); |
49
|
|
|
foreach ($fileList as $fileName) { |
50
|
|
|
@unlink($fileName); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
@rmdir(__DIR__.'/temps/OpenWeatherMapPHPAPI'); |
|
|
|
|
54
|
|
|
@rmdir(__DIR__.'/temps'); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testApiKeyIsEmpty() |
58
|
|
|
{ |
59
|
|
|
$expectApiKey = ''; |
60
|
|
|
$weather = new OpenWeatherMap($expectApiKey, null, false, 600); |
61
|
|
|
$apiKey = $weather->getApiKey(); |
62
|
|
|
|
63
|
|
|
$this->assertSame($expectApiKey, $apiKey); |
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 testSecondIsZero() |
75
|
|
|
{ |
76
|
|
|
$weather = new OpenWeatherMap($this->apiKey, null, false, 0); |
77
|
|
|
$apiKey = $weather->getApiKey(); |
78
|
|
|
|
79
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testSetApiKey() |
83
|
|
|
{ |
84
|
|
|
$weather = $this->owm; |
85
|
|
|
$weather->setApiKey($this->apiKey); |
86
|
|
|
$apiKey = $weather->getApiKey(); |
87
|
|
|
|
88
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testGetApiKey() |
92
|
|
|
{ |
93
|
|
|
$weather = $this->owm; |
94
|
|
|
$apiKey = $weather->getApiKey(); |
95
|
|
|
|
96
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testGetWeather() |
100
|
|
|
{ |
101
|
|
|
$currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', ''); |
102
|
|
|
|
103
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testGetWeatherGroup() |
107
|
|
|
{ |
108
|
|
|
$currentWeather = $this->owm->getWeatherGroup('2950159', 'imperial', 'en', ''); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testGetWeatherForecast() |
114
|
|
|
{ |
115
|
|
|
$days = 1; |
116
|
|
|
$defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
117
|
|
|
|
118
|
|
|
$days = 16; |
119
|
|
|
$maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
120
|
|
|
|
121
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay); |
122
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testGetDailyWeatherForecast() |
126
|
|
|
{ |
127
|
|
|
$days = 16; |
128
|
|
|
$dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
129
|
|
|
|
130
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testGetWeatherHistory() |
134
|
|
|
{ |
135
|
|
|
$this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testWasCached() |
139
|
|
|
{ |
140
|
|
|
$weather = $this->owm; |
141
|
|
|
$result = $weather->wasCached(); |
142
|
|
|
|
143
|
|
|
$this->assertFalse($result); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testCached() |
147
|
|
|
{ |
148
|
|
|
$cache = $this->cache; |
149
|
|
|
$cache->setTempPath(__DIR__.'/temps'); |
150
|
|
|
$weather = new OpenWeatherMap($this->apiKey, new TestFetcher(), $cache, 600); |
|
|
|
|
151
|
|
|
$currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml'); |
152
|
|
|
$cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml'); |
153
|
|
|
|
154
|
|
|
$this->assertInternalType('string', $currWeatherData); |
155
|
|
|
$this->assertInternalType('string', $cachedWeatherData); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testBuildQueryUrlParameter() |
159
|
|
|
{ |
160
|
|
|
$weather = $this->owm; |
161
|
|
|
$queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', ''); |
162
|
|
|
$queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', ''); |
163
|
|
|
|
164
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray); |
165
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testAbstractCache() |
169
|
|
|
{ |
170
|
|
|
$sut = $this->getMockForAbstractClass('\Cmfcmf\OpenWeatherMap\AbstractCache'); |
171
|
|
|
$this->assertNull($sut->setSeconds(10)); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: