Completed
Push — main ( 8b9660...a53349 )
by Christian
83:53 queued 68:57
created

OpenWeatherMapTest::testInvalidData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5
 *
6
 * @license MIT
7
 *
8
 * Please see the LICENSE file distributed with this source code for further
9
 * information regarding copyright and licensing.
10
 *
11
 * Please visit the following links to read about the usage policies and the license of
12
 * OpenWeatherMap data before using this library:
13
 *
14
 * @see https://OpenWeatherMap.org/price
15
 * @see https://OpenWeatherMap.org/terms
16
 * @see https://OpenWeatherMap.org/appid
17
 */
18
19
namespace Cmfcmf\OpenWeatherMap\Tests;
20
21
use Cmfcmf\OpenWeatherMap;
22
use Cmfcmf\OpenWeatherMap\Exception;
23
use Cmfcmf\OpenWeatherMap\Tests\TestHttpClient;
24
use Cache\Adapter\PHPArray\ArrayCachePool;
25
use Http\Factory\Guzzle\RequestFactory;
26
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
27
use Psr\SimpleCache\CacheInterface;
28
29
class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase
30
{
31
    /**
32
     * @var string
33
     */
34
    protected $apiKey;
35
36
    /**
37
     * @var OpenWeatherMap
38
     */
39
    protected $owm;
40
41
    /**
42
     * @var OpenWeatherMap
43
     */
44
    protected $openWeather;
45
46
    /**
47
     * @var CacheInterface
48
     */
49
    protected $cache;
50
51
    /**
52
     * @var TestHttpClient
53
     */
54
    protected $httpClient;
55
56
    protected function setUp()
57
    {
58
        $ini = parse_ini_file(__DIR__.'/../Examples/ApiKey.ini');
59
        $this->apiKey = $ini['api_key'];
60
        $this->httpClient = new TestHttpClient();
61
        $this->owm = new OpenWeatherMap($this->apiKey, $this->httpClient, new RequestFactory());
62
        $this->openWeather = new OpenWeatherMap($this->apiKey, GuzzleAdapter::createWithConfig([]), new RequestFactory());
63
        $this->cache =  new ArrayCachePool();
64
    }
65
66
    public function testApiKeyNotNull()
67
    {
68
        $weather = $this->owm;
69
        $apiKey = $weather->getApiKey();
70
71
        $this->assertSame($this->apiKey, $apiKey);
72
    }
73
74
    public function testSetApiKey()
75
    {
76
        $weather = $this->owm;
77
        $weather->setApiKey($this->apiKey);
78
        $apiKey = $weather->getApiKey();
79
80
        $this->assertSame($this->apiKey, $apiKey);
81
    }
82
83
    public function testGetApiKey()
84
    {
85
        $weather = $this->owm;
86
        $apiKey = $weather->getApiKey();
87
88
        $this->assertSame($this->apiKey, $apiKey);
89
    }
90
91
    /**
92
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
93
     */
94
    public function testInvalidData()
95
    {
96
        $this->httpClient->returnErrorForNextRequest(500);
97
        $this->owm->getWeather('Berlin', 'imperial', 'en', '');
98
    }
99
100
    public function testGetWeather()
101
    {
102
        $currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', '');
103
104
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather);
105
    }
106
107
    public function testGetWeatherGroup()
108
    {
109
        $currentWeather = $this->owm->getWeatherGroup(array('2950159'), 'imperial', 'en', '');
110
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
111
112
        $currentWeather = $this->owm->getWeatherGroup('2950159', 'imperial', 'en', '');
0 ignored issues
show
Documentation introduced by
'2950159' is of type string, 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...
113
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
114
    }
115
116
    public function testGetWeatherForecast()
117
    {
118
        $days = 1;
119
        $defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
120
121
        $days = 16;
122
        $maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
123
124
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay);
125
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
126
    }
127
128
    public function testGetCurrentUVIndex()
129
    {
130
        $owm = $this->openWeather;
131
        $result = $owm->getCurrentUVIndex(40.7, -74.2);
132
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
133
    }
134
135
    public function testGetForecastUVIndex()
136
    {
137
        $owm = $this->openWeather;
138
139
        try {
140
            $result = $owm->getForecastUVIndex(40.7, -74.2, 5);
141
        } catch (Exception $e) {
142
            // OWM might not actually have data for the timespan.
143
            $this->assertSame('An error occurred: not found', $e->getMessage());
144
        }
145
        $this->assertContainsOnlyInstancesOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
146
    }
147
148
    public function testGetHistoryUVIndex()
149
    {
150
        $owm = $this->openWeather;
151
152
        try {
153
            $start = new \DateTime('1969-08-15');
154
            $end = new \DateTime('1969-08-18');
155
            $result = $owm->getHistoricUVIndex(40.7, -74.2, $start, $end);
156
        } catch (Exception $e) {
157
            // OWM might not actually have data for the timespan.
158
            $this->assertSame('An error occurred: not found', $e->getMessage());
159
        }
160
        $this->assertContainsOnlyInstancesOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
161
    }
162
163
    public function testGetDailyWeatherForecast()
164
    {
165
        $days = 16;
166
        $dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
167
168
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast);
169
    }
170
171
    public function testGetAirPollution()
172
    {
173
        $airPollutionCurrent = $this->owm->getAirPollution("CO", "40", "-74", "current");
174
        $this->assertInstanceOf(OpenWeatherMap\AirPollution\COAirPollution::class, $airPollutionCurrent);
175
        $airPollutionPast = $this->owm->getAirPollution("CO", "40", "-74", "2016Z");
176
        $this->assertInstanceOf(OpenWeatherMap\AirPollution\COAirPollution::class, $airPollutionPast);
177
    }
178
179
    public function testWasCached()
180
    {
181
        $weather = $this->owm;
182
        $result = $weather->wasCached();
183
184
        $this->assertFalse($result);
185
    }
186
187
    public function testCached()
188
    {
189
        $weather = new OpenWeatherMap($this->apiKey, new TestHttpClient(), new RequestFactory(), $this->cache, 600);
0 ignored issues
show
Documentation introduced by
$this->cache is of type object<Psr\SimpleCache\CacheInterface>, but the function expects a null|object<Psr\Cache\CacheItemPoolInterface>.

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...
190
        $currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
191
        $this->assertFalse($weather->wasCached());
192
        $cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
193
        $this->assertTrue($weather->wasCached());
194
195
        $this->assertInternalType('string', $currWeatherData);
196
        $this->assertInternalType('string', $cachedWeatherData);
197
        $this->assertSame($currWeatherData, $cachedWeatherData);
198
    }
199
200
    public function testBuildQueryUrlParameter()
201
    {
202
        $weather = $this->owm;
203
        $queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', '');
204
        $queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', '');
205
206
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray);
207
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray);
208
    }
209
}
210