Completed
Pull Request — master (#93)
by lee
12:27
created

OpenWeatherMapTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 154
Duplicated Lines 14.94 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 8 8 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() 7 7 1
A testGetWeatherGroup() 0 7 1
A testGetWeatherForecast() 0 12 1
A testGetDailyWeatherForecast() 8 8 1
A testGetWeatherHistory() 0 4 1
A testWasCached() 0 7 1
A testCached() 0 13 1
A testBuildQueryUrlParameter() 0 9 1
A testAbstractCache() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\WeatherHistory;
19
use \Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap\ExampleCacheTest;
20
21
class OpenWeatherMapTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var apiKey
25
     * @var weather
26
     * @var cache
27
     */
28
    protected $apiKey;
29
    protected $weather;
30
    protected $cache;
31
32 View Code Duplication
    protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        $ini = parse_ini_file(__DIR__ . '/ApiKey.ini');
35
        $apiKey = $ini['api_key'];
36
        $this->apiKey = $apiKey;
37
        $this->weather = new OpenWeatherMap($this->apiKey, null, false, 600);
38
        $this->cache = new ExampleCacheTest();
39
    }
40
41
    protected function tearDown()
42
    {
43
        $fileList = glob(__DIR__.'/temps/OpenWeatherMapPHPAPI/*');
44
        foreach ($fileList as $fileName) {
45
            @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...
46
        }
47
48
        @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...
49
        @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...
50
    }
51
52
    public function testApiKeyIsEmpty()
53
    {
54
        $expectApiKey = '';
55
        $weather = new OpenWeatherMap($expectApiKey, null, false, 600);
56
        $apiKey = $weather->getApiKey();
57
58
        $this->assertSame($expectApiKey, $apiKey);
59
    }
60
61
    public function testApiKeyNotNull()
62
    {
63
        $weather = $this->weather;
64
        $apiKey = $weather->getApiKey();
65
66
        $this->assertSame($this->apiKey, $apiKey);
67
    }
68
69
    public function testSecondIsZero()
70
    {
71
        $weather = new OpenWeatherMap($this->apiKey, null, false, 0);
72
        $apiKey = $weather->getApiKey();
73
74
        $this->assertSame($this->apiKey, $apiKey);
75
    }
76
77
    public function testSetApiKey()
78
    {
79
        $weather = $this->weather;
80
        $weather->setApiKey($this->apiKey);
81
        $apiKey = $weather->getApiKey();
82
83
        $this->assertSame($this->apiKey, $apiKey);
84
    }
85
86
    public function testGetApiKey()
87
    {
88
        $weather = $this->weather;
89
        $apiKey = $weather->getApiKey();
90
91
        $this->assertSame($this->apiKey, $apiKey);
92
    }
93
94 View Code Duplication
    public function testGetWeather()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $weather = $this->weather;
97
        $currentWeather = $weather->getWeather('Berlin', 'imperial', 'en', '');
98
99
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $currentWeather);
100
    }
101
102
    public function testGetWeatherGroup()
103
    {
104
        $weather = $this->weather;
105
        $currentWeather = $weather->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...
106
107
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeatherGroup', $currentWeather);
108
    }
109
110
    public function testGetWeatherForecast()
111
    {
112
        $days = 1;
113
        $weather = $this->weather;
114
        $defaultDay = $weather->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
115
        
116
        $days = 16;
117
        $maxDay = $weather->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
118
119
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $defaultDay);
120
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $maxDay);
121
    }
122
123 View Code Duplication
    public function testGetDailyWeatherForecast()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $days = 16;
126
        $weather = $this->weather;
127
        $dailyForecast = $weather->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
128
129
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\WeatherForecast', $dailyForecast);
130
    }
131
132
    public function testGetWeatherHistory()
133
    {
134
        $this->markTestSkipped('This getWeatherHistory method ignored because the api key need to have a paid permission.');
135
    }
136
137
    public function testWasCached()
138
    {
139
        $weather = $this->weather;
140
        $result = $weather->wasCached();
141
142
        $this->assertFalse($result);
143
    }
144
145
    public function testCached()
146
    {
147
        $sampleUrl = 'http://api.openweathermap.org/data/2.5/weather?q=Berlin&units=imperial&lang=en&mode=json&APPID=50b2ae8523a458183982bf56254556dc';
0 ignored issues
show
Unused Code introduced by
$sampleUrl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
148
149
        $cache = $this->cache;
150
        $cache->setTempPath(__DIR__.'/temps');
151
        $weather = new OpenWeatherMap($this->apiKey, null, $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...
152
        $currWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'json');
153
        $cachedWeatherData = $weather->getRawWeatherData('Berlin', 'imperial', 'en', $this->apiKey, 'json');
154
        
155
        $this->assertInternalType('string', $currWeatherData);
156
        $this->assertInternalType('string', $cachedWeatherData);
157
    }
158
159
    public function testBuildQueryUrlParameter()
160
    {
161
        $weather = $this->weather;
162
        $queryWithNumbericArray = $weather->getWeather(array('2950159'), 'imperial', 'en', '');
163
        $queryWithLatLonArray = $weather->getWeather(array('lat' => 52.524368, 'lon' => 13.410530), 'imperial', 'en', '');
164
165
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithNumbericArray);
166
        $this->assertInstanceOf('\Cmfcmf\OpenWeatherMap\CurrentWeather', $queryWithLatLonArray);
167
    }
168
169
    public function testAbstractCache()
170
    {
171
        $sut = $this->getMockForAbstractClass('\Cmfcmf\OpenWeatherMap\AbstractCache');
172
        $this->assertNull($sut->setSeconds(10));
173
    }
174
}
175