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

OpenWeatherMapExceptionTest   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 199
Duplicated Lines 8.04 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 21
lcom 1
cbo 2
dl 16
loc 199
rs 10
c 1
b 0
f 1

21 Methods

Rating   Name   Duplication   Size   Complexity  
A testSecondNotNumericException() 0 4 1
A setUp() 0 5 1
A testCacheException() 0 4 1
A testGetWeatherForecastException() 0 5 1
A testGetDailyWeatherForecastException() 0 5 1
A testGetWeatherHistoryException() 0 4 1
A testGetWeatherHistoryWithEndException() 0 4 1
A testGetWeatherHistoryInvalidArgumentException() 0 4 1
A testGetRawDailyForecastDataInvalidArgumentException() 0 4 1
A testGetRawWeatherHistoryException() 0 4 1
A testGetRawWeatherHistoryWithEndDateException() 0 4 1
A testGetRawUVIndexWithQueryErrorException() 0 4 1
A testGetRawCurrentUVIndexWithQueryErrorException() 0 4 1
A testGetRawUVIndexWithoutApiKey() 0 5 1
A testGetRawCurrentUVIndexWithoutApiKey() 0 5 1
A testBuildQueryUrlParameterException() 0 4 1
A testParseXMLException() 8 8 1
A testParseXMLWithIsJsonException() 0 9 1
A testParseJsonException() 8 8 1
A uvIndexExceptionDataProvider() 0 8 1
A currentUVIndexExceptionDataProvider() 0 8 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\Tests\TestFetcher;
19
20
class OpenWeatherMapExceptionTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $apiKey;
26
27
    /**
28
     * @var OpenWeatherMap
29
     */
30
    protected $owm;
31
32
    protected function setUp()
33
    {
34
        $this->apiKey = 'unicorn-rainbow';
35
        $this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600);
36
    }
37
38
    /**
39
     * @expectedException \InvalidArgumentException
40
     */
41
    public function testCacheException()
42
    {
43
        new OpenWeatherMap($this->apiKey, null, true, 600);
44
    }
45
46
    /**
47
     * @expectedException \InvalidArgumentException
48
     */
49
    public function testSecondNotNumericException()
50
    {
51
        new OpenWeatherMap($this->apiKey, null, false, 'I am not numeric');
52
    }
53
54
    /**
55
     * @expectedException \InvalidArgumentException
56
     */
57
    public function testGetWeatherForecastException()
58
    {
59
        $days = 20;
60
        $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
61
    }
62
63
    /**
64
     * @expectedException \InvalidArgumentException
65
     */
66
    public function testGetDailyWeatherForecastException()
67
    {
68
        $days = 20;
69
        $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
70
    }
71
72
    /**
73
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
74
     */
75
    public function testGetWeatherHistoryException()
76
    {
77
        $this->owm->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), 1, 'hour', 'imperial', 'en', '');
78
    }
79
80
    /**
81
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
82
     */
83
    public function testGetWeatherHistoryWithEndException()
84
    {
85
        $this->owm->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), new \DateTime('now'), 'hour', 'imperial', 'en', '');
0 ignored issues
show
Documentation introduced by
new \DateTime('now') is of type object<DateTime>, but the function expects a integer.

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...
86
    }
87
88
     /**
89
      * @expectedException \InvalidArgumentException
90
      */
91
    public function testGetWeatherHistoryInvalidArgumentException()
92
    {
93
        $this->owm->getWeatherHistory('Berlin', new \DateTime('now'), 1, 'wrong-type', 'imperial', 'en', '');
94
    }
95
96
    /**
97
     * @expectedException \InvalidArgumentException
98
     */
99
    public function testGetRawDailyForecastDataInvalidArgumentException()
100
    {
101
        $this->owm->getRawDailyForecastData('Berlin', 'imperial', 'en', '', 'xml', 20);
102
    }
103
104
    /**
105
     * @expectedException \InvalidArgumentException
106
     */
107
    public function testGetRawWeatherHistoryException()
108
    {
109
        $this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 1, 'wrong-type', 'imperial', 'en', '');
110
    }
111
112
    /**
113
     * @expectedException \InvalidArgumentException
114
     */
115
    public function testGetRawWeatherHistoryWithEndDateException()
116
    {
117
        $this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 'wrong-endOrCount', 'hour', 'imperial', 'en', '');
118
    }
119
120
    /**
121
     * @expectedException \InvalidArgumentException
122
     * @dataProvider      uvIndexExceptionDataProvider
123
     */
124
    public function testGetRawUVIndexWithQueryErrorException($lat, $lon, $dateTime)
125
    {
126
        $this->owm->getRawUVIndexData($lat, $lon, $dateTime);
127
    }
128
129
    /**
130
     * @expectedException \InvalidArgumentException
131
     * @dataProvider      currentUVIndexExceptionDataProvider
132
     */
133
    public function testGetRawCurrentUVIndexWithQueryErrorException($lat, $lon)
134
    {
135
        $this->owm->getRawCurrentUVIndexData($lat, $lon);
136
    }
137
138
    /**
139
     * @expectedException \RuntimeException
140
     */
141
    public function testGetRawUVIndexWithoutApiKey()
142
    {
143
        $this->owm->setApiKey(null);
144
        $this->owm->getRawUVIndexData(1.1, 1.1, 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...
145
    }
146
147
    /**
148
     * @expectedException \RuntimeException
149
     */
150
    public function testGetRawCurrentUVIndexWithoutApiKey()
151
    {
152
        $this->owm->setApiKey(null);
153
        $this->owm->getRawCurrentUVIndexData(1.1, 1.1);
154
    }
155
156
    /**
157
     * @expectedException \InvalidArgumentException
158
     */
159
    public function testBuildQueryUrlParameterException()
160
    {
161
        $this->owm->getWeather(true, 'imperial', 'en', '');
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a array|integer|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...
162
    }
163
164
    /**
165
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
166
     */
167 View Code Duplication
    public function testParseXMLException()
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...
168
    {
169
        $answer = 'I am not XML formatted data';
170
        $method = new \ReflectionMethod($this->owm, 'parseXML');
171
        $method->setAccessible(true);
172
        
173
        $method->invoke($this->owm, $answer);
174
    }
175
176
    /**
177
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
178
     */
179
    public function testParseXMLWithIsJsonException()
180
    {
181
        $answer = array('message' => 'simple json data');
182
        $answer = json_encode($answer);
183
        $method = new \ReflectionMethod($this->owm, 'parseXML');
184
        $method->setAccessible(true);
185
186
        $method->invoke($this->owm, $answer);
187
    }
188
189
    /**
190
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
191
     */
192 View Code Duplication
    public function testParseJsonException()
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...
193
    {
194
        $answer = 'I am not a json format data';
195
        $method = new \ReflectionMethod($this->owm, 'parseJson');
196
        $method->setAccessible(true);
197
        
198
        $method->invoke($this->owm, $answer);
199
    }
200
201
    public function uvIndexExceptionDataProvider()
202
    {
203
        return array(
204
            array('error-query-format', 'foo', new \DateTime()),
205
            array(5.4, 1.2, 'foo'),
206
            array(5.4, 12, 'foo'),
207
        );
208
    }
209
210
    public function currentUVIndexExceptionDataProvider()
211
    {
212
        return array(
213
            array('error-query-format', 'foo'),
214
            array(5.4, 12),
215
            array(5.4, '1.2'),
216
        );
217
    }
218
}
219