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

WeatherHistoryTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 162
Duplicated Lines 56.17 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 91
loc 162
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 42 1
A testRewind() 9 9 1
A testCurrent() 0 8 1
A testNext() 9 9 1
A testValid() 0 9 1
B testJsonHasCountryAndPopulation() 36 36 1
B testJsonWithRainKey() 37 37 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\WeatherHistory;
18
19
class WeatherHistoryTest extends \PHPUnit_Framework_TestCase
20
{
21
    protected $fakeJson;
22
    protected $history;
23
24
    protected function setUp()
25
    {
26
        $this->fakeJson = '{
27
            "cod":"200","calctime":"123456789","message":0.0032,"city_id":{"id":1851632,"name":"Shuzenji","coord":{"lon":138.933334,"lat":34.966671},"country":"JP"},
28
            "cnt":10,
29
            "list":[{
30
                "dt":1406080800,
31
                "temp":{
32
                    "day":297.77,
33
                    "min":293.52,
34
                    "max":297.77,
35
                    "night":293.52,
36
                    "eve":297.77,
37
                    "morn":297.77
38
                },
39
                "pressure":925.04,
40
                "humidity":76,
41
                "weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],
42
                "main":{"temp":306.15,"pressure":1013,"humidity":44,"temp_min":306,"temp_max":306},
43
                "clouds":{"all":90},
44
                "wind":{"speed":5.71,"deg":229.501}
45
            },{
46
                "dt":1406080800,
47
                "temp":{
48
                    "day":297.77,
49
                    "min":293.52,
50
                    "max":297.77,
51
                    "night":293.52,
52
                    "eve":297.77,
53
                    "morn":297.77
54
                },
55
                "pressure":925.04,
56
                "humidity":76,
57
                "weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],
58
                "main":{"temp":306.15,"pressure":1013,"humidity":44,"temp_min":306,"temp_max":306},
59
                "clouds":{"all":90},
60
                "wind":{"speed":5.71,"deg":229.501}
61
            }]
62
        }';
63
        $this->fakeJson = json_decode($this->fakeJson, true);
64
        $this->history = new WeatherHistory($this->fakeJson, 'Berlin');
65
    }
66
67 View Code Duplication
    public function testRewind()
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...
68
    {
69
        $expectIndex = 0;
70
        $history = $this->history;
71
        $history->rewind();
72
        $position = $history->key();
73
74
        $this->assertSame($expectIndex, $position);
75
    }
76
77
    public function testCurrent()
78
    {
79
        $history = $this->history;
80
        $history->rewind();
81
        $current = $history->current();
82
83
        $this->assertInternalType('object', $current);
84
    }
85
86 View Code Duplication
    public function testNext()
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...
87
    {
88
        $expectIndex = 1;
89
        $history = $this->history;
90
        $history->next();
91
        $position = $history->key();
92
93
        $this->assertSame($expectIndex, $position);
94
    }
95
96
    public function testValid()
97
    {
98
        $history = $this->history;
99
        $history->rewind();
100
        $history->next();
101
        $result = $history->valid();
102
103
        $this->assertTrue($result);
104
    }
105
106 View Code Duplication
    public function testJsonHasCountryAndPopulation()
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...
107
    {
108
        $fakeJson = '{
109
            "cod":"200","calctime":"123456789","message":0.0032,"city_id":{"id":1851632,"name":"Shuzenji","coord":{"lon":138.933334,"lat":34.966671},"country":"JP"},
110
            "cnt":10,
111
            "list":[{
112
                "city":{
113
                    "country": "Berlin", 
114
                    "population": "10,000"
115
                }
116
                },{
117
                    "dt":1406080800,
118
                    "temp":{
119
                        "day":297.77,
120
                        "min":293.52,
121
                        "max":297.77,
122
                        "night":293.52,
123
                        "eve":297.77,
124
                        "morn":297.77
125
                    },
126
                    "pressure":925.04,
127
                    "humidity":76,
128
                    "weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],
129
                    "main":{"temp":306.15,"pressure":1013,"humidity":44,"temp_min":306,"temp_max":306},
130
                    "clouds":{"all":90},
131
                    "wind":{"speed":5.71,"deg":229.501}
132
            }]
133
        }';
134
        $fakeJson = json_decode($fakeJson, true);
135
        $history = new WeatherHistory($fakeJson, 'Berlin');
136
137
        $history->rewind();
138
        $result = $history->valid();
139
140
        $this->assertTrue($result);
141
    }
142
143 View Code Duplication
    public function testJsonWithRainKey()
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...
144
    {
145
        $fakeJson = '{
146
            "cod":"200","calctime":"123456789","message":0.0032,"city_id":{"id":1851632,"name":"Shuzenji","coord":{"lon":138.933334,"lat":34.966671},"country":"JP"},
147
            "cnt":10,
148
            "list":[{
149
                "city":{
150
                    "country": "Berlin", 
151
                    "population": "10,000"
152
                }
153
                },{
154
                    "dt":1406080800,
155
                    "temp":{
156
                        "day":297.77,
157
                        "min":293.52,
158
                        "max":297.77,
159
                        "night":293.52,
160
                        "eve":297.77,
161
                        "morn":297.77
162
                    },
163
                    "pressure":925.04,
164
                    "humidity":76,
165
                    "weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],
166
                    "main":{"temp":306.15,"pressure":1013,"humidity":44,"temp_min":306,"temp_max":306},
167
                    "clouds":{"all":90},
168
                    "wind":{"speed":5.71,"deg":229.501},
169
                    "rain":{"3h":3}
170
            }]
171
        }';
172
        $fakeJson = json_decode($fakeJson, true);
173
        $history = new WeatherHistory($fakeJson, 'Berlin');
174
        
175
        $history->rewind();
176
        $result = $history->valid();
177
178
        $this->assertTrue($result);
179
    }
180
}
181