Completed
Push — v3 ( d2b0fb...99929a )
by Christian
04:44 queued 03:26
created

OpenWeatherMapTest::testGetForecastUVIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Copyright Zikula Foundation 2014 - Zikula Application Framework
4
 *
5
 * This work is contributed to the Zikula Foundation under one or more
6
 * Contributor Agreements and licensed to You under the following license:
7
 *
8
 * @license GNU/LGPv3 (or at your option any later version).
9
 * @package OpenWeatherMap-PHP-Api
10
 *
11
 * Please see the NOTICE file distributed with this source code for further
12
 * information regarding copyright and licensing.
13
 */
14
15
namespace Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap;
16
17
use \Cmfcmf\OpenWeatherMap;
18
use Cmfcmf\OpenWeatherMap\Exception;
19
use Cmfcmf\OpenWeatherMap\Tests\TestFetcher;
20
21
class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $apiKey;
27
28
    /**
29
     * @var OpenWeatherMap
30
     */
31
    protected $owm;
32
33
    /**
34
     * @var OpenWeatherMap
35
     */
36
    protected $openWeather;
37
38
    /**
39
     * @var ExampleCacheTest
40
     */
41
    protected $cache;
42
43
    protected function setUp()
44
    {
45
        $ini = parse_ini_file(__DIR__.'/../Examples/ApiKey.ini');
46
        $myApiKey = $ini['api_key'];
47
        $this->apiKey = $myApiKey;
48
        $this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600);
49
        $this->openWeather = new OpenWeatherMap($this->apiKey, null, false, 600);
50
        $this->cache = new ExampleCacheTest();
51
    }
52
53
    protected function tearDown()
54
    {
55
        $fileList = glob(__DIR__.'/temps/OpenWeatherMapPHPAPI/*');
56
        foreach ($fileList as $fileName) {
57
            @unlink($fileName);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
58
        }
59
60
        @rmdir(__DIR__.'/temps/OpenWeatherMapPHPAPI');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
61
        @rmdir(__DIR__.'/temps');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
62
    }
63
64
    public function testApiKeyNotNull()
65
    {
66
        $weather = $this->owm;
67
        $apiKey = $weather->getApiKey();
68
69
        $this->assertSame($this->apiKey, $apiKey);
70
    }
71
72
    public function testSecondIsZero()
73
    {
74
        $weather = new OpenWeatherMap($this->apiKey, null, false, 0);
75
        $apiKey = $weather->getApiKey();
76
77
        $this->assertSame($this->apiKey, $apiKey);
78
    }
79
80
    public function testSetApiKey()
81
    {
82
        $weather = $this->owm;
83
        $weather->setApiKey($this->apiKey);
84
        $apiKey = $weather->getApiKey();
85
86
        $this->assertSame($this->apiKey, $apiKey);
87
    }
88
89
    public function testGetApiKey()
90
    {
91
        $weather = $this->owm;
92
        $apiKey = $weather->getApiKey();
93
94
        $this->assertSame($this->apiKey, $apiKey);
95
    }
96
97
    public function testGetWeather()
98
    {
99
        $currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', '');
100
101
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather);
102
    }
103
104
    public function testGetWeatherGroup()
105
    {
106
        $currentWeather = $this->owm->getWeatherGroup(array('2950159'), 'imperial', 'en', '');
107
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
108
109
        $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...
110
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
111
    }
112
113
    public function testGetWeatherForecast()
114
    {
115
        $days = 1;
116
        $defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
117
118
        $days = 16;
119
        $maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
120
121
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay);
122
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
123
    }
124
125
    public function testGetCurrentUVIndex()
126
    {
127
        $owm = $this->openWeather;
128
        $result = $owm->getCurrentUVIndex(40.7, -74.2);
129
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
130
    }
131
132
    public function testGetForecastUVIndex()
133
    {
134
        $owm = $this->openWeather;
135
        
136
        try {
137
            $result = $owm->getForecastUVIndex(40.7, -74.2, 5);
138
        } catch (Exception $e) {
139
            // OWM might not actually have data for the timespan.
140
            $this->assertSame('An error occurred: not found', $e->getMessage());
141
        }
142
        $this->assertContainsOnlyInstancesOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
143
    }
144
145
    public function testGetHistoryUVIndex()
146
    {
147
        $owm = $this->openWeather;
148
149
        try {
150
            $start = new \DateTime('1969-08-15');
151
            $end = new \DateTime('1969-08-18');
152
            $result = $owm->getHistoricUVIndex(40.7, -74.2, $start, $end);
153
        } catch (Exception $e) {
154
            // OWM might not actually have data for the timespan.
155
            $this->assertSame('An error occurred: not found', $e->getMessage());
156
        }
157
        $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...
158
    }
159
160
    public function testGetDailyWeatherForecast()
161
    {
162
        $days = 16;
163
        $dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
164
165
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast);
166
    }
167
168
    public function testGetWeatherHistory()
169
    {
170
        $this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.');
171
    }
172
173
    public function testWasCached()
174
    {
175
        $weather = $this->owm;
176
        $result = $weather->wasCached();
177
178
        $this->assertFalse($result);
179
    }
180
181
    public function testCached()
182
    {
183
        $cache = $this->cache;
184
        $cache->setTempPath(__DIR__.'/temps');
185
        $weather = new OpenWeatherMap($this->apiKey, new TestFetcher(), $cache, 600);
0 ignored issues
show
Documentation introduced by
$cache is of type object<Cmfcmf\OpenWeathe...erMap\ExampleCacheTest>, but the function expects a boolean|string.

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...
186
        $currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
187
        $cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
188
189
        $this->assertInternalType('string', $currWeatherData);
190
        $this->assertInternalType('string', $cachedWeatherData);
191
    }
192
193
    public function testBuildQueryUrlParameter()
194
    {
195
        $weather = $this->owm;
196
        $queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', '');
197
        $queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', '');
198
199
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray);
200
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray);
201
    }
202
203
    public function testAbstractCache()
204
    {
205
        /** @var OpenWeatherMap\AbstractCache $sut */
206
        $sut = $this->getMockForAbstractClass('\Cmfcmf\OpenWeatherMap\AbstractCache');
207
        $this->assertNull($sut->setSeconds(10));
208
    }
209
}
210