Completed
Push — uv-index ( 44d314...153dea )
by Christian
02:09
created

OpenWeatherMapTest::testGetCurrentUVIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
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
117
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
118
    }
119
120
    public function testGetWeatherForecast()
121
    {
122
        $days = 1;
123
        $defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
124
        
125
        $days = 16;
126
        $maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
127
128
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay);
129
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
130
    }
131
132
    public function testGetCurrentUVIndex()
133
    {
134
        $owm = $this->openWeather;
135
        $result = $owm->getCurrentUVIndex(40.7, -74.2);
136
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
137
    }
138
139
    public function testGetUVIndex()
140
    {
141
        $owm = $this->openWeather;
142
        $precisions = array('year', 'month', 'day', 'hour', 'minute', 'second');
143
        foreach ($precisions as $precision) {
144
            try {
145
                $result = $owm->getUVIndex(40.7, -74.2, new \DateTime(), $precision);
0 ignored issues
show
Documentation introduced by
new \DateTime() is of type object<DateTime>, but the function expects a object<DateTimeInterface>.

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