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
|
|
|
|
19
|
|
|
class OpenWeatherMapExceptionTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var apiKey |
23
|
|
|
* @var weather |
24
|
|
|
*/ |
25
|
|
|
protected $apiKey; |
26
|
|
|
protected $weather; |
27
|
|
|
|
28
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$ini = parse_ini_file(__DIR__ . '/../ApiKey.ini'); |
31
|
|
|
$apiKey = $ini['api_key']; |
32
|
|
|
$this->apiKey = $apiKey; |
33
|
|
|
$this->weather = new OpenWeatherMap($this->apiKey, null, false, 600); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @expectedException Exception |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function testCacheException() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
$exception = new OpenWeatherMap($this->apiKey, null, true, 600); |
|
|
|
|
43
|
|
|
} catch (\Eception $e) { |
|
|
|
|
44
|
|
|
throw $e; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @expectedException Exception |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function testSecondNotNumbericException() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
try { |
54
|
|
|
$exception = new OpenWeatherMap($this->apiKey, null, false, 'I am not numberic'); |
|
|
|
|
55
|
|
|
} catch (\Exception $e) { |
56
|
|
|
throw $e; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @expectedException InvalidArgumentException |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function testGetWeatherForecastException() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$days = 20; |
66
|
|
|
$weather = $this->weather; |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$argException = $weather->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
|
|
|
|
70
|
|
|
} catch (\InvalidArgumentException $e) { |
71
|
|
|
throw $e; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @expectedException InvalidArgumentException |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function testGetDailyWeatherForecastException() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$days = 20; |
81
|
|
|
$weather = $this->weather; |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
$argException = $weather->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
|
|
|
|
85
|
|
|
} catch (\InvalidArgumentException $e) { |
86
|
|
|
throw $e; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @expectedException \Cmfcmf\OpenWeatherMap\Exception |
92
|
|
|
*/ |
93
|
|
|
public function testGetWeatherHistoryException() |
94
|
|
|
{ |
95
|
|
|
$weather = $this->weather; |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$oWMException = $weather->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), 1, 'hour', 'imperial', 'en', ''); |
|
|
|
|
99
|
|
|
} catch (\Exception $e) { |
100
|
|
|
throw $e; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @expectedException \Cmfcmf\OpenWeatherMap\Exception |
106
|
|
|
*/ |
107
|
|
|
public function testGetWeatherHistoryWithEndException() |
108
|
|
|
{ |
109
|
|
|
$weather = $this->weather; |
110
|
|
|
|
111
|
|
|
try { |
112
|
|
|
$oWMException = $weather->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), new \DateTime('NOW'), 'hour', 'imperial', 'en', ''); |
|
|
|
|
113
|
|
|
} catch (\Exception $e) { |
114
|
|
|
throw $e; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @expectedException InvalidArgumentException |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function testGetWeatherHistoryInvalidArgumentException() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$weather = $this->weather; |
124
|
|
|
|
125
|
|
|
try { |
126
|
|
|
$argException = $weather->getWeatherHistory('Berlin', new \DateTime('NOW'), 1, 'wrong-type', 'imperial', 'en', ''); |
|
|
|
|
127
|
|
|
} catch (\InvalidArgumentException $e) { |
128
|
|
|
throw $e; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @expectedException InvalidArgumentException |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function testGetRawDailyForecastDataInvalidArgumentException() |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$weather = $this->weather; |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
$argException = $weather->getRawDailyForecastData('Berlin', 'imperial', 'en', '', 'xml', 20); |
|
|
|
|
141
|
|
|
} catch (\InvalidArgumentException $e) { |
142
|
|
|
throw $e; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @expectedException InvalidArgumentException |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function testGetRawWeatherHistoryException() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$weather = $this->weather; |
152
|
|
|
|
153
|
|
|
try { |
154
|
|
|
$argException = $weather->getRawWeatherHistory('Berlin', new \DateTime('NOW'), 1, 'wrong-type', 'imperial', 'en', ''); |
|
|
|
|
155
|
|
|
} catch (\InvalidArgumentException $e) { |
156
|
|
|
throw $e; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @expectedException InvalidArgumentException |
162
|
|
|
*/ |
163
|
|
View Code Duplication |
public function testGetRawWeatherHistoryWithEndDateException() |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$weather = $this->weather; |
166
|
|
|
|
167
|
|
|
try { |
168
|
|
|
$argException = $weather->getRawWeatherHistory('Berlin', new \DateTime('NOW'), 'wrong-endOrCount', 'hour', 'imperial', 'en', ''); |
|
|
|
|
169
|
|
|
} catch (\InvalidArgumentException $e) { |
170
|
|
|
throw $e; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @expectedException InvalidArgumentException |
176
|
|
|
*/ |
177
|
|
View Code Duplication |
public function testBuildQueryUrlParameterException() |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
$weather = $this->weather; |
180
|
|
|
|
181
|
|
|
try { |
182
|
|
|
$argException = $weather->getWeather(true, new \DateTime('NOW'), 'wrong-endOrCount', 'hour', 'imperial', 'en', ''); |
|
|
|
|
183
|
|
|
} catch (\InvalidArgumentException $e) { |
184
|
|
|
throw $e; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @expectedException Exception |
190
|
|
|
*/ |
191
|
|
View Code Duplication |
public function testParseXMLException() |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$answer = 'I am not a XML fromat data'; |
194
|
|
|
$weather = $this->weather; |
195
|
|
|
$method = new \ReflectionMethod( |
196
|
|
|
$weather, 'parseXML' |
197
|
|
|
); |
198
|
|
|
$method->setAccessible(true); |
199
|
|
|
|
200
|
|
|
try { |
201
|
|
|
$method->invoke($weather, $answer); |
202
|
|
|
} catch (\Exception $e) { |
203
|
|
|
throw $e; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @expectedException Exception |
209
|
|
|
*/ |
210
|
|
|
public function testParseXMLWithIsJsonException() |
211
|
|
|
{ |
212
|
|
|
$answer = array('message' => 'simple json data'); |
213
|
|
|
$answer = json_encode($answer); |
214
|
|
|
$weather = $this->weather; |
215
|
|
|
$method = new \ReflectionMethod( |
216
|
|
|
$weather, 'parseXML' |
217
|
|
|
); |
218
|
|
|
$method->setAccessible(true); |
219
|
|
|
|
220
|
|
|
try { |
221
|
|
|
$method->invoke($weather, $answer); |
222
|
|
|
} catch (\Exception $e) { |
223
|
|
|
throw $e; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @expectedException \Cmfcmf\OpenWeatherMap\Exception |
229
|
|
|
*/ |
230
|
|
View Code Duplication |
public function testParseJsonException() |
|
|
|
|
231
|
|
|
{ |
232
|
|
|
$weather = $this->weather; |
233
|
|
|
$answer = 'I am not a json format data'; |
234
|
|
|
$method = new \ReflectionMethod( |
235
|
|
|
$weather, 'parseJson' |
236
|
|
|
); |
237
|
|
|
$method->setAccessible(true); |
238
|
|
|
|
239
|
|
|
try { |
240
|
|
|
$method->invoke($weather, $answer); |
241
|
|
|
} catch (\Exception $e) { |
242
|
|
|
throw $e; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
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.