Completed
Pull Request — master (#2)
by lee
02:40 queued 27s
created

testCallApiCityNameException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 12
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
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 22 and the first side effect is on line 16.

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';
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
14
//require 'src/YahooWeatherAPIInterface.php';
15
//require 'src/YahooWeatherAPI.php';
16
require 'vendor/autoload.php';
17
18
use GuzzleHttp\Client;
19
use PHPUnit\Framework\TestCase;
20
use Th3Mouk\YahooWeatherAPI\YahooWeatherAPI;
21
22
class YahooWeatherClientTests extends TestCase
23
{
24
    /** @test */
25 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...
26
    {
27
        $service = new YahooWeatherAPI();
28
29
        $this->expectException(\Exception::class);
30
31
        try {
32
            $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...
33
        } catch (\Exception $e) {
34
            throw $e;
35
        }
36
    }
37
38
    /** @test */
39 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...
40
    {
41
        $service = new YahooWeatherAPI();
42
43
        $this->expectException(\Exception::class);
44
45
        try {
46
            $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...
47
        } catch (\Exception $e) {
48
            throw $e;
49
        }
50
    }
51
52
    /** @test */
53 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...
54
    {
55
        $service = new YahooWeatherAPI();
56
57
        $this->expectException(\Exception::class);
58
59
        try {
60
            $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...
61
        } catch (\Exception $e) {
62
            throw $e;
63
        }
64
    }
65
66
    /** @test */
67 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...
68
    {
69
        $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';
70
71
        $service = new YahooWeatherAPI();
72
73
        $this->expectException(\Exception::class);
74
75
        try {
76
            $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...
77
        } catch (\Exception $e) {
78
            throw $e;
79
        }
80
    }
81
82
    /** @test */
83
    public function setClientTest()
84
    {
85
        $service = new YahooWeatherAPI();
86
        $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...
87
        $this->assertSame($response, null);
88
    }
89
90
    /** @test */
91
    public function setLastResTest()
92
    {
93
        $service = new YahooWeatherAPI();
94
95
        $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...
96
        $this->assertSame($response, null);
97
    }
98
99
    /** @test */
100
    public function callApiWoeidTest()
101
    {
102
        $service = new YahooWeatherAPI();
103
        $response = $service->callApiWoeid(1232345678);
104
        $this->assertSame($response, false);
105
106
        $response = $service->callApiWoeid(2306179);
107
        $city = str_replace(' ', '', $response['location']['city']);
108
        $country = str_replace(' ', '', $response['location']['country']);
109
        $region = str_replace(' ', '', $response['location']['region']);
110
        $this->assertSame($city, 'TaipeiCity');
111
        $this->assertSame($country, 'Taiwan');
112
        $this->assertSame($region, 'TaipeiCity');
113
114
        $woeid = 2306179;
115
        $response = $this->getWoeidTest($service);
116
        $this->assertSame($response, $woeid);
117
    }
118
119
    /** @test */
120
    public function callApiCityNameTest()
121
    {
122
        $service = new YahooWeatherAPI();
123
        $response = $service->callApiCityName('Taipei');
124
        $city = str_replace(' ', '', $response['location']['city']);
125
        $country = str_replace(' ', '', $response['location']['country']);
126
        $region = str_replace(' ', '', $response['location']['region']);
127
        $this->assertSame($city, 'TaipeiCity');
128
        $this->assertSame($country, 'Taiwan');
129
        $this->assertSame($region, 'TaipeiCity');
130
131
        $city = 'Taipei';
132
        $response = $this->getCityTest($service);
133
        $this->assertSame($response, $city);
134
    }
135
136
    /** @test */
137
    public function callApiTest()
138
    {
139
        $service = new YahooWeatherAPI();
140
        $yql = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="123456789")';
141
        $url = 'https://query.yahooapis.com/v1/public/yql?q='.$yql.'&format=json&env=store://datatables.org/alltableswithkeys';
142
        $url = $this->encodeURI($url);
143
        $response = $service->callApi($url);
144
        $this->assertSame($response, false);
145
146
        $yql = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="Taipei")';
147
        $url = 'https://query.yahooapis.com/v1/public/yql?q='.$yql.'&format=json&env=store://datatables.org/alltableswithkeys';
148
        $url = $this->encodeURI($url);
149
        $response = $service->callApi($url);
150
        $city = str_replace(' ', '', $response['location']['city']);
151
        $country = str_replace(' ', '', $response['location']['country']);
152
        $region = str_replace(' ', '', $response['location']['region']);
153
        $this->assertSame($city, 'TaipeiCity');
154
        $this->assertSame($country, 'Taiwan');
155
        $this->assertSame($region, 'TaipeiCity');
156
157
        $response = $this->getYqlTest($service);
158
        $this->assertSame($response, $url);
159
    }
160
161
    /** @test */
162
    public function getLastResTest()
163
    {
164
        $service = new YahooWeatherAPI();
165
166
        $service->setLastResponse($this->getResponseData());
167
168
        $response = $service->getLastResponse(false);
169
        $this->assertSame(is_array($response), true);
170
171
        $response = $service->getLastResponse(true);
172
        $this->assertSame(is_array($response), false);
173
    }
