Completed
Pull Request — master (#132)
by
unknown
12:30
created

OpenWeatherMapTest::testGetWeatherHistory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 testApiKeyIsEmpty()
65
    {
66
        $expectApiKey = '';
67
        $weather = new OpenWeatherMap($expectApiKey, null, false, 600);
68
        $apiKey = $weather->getApiKey();
69
70
        $this->assertSame($expectApiKey, $apiKey);
71
    }
72
73
    public function testApiKeyNotNull()
74
    {
75
        $weather = $this->owm;
76
        $apiKey = $weather->getApiKey();
77
78
        $this->assertSame($this->apiKey, $apiKey);
79
    }
80
81
    public function testSecondIsZero()
82
    {
83
        $weather = new OpenWeatherMap($this->apiKey, null, false, 0);
84
        $apiKey = $weather->getApiKey();
85
86
        $this->assertSame($this->apiKey, $apiKey);
87
    }
88
89
    public function testSetApiKey()
90
    {
91
        $weather = $this->owm;
92
        $weather->setApiKey($this->apiKey);
93
        $apiKey = $weather->getApiKey();
94
95
        $this->assertSame($this->apiKey, $apiKey);
96
    }
97
98
    public function testGetApiKey()
99
    {
100
        $weather = $this->owm;
101
        $apiKey = $weather->getApiKey();
102
103
        $this->assertSame($this->apiKey, $apiKey);
104
    }
105
106
    public function testGetWeather()
107
    {
108
        $currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', '');
109
110
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather);
111
    }
112
113
    public function testGetWeatherGroup()
114
    {
115
        $currentWeather = $this->owm->getWeatherGroup(array('2950159'), 'imperial', 'en', '');
116
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
117
118
        $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...
119
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
120
    }
121
122
    public function testGetWeatherForecast()
123
    {
124
        $days = 1;
125
        $defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
126
        
127
        $days = 16;
128
        $maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
129
130
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay);
131
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
132
    }
133
134
    public function testGetCurrentUVIndex()
135
    {
136
        $owm = $this->openWeather;
137
        $result = $owm->getCurrentUVIndex(40.7, -74.2);
138
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
139
    }
140
141
    public function testGetForecastUVIndex()
142
    {
143
        $owm = $this->openWeather;
144
        foreach ($precisions as $precision) {
0 ignored issues
show
Bug introduced by
The variable $precisions does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
145
            try {
146
                $result = $owm->getForecastUVIndex(40.7, -74.2, 5);
147
            } catch (Exception $e) {
148
                // OWM might not actually have data for the timespan.
149
                $this->assertSame('An error occurred: not found', $e->getMessage());
150
            }
151
            $this->assertContainsOnlyInstancesOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
152
        }
153
    }
154
155
    public function testGetHistoryUVIndex()
156
    {
157
        $owm = $this->openWeather;
158
        foreach ($precisions as $precision) {
0 ignored issues
show
Bug introduced by
The variable $precisions does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
159
            try {
160
                $start = new \DateTime('1969-08-15');
161
                $end = new \DateTime('1969-08-18');
162
                $result = $owm->getForecastUVIndex(40.7, -74.2, null, $start, $end);
0 ignored issues
show
Unused Code introduced by
The call to OpenWeatherMap::getForecastUVIndex() has too many arguments starting with $start.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
163
            } catch (Exception $e) {
164
                // OWM might not actually have data for the timespan.
165
                $this->assertSame('An error occurred: not found', $e->getMessage());
166
            }
167
            $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...
168
        }
169
    }
170
171
    public function testGetDailyWeatherForecast()
172
    {
173
        $days = 16;
174
        $dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
175
176
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast);
177
    }
178
179
    public function testGetWeatherHistory()
180
    {
181
        $this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.');
182
    }
183
184
    public function testWasCached()
185
    {
186
        $weather = $this->owm;
187
        $result = $weather->wasCached();
188
189
        $this->assertFalse($result);
190
    }
191
192
    public function testCached()
193
    {
194
        $cache = $this->cache;
195
        $cache->setTempPath(__DIR__.'/temps');
196
        $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...
197
        $currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
198
        $cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
199
        
200
        $this->assertInternalType('string', $currWeatherData);
201
        $this->assertInternalType('string', $cachedWeatherData);
202
    }
203
204
    public function testBuildQueryUrlParameter()
205
    {
206
        $weather = $this->owm;
207
        $queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', '');
208
        $queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', '');
209
210
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray);
211
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray);
212
    }
213
214
    public function testAbstractCache()
215
    {
216
        /** @var OpenWeatherMap\AbstractCache $sut */
217
        $sut = $this->getMockForAbstractClass('\Cmfcmf\OpenWeatherMap\AbstractCache');
218
        $this->assertNull($sut->setSeconds(10));
219
    }
220
}
221