Completed
Pull Request — master (#134)
by Christian
02:30 queued 01:24
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 Method

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