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

testCallApiWoeidException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

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