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