Completed
Push — v3 ( cbcde0...d23911 )
by Christian
03:01
created

OpenWeatherMapTest::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
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
use Cache\Adapter\PHPArray\ArrayCachePool;
21
22
class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $apiKey;
28
29
    /**
30
     * @var OpenWeatherMap
31
     */
32
    protected $owm;
33
34
    /**
35
     * @var OpenWeatherMap
36
     */
37
    protected $openWeather;
38
39
    /**
40
     * @var \Psr\SimpleCache\CacheInterface
41
     */
42
    protected $cache;
43
44
    protected function setUp()
45
    {
46
        $ini = parse_ini_file(__DIR__.'/../Examples/ApiKey.ini');
47
        $this->apiKey = $ini['api_key'];
48
        $this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher());
49
        $this->openWeather = new OpenWeatherMap($this->apiKey);
50
        $this->cache =  new ArrayCachePool();
51
    }
52
53
    public function testApiKeyNotNull()
54
    {
55
        $weather = $this->owm;
56
        $apiKey = $weather->getApiKey();
57
58
        $this->assertSame($this->apiKey, $apiKey);
59
    }
60
61
    public function testSetApiKey()
62
    {
63
        $weather = $this->owm;
64
        $weather->setApiKey($this->apiKey);
65
        $apiKey = $weather->getApiKey();
66
67
        $this->assertSame($this->apiKey, $apiKey);
68
    }
69
70
    public function testGetApiKey()
71
    {
72
        $weather = $this->owm;
73
        $apiKey = $weather->getApiKey();
74
75
        $this->assertSame($this->apiKey, $apiKey);
76
    }
77
78
    public function testGetWeather()
79
    {
80
        $currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', '');
81
82
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather);
83
    }
84
85
    public function testGetWeatherGroup()
86
    {
87
        $currentWeather = $this->owm->getWeatherGroup(array('2950159'), 'imperial', 'en', '');
88
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
89
90
        $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...
91
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
92
    }
93
94
    public function testGetWeatherForecast()
95
    {
96
        $days = 1;
97
        $defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
98
99
        $days = 16;
100
        $maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
101
102
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay);
103
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
104
    }
105
106
    public function testGetCurrentUVIndex()
107
    {
108
        $owm = $this->openWeather;
109
        $result = $owm->getCurrentUVIndex(40.7, -74.2);
110
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
111
    }
112
113
    public function testGetForecastUVIndex()
114
    {
115
        $owm = $this->openWeather;
116
117
        try {
118
            $result = $owm->getForecastUVIndex(40.7, -74.2, 5);
119
        } catch (Exception $e) {
120
            // OWM might not actually have data for the timespan.
121
            $this->assertSame('An error occurred: not found', $e->getMessage());
122
        }
123
        $this->assertContainsOnlyInstancesOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
124
    }
125
126
    public function testGetHistoryUVIndex()
127
    {
128
        $owm = $this->openWeather;
129
130
        try {
131
            $start = new \DateTime('1969-08-15');
132
            $end = new \DateTime('1969-08-18');
133
            $result = $owm->getHistoricUVIndex(40.7, -74.2, $start, $end);
134
        } catch (Exception $e) {
135
            // OWM might not actually have data for the timespan.
136
            $this->assertSame('An error occurred: not found', $e->getMessage());
137
        }
138
        $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...
139
    }
140
141
    public function testGetDailyWeatherForecast()
142
    {
143
        $days = 16;
144
        $dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
145
146
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast);
147
    }
148
149
    public function testGetWeatherHistory()
150
    {
151
        $this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.');
152
    }
153
154
    public function testWasCached()
155
    {
156
        $weather = $this->owm;
157
        $result = $weather->wasCached();
158
159
        $this->assertFalse($result);
160
    }
161
162
    public function testCached()
163
    {
164
        $cache = $this->cache;
165
        $weather = new OpenWeatherMap($this->apiKey, new TestFetcher(), $cache, 600);
0 ignored issues
show
Documentation introduced by
$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...
166
        $currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
167
        $this->assertFalse($weather->wasCached());
168
        $cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
169
        $this->assertTrue($weather->wasCached());
170
171
        $this->assertInternalType('string', $currWeatherData);
172
        $this->assertInternalType('string', $cachedWeatherData);
173
        $this->assertSame($currWeatherData, $cachedWeatherData);
174
    }
175
176
    public function testBuildQueryUrlParameter()
177
    {
178
        $weather = $this->owm;
179
        $queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', '');
180
        $queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', '');
181
182
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray);
183
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray);
184
    }
185
}
186