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

YahooWeatherClientTests::callApiTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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