Completed
Pull Request — master (#2)
by lee
02:13
created

YahooWeatherClientTests::callApiTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 19
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 13.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
25
    {
26
        $service = new YahooWeatherAPI();
27
28
        $this->expectException(\Exception::class);
29
30
        try {
31
            $response = $service->callApiWoeid(null);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32
        } catch (\Exception $e) {
33
            throw $e;
34
        }
35
    }
36
37
    /** @test */
38 View Code Duplication
    public function testCallApiCityNameException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
39
    {
40
        $service = new YahooWeatherAPI();
41
42
        $this->expectException(\Exception::class);
43
44
        try {
45
            $response = $service->callApiCityName(null);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
46
        } catch (\Exception $e) {
47
            throw $e;
48
        }
49
    }
50
51
    /** @test */
52 View Code Duplication
    public function testCallApiTestException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
53
    {
54
        $service = new YahooWeatherAPI();
55
56
        $this->expectException(\Exception::class);
57
58
        try {
59
            $response = $service->callApi(null);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
        } catch (\Exception $e) {
61
            throw $e;
62
        }
63
    }
64
65
    /** @test */
66 View Code Duplication
    public function testCallApiLastRes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $response is correct as $service->setClient(new \GuzzleHttp\Client()) (which targets Th3Mouk\YahooWeatherAPI\...WeatherAPI::setClient()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
86
        $this->assertSame($response, null);
87
    }
88
89
    /** @test */
90
    public function setLastResTest()
91
    {
92
        $service = new YahooWeatherAPI();
93
94
        $response = $service->setLastResponse(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
Are you sure the assignment to $response is correct as $service->setLastResponse(null) (which targets Th3Mouk\YahooWeatherAPI\...rAPI::setLastResponse()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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