Completed
Push — peter279k-master ( fcaf21 )
by Christian
13:01
created

OpenWeatherMapTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 152
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A tearDown() 0 10 2
A testApiKeyIsEmpty() 0 8 1
A testApiKeyNotNull() 0 7 1
A testSecondIsZero() 0 7 1
A testSetApiKey() 0 8 1
A testGetApiKey() 0 7 1
A testGetWeather() 0 6 1
A testGetWeatherGroup() 0 6 1
A testGetWeatherForecast() 0 11 1
A testGetDailyWeatherForecast() 0 7 1
A testGetWeatherHistory() 0 4 1
A testWasCached() 0 7 1
A testCached() 0 11 1
A testBuildQueryUrlParameter() 0 9 1
A testAbstractCache() 0 5 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\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 ExampleCacheTest
36
     */
37
    protected $cache;
38
39
    protected function setUp()
40
    {
41
        $this->apiKey = 'unicorn-rainbow';
42
        $this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600);
43
        $this->cache = new ExampleCacheTest();
44
    }
45
46
    protected function tearDown()
47
    {
48
        $fileList = glob(__DIR__.'/temps/OpenWeatherMapPHPAPI/*');
49
        foreach ($fileList as $fileName) {
50
            @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...
51
        }
52
53
        @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...
54
        @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...
55
    }
56
57
    public function testApiKeyIsEmpty()
58
    {
59
        $expectApiKey = '';
60
        $weather = new OpenWeatherMap($expectApiKey, null, false, 600);
61
        $apiKey = $weather->getApiKey();
62
63
        $this->assertSame($expectApiKey, $apiKey);
64
    }
65
66
    public function testApiKeyNotNull()
67
    {
68
        $weather = $this->owm;
69
        $apiKey = $weather->getApiKey();
70
71
        $this->assertSame($this->apiKey, $apiKey);
72
    }
73
74
    public function testSecondIsZero()
75
    {
76
        $weather = new OpenWeatherMap($this->apiKey, null, false, 0);
77
        $apiKey = $weather->getApiKey();
78
79
        $this->assertSame($this->apiKey, $apiKey);
80
    }
81
82
    public function testSetApiKey()
83
    {
84
        $weather = $this->owm;
85
        $weather->setApiKey($this->apiKey);
86
        $apiKey = $weather->getApiKey();
87
88
        $this->assertSame($this->apiKey, $apiKey);
89
    }
90
91
    public function testGetApiKey()
92
    {
93
        $weather = $this->owm;
94
        $apiKey = $weather->getApiKey();
95
96
        $this->assertSame($this->apiKey, $apiKey);
97
    }
98
99
    public function testGetWeather()
100
    {
101
        $currentWeather = $this->owm->getWeather('Berlin', 'imperial', 'en', '');
102
103
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather);
104
    }
105
106
    public function testGetWeatherGroup()
107
    {
108
        $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...
109
110
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
111
    }
112
113
    public function testGetWeatherForecast()
114
    {
115
        $days = 1;
116
        $defaultDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
117
        
118
        $days = 16;
119
        $maxDay = $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
120
121
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay);
122
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
123
    }
124
125
    public function testGetDailyWeatherForecast()
126
    {
127
        $days = 16;
128
        $dailyForecast = $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
129
130
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast);
131
    }
132
133
    public function testGetWeatherHistory()
134
    {
135
        $this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.');
136
    }
137
138
    public function testWasCached()
139
    {
140
        $weather = $this->owm;
141
        $result = $weather->wasCached();
142
143
        $this->assertFalse($result);
144
    }
145
146
    public function testCached()
147
    {
148
        $cache = $this->cache;
149
        $cache->setTempPath(__DIR__.'/temps');
150
        $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...
151
        $currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
152
        $cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'xml');
153
        
154
        $this->assertInternalType('string', $currWeatherData);
155
        $this->assertInternalType('string', $cachedWeatherData);
156
    }
157
158
    public function testBuildQueryUrlParameter()
159
    {
160
        $weather = $this->owm;
161
        $queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', '');
162
        $queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', '');
163
164
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray);
165
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray);
166
    }
167
168
    public function testAbstractCache()
169
    {
170
        $sut = $this->getMockForAbstractClass('\Cmfcmf\OpenWeatherMap\AbstractCache');
171
        $this->assertNull($sut->setSeconds(10));
172
    }
173
}
174