|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Savchenko\Bundle\OpenWeatherMapBundle\Tests\Api; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
7
|
|
|
use GuzzleHttp\Psr7\Request; |
|
8
|
|
|
use GuzzleHttp\Psr7\Response; |
|
9
|
|
|
use Savchenko\Bundle\OpenWeatherMapBundle\Api\CurrentWeatherData; |
|
10
|
|
|
|
|
11
|
|
|
class CurrentWeatherDataTest extends \PHPUnit_Framework_TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Client|\PHPUnit_Framework_MockObject_MockObject |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $client; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var CurrentWeatherData |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $currentWeatherData; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get OpenWeatherMap response example |
|
24
|
|
|
* |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function getWeatherData() |
|
28
|
|
|
{ |
|
29
|
|
|
return json_encode([ |
|
30
|
|
|
'coord' => [ |
|
31
|
|
|
'lon' => -0.13, |
|
32
|
|
|
'lat' => 51.51, |
|
33
|
|
|
], |
|
34
|
|
|
'weather' => [[ |
|
35
|
|
|
'id' => 800, |
|
36
|
|
|
'main' => 'Clear', |
|
37
|
|
|
'description' => 'clear sky', |
|
38
|
|
|
'icon' => '01n', |
|
39
|
|
|
]], |
|
40
|
|
|
'main' => [ |
|
41
|
|
|
'temp' => 282.94, |
|
42
|
|
|
'pressure' => 1007, |
|
43
|
|
|
'humidity' => 71, |
|
44
|
|
|
'temp_min' => 281.15, |
|
45
|
|
|
'temp_max' => 285.25, |
|
46
|
|
|
], |
|
47
|
|
|
'wind' => [ |
|
48
|
|
|
'speed' => 3.6, |
|
49
|
|
|
'deg' => 100, |
|
50
|
|
|
], |
|
51
|
|
|
'clouds' => [ |
|
52
|
|
|
'all' => 0, |
|
53
|
|
|
], |
|
54
|
|
|
'dt' => 1460493742, |
|
55
|
|
|
'sys' => [ |
|
56
|
|
|
'country' => 'GB', |
|
57
|
|
|
'sunrise' => 1460437723, |
|
58
|
|
|
'sunset' => 1460487258, |
|
59
|
|
|
], |
|
60
|
|
|
'id' => 2643743, |
|
61
|
|
|
'name' => 'London', |
|
62
|
|
|
]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function setUp() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->client = self::getMockBuilder('GuzzleHttp\Client') |
|
71
|
|
|
->setMethods(['get']) |
|
72
|
|
|
->getMock(); |
|
73
|
|
|
|
|
74
|
|
|
$this->client->method('get') |
|
75
|
|
|
->willReturn(new Response(200, [], $this->getWeatherData())); |
|
76
|
|
|
|
|
77
|
|
|
$this->currentWeatherData = new CurrentWeatherData('abc', $this->client); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Override Guzzle get() method to throw exception |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $message |
|
84
|
|
|
* @param int $statusCode |
|
85
|
|
|
* @throws \InvalidArgumentException |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function overrideClientGetMethod(string $message, int $statusCode) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->client->method('get') |
|
90
|
|
|
->willThrowException( |
|
91
|
|
|
new ClientException( |
|
92
|
|
|
$message, |
|
93
|
|
|
new Request('GET', 'http://api.openweathermap.org/data/2.5/weather'), |
|
94
|
|
|
new Response($statusCode) |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\BadRequestException |
|
101
|
|
|
* @expectedExceptionMessage You doesn't set APPID parameter or it has incorrect value |
|
102
|
|
|
*/ |
|
103
|
|
View Code Duplication |
public function testAppIdProblem() |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
$this->overrideClientGetMethod( |
|
106
|
|
|
'Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.', |
|
107
|
|
|
401 |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
$this->currentWeatherData = new CurrentWeatherData('abc', $this->client); |
|
111
|
|
|
|
|
112
|
|
|
$this->currentWeatherData->loadByCityName('London'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\BadRequestException |
|
117
|
|
|
* @expectedExceptionMessage Data available only on commercial terms |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function testPaidFeatures() |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
$this->overrideClientGetMethod('', 403); |
|
122
|
|
|
|
|
123
|
|
|
$this->currentWeatherData = new CurrentWeatherData('abc', $this->client); |
|
124
|
|
|
|
|
125
|
|
|
$this->currentWeatherData->loadByCityName('London'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @expectedException \GuzzleHttp\Exception\ClientException |
|
130
|
|
|
*/ |
|
131
|
|
View Code Duplication |
public function testCatchNonExpectedException() |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
$this->overrideClientGetMethod('', 404); |
|
134
|
|
|
|
|
135
|
|
|
$this->currentWeatherData = new CurrentWeatherData('abc', $this->client); |
|
136
|
|
|
|
|
137
|
|
|
$this->currentWeatherData->loadByCityName('London'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testLoadByCityNameWithoutCountry() |
|
141
|
|
|
{ |
|
142
|
|
|
$response = $this->currentWeatherData->loadByCityName('London'); |
|
143
|
|
|
|
|
144
|
|
|
self::assertEquals('London', $response->name); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function testLoadByCityNameWithCorrectCountryCode() |
|
148
|
|
|
{ |
|
149
|
|
|
$response = $this->currentWeatherData->loadByCityName('London', 'GB'); |
|
150
|
|
|
|
|
151
|
|
|
self::assertEquals('London', $response->name); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\InvalidCountryCodeException |
|
156
|
|
|
* @expectedExceptionMessage You should provide ISO 3166-1 alpha-2 country code |
|
157
|
|
|
*/ |
|
158
|
|
|
public function testLoadByCityNameWithIncorrectCountryCode() |
|
159
|
|
|
{ |
|
160
|
|
|
$this->currentWeatherData->loadByCityName('London', 'UKR'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function testLoadByCityId() |
|
164
|
|
|
{ |
|
165
|
|
|
$response = $this->currentWeatherData->loadByCityId('2643743'); |
|
166
|
|
|
|
|
167
|
|
|
self::assertEquals('London', $response->name); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testLoadByGeographicCoordinates() |
|
171
|
|
|
{ |
|
172
|
|
|
$response = $this->currentWeatherData->loadByGeographicCoordinates(-0.13, 51.51); |
|
173
|
|
|
|
|
174
|
|
|
self::assertEquals('London', $response->name); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testLoadByZipCodeWithoutCountry() |
|
178
|
|
|
{ |
|
179
|
|
|
$response = $this->currentWeatherData->loadByZipCode('EC1A1AH'); |
|
180
|
|
|
|
|
181
|
|
|
self::assertEquals('London', $response->name); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function testLoadByZipCodeWithCorrectCountryCode() |
|
185
|
|
|
{ |
|
186
|
|
|
$response = $this->currentWeatherData->loadByZipCode('EC1A1AH', 'GB'); |
|
187
|
|
|
|
|
188
|
|
|
self::assertEquals('London', $response->name); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\InvalidCountryCodeException |
|
193
|
|
|
* @expectedExceptionMessage You should provide ISO 3166-1 alpha-2 country code |
|
194
|
|
|
*/ |
|
195
|
|
|
public function testLoadByZipCodeWithIncorrectCountryCode() |
|
196
|
|
|
{ |
|
197
|
|
|
$this->currentWeatherData->loadByZipCode('EC1A1AH', 'GBA'); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
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.