174
175
    /** @test */
176
    public function getTemperatureTest()
177
    {
178
        $service = new YahooWeatherAPI();
179
        $response = $this->getResponseData();
180
        $storeRes = $response;
181
182
        $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...
183
        $service->getLastResponse(true);
184
        $response = $service->getTemperature(true);
185
        $this->assertSame($response, '');
186
187
        $noTemp = $storeRes;
188
        $noTemp['item']['condition']['temp'] = null;
189
        $service->setLastResponse($noTemp);
190
        $response = $service->getTemperature(false);
191
        $this->assertSame($response, '');
192
193
        $service->setLastResponse($storeRes);
194
        $response = $service->getTemperature(true);
195
        $this->assertSame(is_string($response), true);
196
197
        $response = $service->getTemperature(false);
198
        $this->assertSame(is_string((int) $response), false);
199
    }
200
201
    /** @test */
202
    public function getLocationTest()
203
    {
204
        $service = new YahooWeatherAPI();
205
        $response = $this->getResponseData();
206
        $storeRes = $response;
207
208
        $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...
209
        $response = $service->getLocation();
210
        $this->assertSame($response, '');
211
212
        $noCity = $storeRes;
213
        $noCity['location']['city'] = null;
214
        $service->setLastResponse($noCity);
215
        $response = $service->getLocation();
216
        $this->assertSame($response, '');
217
218
219
        $service->setLastResponse($storeRes);
220
        $response = $service->getLocation();
221
        $city = str_replace(' ', '', $response);
222
        $this->assertSame($city, 'TaipeiCity');
223
    }
224
225
    /** @test */
226
    public function getForecastTest()
227
    {
228
        $service = new YahooWeatherAPI();
229
        $response = $this->getResponseData();
230
        $storeRes = $response;
231
232
        $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...
233
        $response = $service->getForecast();
234
        $this->assertSame(count($response), 0);
235
236
        $noForecast = $storeRes;
237
        $noForecast['item']['forecast'] = null;
238
        $service->setLastResponse($noForecast);
239
        $response = $service->getForecast();
240
        $this->assertSame(count($response), 0);
241
242
        $service->setLastResponse($storeRes);
243
        $response = $service->getForecast();
244
        $this->assertSame(count($response), 10);
245
    }
246
247
    /** @test */
248
    public function getWindTest()
249
    {
250
        $service = new YahooWeatherAPI();
251
        $response = $this->getResponseData();
252
        $storeRes = $response;
253
254
        $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...
255
        $response = $service->getWind(true);
256
        $this->assertSame(count($response), 0);
257
258
        $noSpeed = $storeRes;
259
        $noSpeed['wind']['speed'] = null;
260
        $service->setLastResponse($noSpeed);
261
        $response = $service->getWind(true);
262
        $this->assertSame(count($response), 0);
263
264
        $service->setLastResponse($storeRes);
265
        $expectRes = array(
266
            'chill' => $storeRes['wind']['chill'],
267
            'direction' => $storeRes['wind']['direction'],
268
            'speed' => $storeRes['wind']['speed'],
269
        );
270
271
        $response = $service->getWind(true);
272
        $expectRes['speed'] .= ' '.$storeRes['units']['speed'];
273
        $this->assertSame($response['speed'], $expectRes['speed']);
274
275
        $expectRes = array(
276
            'chill' => $storeRes['wind']['chill'],
277
            'direction' => $storeRes['wind']['direction'],
278
            'speed' => $storeRes['wind']['speed'],
279
        );
280
281
        $response = $service->getWind(false);
282
        $this->assertSame($response['speed'], $expectRes['speed']);
283
    }
284
285
    public function getWoeidTest(YahooWeatherAPI $service)
286
    {
287
        return $service->getWoeid();
288
    }
289
290
    public function getCityTest(YahooWeatherAPI $service)
291
    {
292
        return $service->getCity();
293
    }
294
295
    public function getYqlTest(YahooWeatherAPI $service)
296
    {
297
        return $service->getYql();
298
    }
299
300
    public function encodeURI($url)
301
    {
302
        $unescaped = array(
303
            '%2D' => '-', '%5F' => '_', '%2E' => '.', '%21' => '!', '%7E' => '~',
304
            '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')',
305
        );
306
        $reserved = array(
307
            '%3B' => ';', '%2C' => ',', '%2F' => '/', '%3F' => '?', '%3A' => ':',
308
            '%40' => '@', '%26' => '&', '%3D' => '=', '%2B' => '+', '%24' => '$',
309
        );
310
        $score = array(
311
            '%23' => '#',
312
        );
313
314
        return strtr(rawurlencode($url), array_merge($reserved, $unescaped, $score));
315
    }
316
317
    public function getResponseData()
318
    {
319
        $response = file_get_contents('tests/response.json');
320
        $response = json_decode($response, true);
321
322
        return $response;
323
    }
324
}
325