Completed
Push — uv-index ( 9af66d )
by Christian
02:24
created

OpenWeatherMapTest::testGetUVIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
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\Tests\TestFetcher;
19
use \Cmfcmf\OpenWeatherMap\WeatherHistory;
20
use \Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap\ExampleCacheTest;
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 ExampleCacheTest
41
     */
42
    protected $cache;
43
44
    protected function setUp()
45
    {
46
        $ini = parse_ini_file(__DIR__.'/../Examples/ApiKey.ini');
47
        $myApiKey = $ini['api_key'];
48
        $this->apiKey = $myApiKey;
49
        $this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600);
50
        $this->openWeather = new OpenWeatherMap($this->apiKey, null, false, 600);
51
        $this->cache = new ExampleCacheTest();
52
    }
53
54
    protected function tearDown()
55
    {
56
        $fileList = glob(__DIR__.'/temps/OpenWeatherMapPHPAPI/*');
57
        foreach ($fileList as $fileName) {
58
            @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...
59
        }
60
61
        @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...
62
        @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...
63
    }
64
65
    public function testApiKeyIsEmpty()
66
    {
67
        $expectApiKey = '';
68
        $weather = new OpenWeatherMap($expectApiKey, null, false, 600);
69
        $apiKey = $weather->getApiKey();
70
71
        $this->assertSame($expectApiKey, $apiKey);
72
    }
73
74
    public function testApiKeyNotNull()
75
    {
76
        $weather = $this->owm;
77
        $apiKey = $weather->getApiKey();
78
79
        $this->assertSame($this->apiKey, $apiKey);
80
    }
81
82
    public function testSecondIsZero()
83
    {
84
        $weather = new OpenWeatherMap($this->apiKey, null, false, 0);
85
        $apiKey = $weather->getApiKey();
86
87
        $this->assertSame($this->apiKey, $apiKey);
88
    }
89
90
    public function testSetApiKey()
91
    {
92
        $weather = $this->owm;
93
        $weather->setApiKey($this->apiKey);
94
        $apiKey = $weather->getApiKey();
95
96
        $this->assertSame($this->apiKey, $apiKey);
97
    }
98
99
    public function testGetApiKey()
100
    {
101
        $weather = $this->owm;
102
        $apiKey = $weather->getApiKey();
103
104
        $this->assertSame($this->apiKey, $apiKey);
105
    }
106
107
    public function testGetWeather()
108
    {
109
        $currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', '');
110
111
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather);
112
    }
113
114
    public function testGetWeatherGroup()
115
    {
116
        $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...
117
118
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
119
    }
120
121
    public function testGetWeatherForecast()
122
    {
123
        $days = 1;
124
        $defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
125
        
126
        $days = 16;
127
        $maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
128
129
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay);
130
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
131
    }
132
133
    public function testGetUVIndex()
134
    {
135
        $weather = $this->openWeather;
136
        $result = $weather->getUVIndex(40.7, -74.2, new \DateTime());
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...
137
138
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
139
    }
140
141
    public function testGetUUVIndexForYear()
142
    {
143
        $weather = $this->openWeather;
144
        $year = new \DateTime(date('Y'));
145
        $result = $weather->getUVIndex(40.7, -74.2, $year, 'year');
0 ignored issues
show
Documentation introduced by
$year 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
147
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\UVIndex', $result);
148
    }
149
150
    public function testGetDailyWeatherForecast()
151
    {
152
        $days = 16;
153
        $dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
154
155
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast);
156
    }
157
158
    public function testGetWeatherHistory()
159
    {
160
        // @TODO!!
161
        $this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.');
162
    }
163
164
    public function testWasCached()
165
    {
166
        $weather = $this->owm;
167
        $result = $weather->wasCached();
168
169
        $this->assertFalse($result);
170
    }
171
172
    public function testCached()
173
    {
174
        $cache = $this->cache;
175
        $cache->setTempPath(__DIR__.'/temps');
176
        $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...
177
        $currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
178
        $cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
179
        
180
        $this->assertInternalType('string', $currWeatherData);
181
        $this->assertInternalType('string', $cachedWeatherData);
182
    }
183
184
    public function testBuildQueryUrlParameter()
185
    {
186
        $weather = $this->owm;
187
        $queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', '');
188
        $queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', '');
189
190
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray);
191
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray);
192
    }
193
194
    public function testAbstractCache()
195
    {
196
        $sut = $this->getMockForAbstractClass('\Cmfcmf\OpenWeatherMap\AbstractCache');
197
        $this->assertNull($sut->setSeconds(10));
198
    }
199
}
200