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\WeatherHistory; |
19
|
|
|
use \Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap\ExampleCacheTest; |
20
|
|
|
|
21
|
|
|
class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var apiKey |
25
|
|
|
* @var weather |
26
|
|
|
* @var cache |
27
|
|
|
*/ |
28
|
|
|
protected $apiKey; |
29
|
|
|
protected $weather; |
30
|
|
|
protected $cache; |
31
|
|
|
|
32
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$ini = parse_ini_file(__DIR__ . '/ApiKey.ini'); |
35
|
|
|
$apiKey = $ini['api_key']; |
36
|
|
|
$this->apiKey = $apiKey; |
37
|
|
|
$this->weather = new OpenWeatherMap($this->apiKey, null, false, 600); |
38
|
|
|
$this->cache = new ExampleCacheTest(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function tearDown() |
42
|
|
|
{ |
43
|
|
|
$fileList = glob(__DIR__.'/temps/OpenWeatherMapPHPAPI/*'); |
44
|
|
|
foreach ($fileList as $fileName) { |
45
|
|
|
@unlink($fileName); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
@rmdir(__DIR__.'/temps/OpenWeatherMapPHPAPI'); |
|
|
|
|
49
|
|
|
@rmdir(__DIR__.'/temps'); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testApiKeyIsEmpty() |
53
|
|
|
{ |
54
|
|
|
$expectApiKey = ''; |
55
|
|
|
$weather = new OpenWeatherMap($expectApiKey, null, false, 600); |
56
|
|
|
$apiKey = $weather->getApiKey(); |
57
|
|
|
|
58
|
|
|
$this->assertSame($expectApiKey, $apiKey); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testApiKeyNotNull() |
62
|
|
|
{ |
63
|
|
|
$weather = $this->weather; |
64
|
|
|
$apiKey = $weather->getApiKey(); |
65
|
|
|
|
66
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testSecondIsZero() |
70
|
|
|
{ |
71
|
|
|
$weather = new OpenWeatherMap($this->apiKey, null, false, 0); |
72
|
|
|
$apiKey = $weather->getApiKey(); |
73
|
|
|
|
74
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testSetApiKey() |
78
|
|
|
{ |
79
|
|
|
$weather = $this->weather; |
80
|
|
|
$weather->setApiKey($this->apiKey); |
81
|
|
|
$apiKey = $weather->getApiKey(); |
82
|
|
|
|
83
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testGetApiKey() |
87
|
|
|
{ |
88
|
|
|
$weather = $this->weather; |
89
|
|
|
$apiKey = $weather->getApiKey(); |
90
|
|
|
|
91
|
|
|
$this->assertSame($this->apiKey, $apiKey); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function testGetWeather() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$weather = $this->weather; |
97
|
|
|
$currentWeather = $weather->getWeather('Berlin', 'imperial', 'en', ''); |
98
|
|
|
|
99
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testGetWeatherGroup() |
103
|
|
|
{ |
104
|
|
|
$weather = $this->weather; |
105
|
|
|
$currentWeather = $weather->getWeatherGroup('2950159', 'imperial', 'en', ''); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testGetWeatherForecast() |
111
|
|
|
{ |
112
|
|
|
$days = 1; |
113
|
|
|
$weather = $this->weather; |
114
|
|
|
$defaultDay = $weather->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
115
|
|
|
|
116
|
|
|
$days = 16; |
117
|
|
|
$maxDay = $weather->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
118
|
|
|
|
119
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay); |
120
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
View Code Duplication |
public function testGetDailyWeatherForecast() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$days = 16; |
126
|
|
|
$weather = $this->weather; |
127
|
|
|
$dailyForecast = $weather->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
128
|
|
|
|
129
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testGetWeatherHistory() |
133
|
|
|
{ |
134
|
|
|
$this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testWasCached() |
138
|
|
|
{ |
139
|
|
|
$weather = $this->weather; |
140
|
|
|
$result = $weather->wasCached(); |
141
|
|
|
|
142
|
|
|
$this->assertFalse($result); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testCached() |
146
|
|
|
{ |
147
|
|
|
$sampleUrl = 'http://api.openweathermap.org/data/2.5/weather?q=Berlin&units=imperial&lang=en&mode=json&APPID=50b2ae8523a458183982bf56254556dc'; |
|
|
|
|
148
|
|
|
|
149
|
|
|
$cache = $this->cache; |
150
|
|
|
$cache->setTempPath(__DIR__.'/temps'); |
151
|
|
|
$weather = new OpenWeatherMap($this->apiKey, null, $cache, 600); |
|
|
|
|
152
|
|
|
$currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'json'); |
153
|
|
|
$cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'json'); |
154
|
|
|
|
155
|
|
|
$this->assertInternalType('string', $currWeatherData); |
156
|
|
|
$this->assertInternalType('string', $cachedWeatherData); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testBuildQueryUrlParameter() |
160
|
|
|
{ |
161
|
|
|
$weather = $this->weather; |
162
|
|
|
$queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', ''); |
163
|
|
|
$queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', ''); |
164
|
|
|
|
165
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray); |
166
|
|
|
$this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testAbstractCache() |
170
|
|
|
{ |
171
|
|
|
$sut = $this->getMockForAbstractClass('\Cmfcmf\OpenWeatherMap\AbstractCache'); |
172
|
|
|
$this->assertNull($sut->setSeconds(10)); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.