1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Jean-Baptiste Audebert <[email protected]> |
5
|
|
|
* (c) Jérémy Marodon <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Th3Mouk\YahooWeatherAPI\Tests; |
12
|
|
|
|
13
|
|
|
require 'src/Query/Query.php'; |
14
|
|
|
require 'src/YahooWeatherAPIInterface.php'; |
15
|
|
|
require 'src/YahooWeatherAPI.php'; |
16
|
|
|
|
17
|
|
|
use GuzzleHttp\Client; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
use Th3Mouk\YahooWeatherAPI\YahooWeatherAPI; |
20
|
|
|
|
21
|
|
|
class YahooWeatherClientTests extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** @test */ |
24
|
|
View Code Duplication |
public function testCallApiWoeidException() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$service = new YahooWeatherAPI(); |
27
|
|
|
|
28
|
|
|
$this->expectException(\Exception::class); |
29
|
|
|
|
30
|
|
|
try { |
31
|
|
|
$response = $service->callApiWoeid(null); |
|
|
|
|
32
|
|
|
} catch (\Exception $e) { |
33
|
|
|
throw $e; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @test */ |
38
|
|
View Code Duplication |
public function testCallApiCityNameException() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$service = new YahooWeatherAPI(); |
41
|
|
|
|
42
|
|
|
$this->expectException(\Exception::class); |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
$response = $service->callApiCityName(null); |
|
|
|
|
46
|
|
|
} catch (\Exception $e) { |
47
|
|
|
throw $e; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @test */ |
52
|
|
View Code Duplication |
public function testCallApiTestException() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$service = new YahooWeatherAPI(); |
55
|
|
|
|
56
|
|
|
$this->expectException(\Exception::class); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$response = $service->callApi(null); |
|
|
|
|
60
|
|
|
} catch (\Exception $e) { |
61
|
|
|
throw $e; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @test */ |
66
|
|
View Code Duplication |
public function testCallApiLastRes() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$errYql = 'https://query.yahooapis.com/v1/public/yql?q=from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text=%22Taipei%22)&format=json&env=store://datatables.org/alltableswithkeys'; |
69
|
|
|
|
70
|
|
|
$service = new YahooWeatherAPI(); |
71
|
|
|
|
72
|
|
|
$this->expectException(\Exception::class); |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$response = $service->callApi($errYql); |
|
|
|
|
76
|
|
|
} catch (\Exception $e) { |
77
|
|
|
throw $e; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @test */ |
82
|
|
|
public function setClientTest() |
83
|
|
|
{ |
84
|
|
|
$service = new YahooWeatherAPI(); |
85
|
|
|
$response = $service->setClient(new Client()); |
|
|
|
|
86
|
|
|
$this->assertSame($response, null); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** @test */ |
90
|
|
|
public function setLastResTest() |
91
|
|
|
{ |
92
|
|
|
$service = new YahooWeatherAPI(); |
93
|
|
|
|
94
|
|
|
$response = $service->setLastResponse(null); |
|
|
|
|
95
|
|
|
$this->assertSame($response, null); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** @test */ |
99
|
|
|
public function callApiWoeidTest() |
100
|
|
|
{ |
101
|
|
|
$service = new YahooWeatherAPI(); |
102
|
|
|
$response = $service->callApiWoeid(1232345678); |
103
|
|
|
$this->assertSame($response, false); |
104
|
|
|
|
105
|
|
|
$response = $service->callApiWoeid(2306179); |
106
|
|
|
$city = str_replace(' ', '', $response['location']['city']); |
107
|
|
|
$country = str_replace(' ', '', $response['location']['country']); |
108
|
|
|
$region = str_replace(' ', '', $response['location']['region']); |
109
|
|
|
$this->assertSame($city, 'TaipeiCity'); |
110
|
|
|
$this->assertSame($country, 'Taiwan'); |
111
|
|
|
$this->assertSame($region, 'TaipeiCity'); |
112
|
|
|
|
113
|
|
|
$woeid = 2306179; |
114
|
|
|
$response = $this->getWoeidTest($service); |
115
|
|
|
$this->assertSame($response, $woeid); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** @test */ |
119
|
|
|
public function callApiCityNameTest() |
120
|
|
|
{ |
121
|
|
|
$service = new YahooWeatherAPI(); |
122
|
|
|
$response = $service->callApiCityName('Taipei'); |
123
|
|
|
$city = str_replace(' ', '', $response['location']['city']); |
124
|
|
|
$country = str_replace(' ', '', $response['location']['country']); |
125
|
|
|
$region = str_replace(' ', '', $response['location']['region']); |
126
|
|
|
$this->assertSame($city, 'TaipeiCity'); |
127
|
|
|
$this->assertSame($country, 'Taiwan'); |
128
|
|
|
$this->assertSame($region, 'TaipeiCity'); |
129
|
|
|
|
130
|
|
|
$city = 'Taipei'; |
131
|
|
|
$response = $this->getCityTest($service); |
132
|
|
|
$this->assertSame($response, $city); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** @test */ |
136
|
|
|
public function callApiTest() |
137
|
|
|
{ |
138
|
|
|
$service = new YahooWeatherAPI(); |
139
|
|
|
$yql = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="123456789")'; |
140
|
|
|
$url = 'https://query.yahooapis.com/v1/public/yql?q='.$yql.'&format=json&env=store://datatables.org/alltableswithkeys'; |
141
|
|
|
$url = $this->encodeURI($url); |
142
|
|
|
$response = $service->callApi($url); |
143
|
|
|
$this->assertSame($response, false); |
144
|
|
|
|
145
|
|
|
$yql = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="Taipei")'; |
146
|
|
|
$url = 'https://query.yahooapis.com/v1/public/yql?q='.$yql.'&format=json&env=store://datatables.org/alltableswithkeys'; |
147
|
|
|
$url = $this->encodeURI($url); |
148
|
|
|
$response = $service->callApi($url); |
149
|
|
|
$city = str_replace(' ', '', $response['location']['city']); |
150
|
|
|
$country = str_replace(' ', '', $response['location']['country']); |
151
|
|
|
$region = str_replace(' ', '', $response['location']['region']); |
152
|
|
|
$this->assertSame($city, 'TaipeiCity'); |
153
|
|
|
$this->assertSame($country, 'Taiwan'); |
154
|
|
|
$this->assertSame($region, 'TaipeiCity'); |
155
|
|
|
|
156
|
|
|
$response = $this->getYqlTest($service); |
157
|
|
|
$this->assertSame($response, $url); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** @test */ |
161
|
|
|
public function getLastResTest() |
162
|
|
|
{ |
163
|
|
|
$service = new YahooWeatherAPI(); |
164
|
|
|
|
165
|
|
|
$service->setLastResponse($this->getResponseData()); |
166
|
|
|
|
167
|
|
|
$response = $service->getLastResponse(false); |
168
|
|
|
$this->assertSame(is_array($response), true); |
169
|
|
|
|
170
|
|
|
$response = $service->getLastResponse(true); |
171
|
|
|
$this->assertSame(is_array($response), false); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** @test */ |
175
|
|
|
public function getTemperatureTest() |
176
|
|
|
{ |
177
|
|
|
$service = new YahooWeatherAPI(); |
178
|
|
|
$response = $this->getResponseData(); |
179
|
|
|
$storeRes = $response; |
180
|
|
|
|
181
|
|
|
$service->setLastResponse(null); |
|
|
|
|
182
|
|
|
$service->getLastResponse(true); |
183
|
|
|
$response = $service->getTemperature(true); |
184
|
|
|
$this->assertSame($response, ''); |
185
|
|
|
|
186
|
|
|
$noTemp = $storeRes; |
187
|
|
|
$noTemp['item']['condition']['temp'] = null; |
188
|
|
|
$service->setLastResponse($noTemp); |
189
|
|
|
$response = $service->getTemperature(false); |
190
|
|
|
$this->assertSame($response, ''); |
191
|
|
|
|
192
|
|
|
$service->setLastResponse($storeRes); |
193
|
|
|
$response = $service->getTemperature(true); |
194
|
|
|
$this->assertSame(is_string($response), true); |
195
|
|
|
|
196
|
|
|
$response = $service->getTemperature(false); |
197
|
|
|
$this->assertSame(is_string((int) $response), false); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** @test */ |
201
|
|
|
public function getLocationTest() |
202
|
|
|
{ |
203
|
|
|
$service = new YahooWeatherAPI(); |
204
|
|
|
$response = $this->getResponseData(); |
205
|
|
|
$storeRes = $response; |
206
|
|
|
|
207
|
|
|
$service->setLastResponse(null); |
|
|
|
|
208
|
|
|
$response = $service->getLocation(); |
209
|
|
|
$this->assertSame($response, ''); |
210
|
|
|
|
211
|
|
|
$noCity = $storeRes; |
212
|
|
|
$noCity['location']['city'] = null; |
213
|
|
|
$service->setLastResponse($noCity); |
214
|
|
|
$response = $service->getLocation(); |
215
|
|
|
$this->assertSame($response, ''); |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
$service->setLastResponse($storeRes); |
219
|
|
|
$response = $service->getLocation(); |
220
|
|
|
$city = str_replace(' ', '', $response); |
221
|
|
|
$this->assertSame($city, 'TaipeiCity'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** @test */ |
225
|
|
|
public function getForecastTest() |
226
|
|
|
{ |
227
|
|
|
$service = new YahooWeatherAPI(); |
228
|
|
|
$response = $this->getResponseData(); |
229
|
|
|
$storeRes = $response; |
230
|
|
|
|
231
|
|
|
$service->setLastResponse(null); |
|
|
|
|
232
|
|
|
$response = $service->getForecast(); |
233
|
|
|
$this->assertSame(count($response), 0); |
234
|
|
|
|
235
|
|
|
$noForecast = $storeRes; |
236
|
|
|
$noForecast['item']['forecast'] = null; |
237
|
|
|
$service->setLastResponse($noForecast); |
238
|
|
|
$response = $service->getForecast(); |
239
|
|
|
$this->assertSame(count($response), 0); |
240
|
|
|
|
241
|
|
|
$service->setLastResponse($storeRes); |
242
|
|
|
$response = $service->getForecast(); |
243
|
|
|
$this->assertSame(count($response), 10); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** @test */ |
247
|
|
|
public function getWindTest() |
248
|
|
|
{ |
249
|
|
|
$service = new YahooWeatherAPI(); |
250
|
|
|
$response = $this->getResponseData(); |
251
|
|
|
$storeRes = $response; |
252
|
|
|
|
253
|
|
|
$service->setLastResponse(null); |
|
|
|
|
254
|
|
|
$response = $service->getWind(true); |
255
|
|
|
$this->assertSame(count($response), 0); |
256
|
|
|
|
257
|
|
|
$noSpeed = $storeRes; |
258
|
|
|
$noSpeed['wind']['speed'] = null; |
259
|
|
|
$service->setLastResponse($noSpeed); |
260
|
|
|
$response = $service->getWind(true); |
261
|
|
|
$this->assertSame(count($response), 0); |
262
|
|
|
|
263
|
|
|
$service->setLastResponse($storeRes); |
264
|
|
|
$expectRes = array( |
265
|
|
|
'chill' => $storeRes['wind']['chill'], |
266
|
|
|
'direction' => $storeRes['wind']['direction'], |
267
|
|
|
'speed' => $storeRes['wind']['speed'], |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
$response = $service->getWind(true); |
271
|
|
|
$expectRes['speed'] .= ' '.$storeRes['units']['speed']; |
272
|
|
|
$this->assertSame($response['speed'], $expectRes['speed']); |
273
|
|
|
|
274
|
|
|
$expectRes = array( |
275
|
|
|
'chill' => $storeRes['wind']['chill'], |
276
|
|
|
'direction' => $storeRes['wind']['direction'], |
277
|
|
|
'speed' => $storeRes['wind']['speed'], |
278
|
|
|
); |
279
|
|
|
|
280
|
|
|
$response = $service->getWind(false); |
281
|
|
|
$this->assertSame($response['speed'], $expectRes['speed']); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function getWoeidTest(YahooWeatherAPI $service) |
285
|
|
|
{ |
286
|
|
|
return $service->getWoeid(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public function getCityTest(YahooWeatherAPI $service) |
290
|
|
|
{ |
291
|
|
|
return $service->getCity(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function getYqlTest(YahooWeatherAPI $service) |
295
|
|
|
{ |
296
|
|
|
return $service->getYql(); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function encodeURI($url) |
300
|
|
|
{ |
301
|
|
|
$unescaped = array( |
302
|
|
|
'%2D' => '-', '%5F' => '_', '%2E' => '.', '%21' => '!', '%7E' => '~', |
303
|
|
|
'%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')', |
304
|
|
|
); |
305
|
|
|
$reserved = array( |
306
|
|
|
'%3B' => ';', '%2C' => ',', '%2F' => '/', '%3F' => '?', '%3A' => ':', |
307
|
|
|
'%40' => '@', '%26' => '&', '%3D' => '=', '%2B' => '+', '%24' => '$', |
308
|
|
|
); |
309
|
|
|
$score = array( |
310
|
|
|
'%23' => '#', |
311
|
|
|
); |
312
|
|
|
|
313
|
|
|
return strtr(rawurlencode($url), array_merge($reserved, $unescaped, $score)); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function getResponseData() |
317
|
|
|
{ |
318
|
|
|
$response = file_get_contents('tests/response.json'); |
319
|
|
|
$response = json_decode($response, true); |
320
|
|
|
|
321
|
|
|
return $response; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.