Completed
Pull Request — master (#2)
by lee
04:31
created

YahooWeatherClientTests::clientAttributesTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
eloc 1
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 23 and the first side effect is on line 14.

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