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
|
|
|
|
20
|
|
|
class OpenWeatherMapExceptionTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $apiKey; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var OpenWeatherMap |
29
|
|
|
*/ |
30
|
|
|
protected $owm; |
31
|
|
|
|
32
|
|
|
protected function setUp() |
33
|
|
|
{ |
34
|
|
|
$this->apiKey = 'unicorn-rainbow'; |
35
|
|
|
$this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @expectedException \InvalidArgumentException |
40
|
|
|
*/ |
41
|
|
|
public function testCacheException() |
42
|
|
|
{ |
43
|
|
|
new OpenWeatherMap($this->apiKey, null, true, 600); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @expectedException \InvalidArgumentException |
48
|
|
|
*/ |
49
|
|
|
public function testSecondNotNumericException() |
50
|
|
|
{ |
51
|
|
|
new OpenWeatherMap($this->apiKey, null, false, 'I am not numeric'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @expectedException \InvalidArgumentException |
56
|
|
|
*/ |
57
|
|
|
public function testGetWeatherForecastException() |
58
|
|
|
{ |
59
|
|
|
$days = 20; |
60
|
|
|
$this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @expectedException \InvalidArgumentException |
65
|
|
|
*/ |
66
|
|
|
public function testGetDailyWeatherForecastException() |
67
|
|
|
{ |
68
|
|
|
$days = 20; |
69
|
|
|
$this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @expectedException \Cmfcmf\OpenWeatherMap\Exception |
74
|
|
|
*/ |
75
|
|
|
public function testGetWeatherHistoryException() |
76
|
|
|
{ |
77
|
|
|
$this->owm->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), 1, 'hour', 'imperial', 'en', ''); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @expectedException \Cmfcmf\OpenWeatherMap\Exception |
82
|
|
|
*/ |
83
|
|
|
public function testGetWeatherHistoryWithEndException() |
84
|
|
|
{ |
85
|
|
|
$this->owm->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), new \DateTime('now'), 'hour', 'imperial', 'en', ''); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @expectedException \InvalidArgumentException |
90
|
|
|
*/ |
91
|
|
|
public function testGetWeatherHistoryInvalidArgumentException() |
92
|
|
|
{ |
93
|
|
|
$this->owm->getWeatherHistory('Berlin', new \DateTime('now'), 1, 'wrong-type', 'imperial', 'en', ''); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @expectedException \InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
public function testGetRawDailyForecastDataInvalidArgumentException() |
100
|
|
|
{ |
101
|
|
|
$this->owm->getRawDailyForecastData('Berlin', 'imperial', 'en', '', 'xml', 20); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @expectedException \InvalidArgumentException |
106
|
|
|
*/ |
107
|
|
|
public function testGetRawWeatherHistoryException() |
108
|
|
|
{ |
109
|
|
|
$this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 1, 'wrong-type', 'imperial', 'en', ''); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @expectedException \InvalidArgumentException |
114
|
|
|
*/ |
115
|
|
|
public function testGetRawWeatherHistoryWithEndDateException() |
116
|
|
|
{ |
117
|
|
|
$this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 'wrong-endOrCount', 'hour', 'imperial', 'en', ''); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @expectedException \InvalidArgumentException |
122
|
|
|
* @dataProvider uviExceptionDataProvider |
123
|
|
|
*/ |
124
|
|
|
public function testGetRawUviWithQueryErrorException($lat, $lon, $dateTime) |
125
|
|
|
{ |
126
|
|
|
$this->owm->getRawUVIndexData($lat, $lon, $dateTime); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @expectedException \InvalidArgumentException |
131
|
|
|
*/ |
132
|
|
|
public function testBuildQueryUrlParameterException() |
133
|
|
|
{ |
134
|
|
|
$this->owm->getWeather(true, 'imperial', 'en', ''); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @expectedException \Cmfcmf\OpenWeatherMap\Exception |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function testParseXMLException() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$answer = 'I am not XML formatted data'; |
143
|
|
|
$method = new \ReflectionMethod($this->owm, 'parseXML'); |
144
|
|
|
$method->setAccessible(true); |
145
|
|
|
|
146
|
|
|
$method->invoke($this->owm, $answer); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @expectedException \Cmfcmf\OpenWeatherMap\Exception |
151
|
|
|
*/ |
152
|
|
|
public function testParseXMLWithIsJsonException() |
153
|
|
|
{ |
154
|
|
|
$answer = array('message' => 'simple json data'); |
155
|
|
|
$answer = json_encode($answer); |
156
|
|
|
$method = new \ReflectionMethod($this->owm, 'parseXML'); |
157
|
|
|
$method->setAccessible(true); |
158
|
|
|
|
159
|
|
|
$method->invoke($this->owm, $answer); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @expectedException \Cmfcmf\OpenWeatherMap\Exception |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public function testParseJsonException() |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$answer = 'I am not a json format data'; |
168
|
|
|
$method = new \ReflectionMethod($this->owm, 'parseJson'); |
169
|
|
|
$method->setAccessible(true); |
170
|
|
|
|
171
|
|
|
$method->invoke($this->owm, $answer); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function uviExceptionDataProvider() |
175
|
|
|
{ |
176
|
|
|
return array( |
177
|
|
|
array('error-query-format', 'foo', new \DateTime()), |
178
|
|
|
array(5.4, 1.2, 'foo'), |
179
|
|
|
array(5.4, 12, 'foo'), |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
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: