Completed
Pull Request — master (#134)
by Christian
13:33 queued 12:18
created

OpenWeatherMapExceptionTest::testCacheException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5
 *
6
 * @license MIT
7
 *
8
 * Please see the LICENSE file distributed with this source code for further
9
 * information regarding copyright and licensing.
10
 *
11
 * Please visit the following links to read about the usage policies and the license of
12
 * OpenWeatherMap data before using this library:
13
 *
14
 * @see https://OpenWeatherMap.org/price
15
 * @see https://OpenWeatherMap.org/terms
16
 * @see https://OpenWeatherMap.org/appid
17
 */
18
19
namespace Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap;
20
21
use Cmfcmf\OpenWeatherMap;
22
use Cmfcmf\OpenWeatherMap\Tests\TestHttpClient;
23
use Http\Factory\Guzzle\RequestFactory;
24
25
class OpenWeatherMapExceptionTest extends \PHPUnit_Framework_TestCase
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $apiKey;
31
32
    /**
33
     * @var OpenWeatherMap
34
     */
35
    protected $owm;
36
37
    protected function setUp()
38
    {
39
        $this->apiKey = 'unicorn-rainbow';
40
        $this->owm = new OpenWeatherMap($this->apiKey, new TestHttpClient(), new RequestFactory());
41
    }
42
43
    /**
44
     * @expectedException \InvalidArgumentException
45
     */
46
    public function testGetWeatherForecastException()
47
    {
48
        $days = 20;
49
        $this->owm->getWeatherForecast('Berlin', 'imperial', 'en', '', $days);
50
    }
51
52
    /**
53
     * @expectedException \InvalidArgumentException
54
     */
55
    public function testGetDailyWeatherForecastException()
56
    {
57
        $days = 20;
58
        $this->owm->getDailyWeatherForecast('Berlin', 'imperial', 'en', '', $days);
59
    }
60
61
    /**
62
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
63
     */
64
    public function testGetWeatherHistoryException()
65
    {
66
        $this->owm->getWeatherHistory('Berlin', new \DateTime('2015-11-01 00:00:00'), 1, 'hour', 'imperial', 'en', '');
67
    }
68
69
    /**
70
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
71
     */
72
    public function testGetWeatherHistoryWithEndException()
73
    {
74
        $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...
75
    }
76
77
    /**
78
     * @expectedException \InvalidArgumentException
79
     */
80
    public function testGetWeatherHistoryInvalidArgumentException()
81
    {
82
        $this->owm->getWeatherHistory('Berlin', new \DateTime('now'), 1, 'wrong-type', 'imperial', 'en', '');
83
    }
84
85
    /**
86
     * @expectedException \InvalidArgumentException
87
     */
88
    public function testGetRawDailyForecastDataInvalidArgumentException()
89
    {
90
        $this->owm->getRawDailyForecastData('Berlin', 'imperial', 'en', '', 'xml', 20);
91
    }
92
93
    /**
94
     * @expectedException \InvalidArgumentException
95
     */
96
    public function testGetRawWeatherHistoryException()
97
    {
98
        $this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 1, 'wrong-type', 'imperial', 'en', '');
99
    }
100
101
    /**
102
     * @expectedException \InvalidArgumentException
103
     */
104
    public function testGetRawWeatherHistoryWithEndDateException()
105
    {
106
        $this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 'wrong-endOrCount', 'hour', 'imperial', 'en', '');
107
    }
108
109
    /**
110
     * @expectedException \InvalidArgumentException
111
     * @dataProvider      uvIndexExceptionDataProvider
112
     */
113
    public function testGetRawUVIndexWithQueryErrorException($mode, $lat, $lon, $cnt, $start, $end)
114
    {
115
        $this->owm->getRawUVIndexData($mode, $lat, $lon, $cnt, $start, $end);
116
    }
117
118
    /**
119
     * @expectedException \InvalidArgumentException
120
     */
121
    public function testBuildQueryUrlParameterException()
122
    {
123
        $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...
124
    }
125
126
    /**
127
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
128
     */
129 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...
130
    {
131
        $answer = 'I am not XML formatted data';
132
        $method = new \ReflectionMethod($this->owm, 'parseXML');
133
        $method->setAccessible(true);
134
135
        $method->invoke($this->owm, $answer);
136
    }
137
138
    /**
139
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
140
     */
141
    public function testParseXMLWithIsJsonException()
142
    {
143
        $answer = array('message' => 'simple json data');
144
        $answer = json_encode($answer);
145
        $method = new \ReflectionMethod($this->owm, 'parseXML');
146
        $method->setAccessible(true);
147
148
        $method->invoke($this->owm, $answer);
149
    }
150
151
    /**
152
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
153
     */
154 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...
155
    {
156
        $answer = 'I am not a json format data';
157
        $method = new \ReflectionMethod($this->owm, 'parseJson');
158
        $method->setAccessible(true);
159
160
        $method->invoke($this->owm, $answer);
161
    }
162
163
    /**
164
     * @expectedException \Cmfcmf\OpenWeatherMap\Exception
165
     */
166
    public function uvIndexExceptionDataProvider()
167
    {
168
        return array(
169
            array('current', 5.4, 1, 5, null, null),
170
            array('forecast', 5.4, 1.2, '5', null, null),
171
            array('forecast', 5.4, 1.2, 0, null, null),
172
            array('forecast', 5.4, 1.2, 9, null, null),
173
            array('forecast', 5.4, 1.2, 5, new \DateTime(), new \DateTime()),
174
            array('forecast', 5.4, 12.0, null, '2000-1-1', null),
175
            array('historic', 5.4, 1.2, null, new \DateTime(), '2000-1-1'),
176
            array('historic', 5.4, 1.2, 5, new \DateTime(), new \DateTime()),
177
        );
178
    }
179
}
180