Completed
Pull Request — v3 (#135)
by Christian
30:58 queued 15:52
created

OpenWeatherMapExceptionTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 155
Duplicated Lines 10.32 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 16
loc 155
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 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 testBuildQueryUrlParameterException() 0 4 1
A testParseXMLException() 8 8 1
A testParseXMLWithIsJsonException() 0 9 1
A testParseJsonException() 8 8 1
A uvIndexExceptionDataProvider() 0 13 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\TestHttpClient;
19
use Http\Factory\Guzzle\RequestFactory;
20
21
class OpenWeatherMapExceptionTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $apiKey;
27
28
    /**
29
     * @var OpenWeatherMap
30
     */
31
    protected $owm;
32
33
    protected function setUp()
34
    {
35
        $this->apiKey = 'unicorn-rainbow';
36
        $this->owm = new OpenWeatherMap($this->apiKey, new TestHttpClient(), new RequestFactory());
37
    }
38
39
    /**
40
     * @expectedException \InvalidArgumentException
41
     */
42
    public function testGetWeatherForecastException()
43
    {
44
        $days = 20;
45
        $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
46
    }
47
48
    /**
49
     * @expectedException \InvalidArgumentException
50
     */
51
    public function testGetDailyWeatherForecastException()
52
    {
53
        $days = 20;
54
        $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
55
    }
56
57
    /**
58
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
59
     */
60
    public function testGetWeatherHistoryException()
61
    {
62
        $this->owm->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), 1, 'hour', 'imperial', 'en', '');
63
    }
64
65
    /**
66
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
67
     */
68
    public function testGetWeatherHistoryWithEndException()
69
    {
70
        $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...
71
    }
72
73
    /**
74
     * @expectedException \InvalidArgumentException
75
     */
76
    public function testGetWeatherHistoryInvalidArgumentException()
77
    {
78
        $this->owm->getWeatherHistory('Berlin', new \DateTime('now'), 1, 'wrong-type', 'imperial', 'en', '');
79
    }
80
81
    /**
82
     * @expectedException \InvalidArgumentException
83
     */
84
    public function testGetRawDailyForecastDataInvalidArgumentException()
85
    {
86
        $this->owm->getRawDailyForecastData('Berlin', 'imperial', 'en', '', 'xml', 20);
87
    }
88
89
    /**
90
     * @expectedException \InvalidArgumentException
91
     */
92
    public function testGetRawWeatherHistoryException()
93
    {
94
        $this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 1, 'wrong-type', 'imperial', 'en', '');
95
    }
96
97
    /**
98
     * @expectedException \InvalidArgumentException
99
     */
100
    public function testGetRawWeatherHistoryWithEndDateException()
101
    {
102
        $this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 'wrong-endOrCount', 'hour', 'imperial', 'en', '');
103
    }
104
105
    /**
106
     * @expectedException \InvalidArgumentException
107
     * @dataProvider      uvIndexExceptionDataProvider
108
     */
109
    public function testGetRawUVIndexWithQueryErrorException($mode, $lat, $lon, $cnt, $start, $end)
110
    {
111
        $this->owm->getRawUVIndexData($mode, $lat, $lon, $cnt, $start, $end);
112
    }
113
114
    /**
115
     * @expectedException \InvalidArgumentException
116
     */
117
    public function testBuildQueryUrlParameterException()
118
    {
119
        $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...
120
    }
121
122
    /**
123
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
124
     */
125 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...
126
    {
127
        $answer = 'I am not XML formatted data';
128
        $method = new \ReflectionMethod($this->owm, 'parseXML');
129
        $method->setAccessible(true);
130
131
        $method->invoke($this->owm, $answer);
132
    }
133
134
    /**
135
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
136
     */
137
    public function testParseXMLWithIsJsonException()
138
    {
139
        $answer = array('message' => 'simple json data');
140
        $answer = json_encode($answer);
141
        $method = new \ReflectionMethod($this->owm, 'parseXML');
142
        $method->setAccessible(true);
143
144
        $method->invoke($this->owm, $answer);
145
    }
146
147
    /**
148
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
149
     */
150 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...
151
    {
152
        $answer = 'I am not a json format data';
153
        $method = new \ReflectionMethod($this->owm, 'parseJson');
154
        $method->setAccessible(true);
155
156
        $method->invoke($this->owm, $answer);
157
    }
158
159
    /**
160
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
161
     */
162
    public function uvIndexExceptionDataProvider()
163
    {
164
        return array(
165
            array('current', 5.4, 1, 5, null, null),
166
            array('forecast', 5.4, 1.2, '5', null, null),
167
            array('forecast', 5.4, 1.2, 0, null, null),
168
            array('forecast', 5.4, 1.2, 9, null, null),
169
            array('forecast', 5.4, 1.2, 5, new \DateTime(), new \DateTime()),
170
            array('forecast', 5.4, 12.0, null, '2000-1-1', null),
171
            array('historic', 5.4, 1.2, null, new \DateTime(), '2000-1-1'),
172
            array('historic', 5.4, 1.2, 5, new \DateTime(), new \DateTime()),
173
        );
174
    }
175
}
176