1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE file |
5
|
|
|
* that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare (strict_types = 1); |
9
|
|
|
|
10
|
|
|
namespace Component\Remote\BurzeDzisNet; |
11
|
|
|
|
12
|
|
|
use PHPUnit_Framework_TestCase; |
13
|
|
|
use SoapClient; |
14
|
|
|
use stdClass; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@see BurzeDzisNet} test. |
18
|
|
|
* |
19
|
|
|
* @author Krzysztof Piasecki <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class BurzeDzisNetTest extends PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @covers Component\Remote\BurzeDzisNet\BurzeDzisNet::__construct |
25
|
|
|
*/ |
26
|
|
|
public function test__construct() |
27
|
|
|
{ |
28
|
|
|
$client = $this->getMockBuilder('\SoapClient')->disableOriginalConstructor()->getMock(); |
29
|
|
|
$endpoint = $this->getMockBuilder("Component\Remote\BurzeDzisNet\Endpoint") |
30
|
|
|
->disableOriginalConstructor() |
31
|
|
|
->setMethods(['client', 'apiKey']) |
32
|
|
|
->getMock(); |
33
|
|
|
$endpoint->expects($this->once())->method('client')->willReturn($client); |
34
|
|
|
$endpoint->expects($this->once())->method('apiKey'); |
35
|
|
|
new BurzeDzisNet($endpoint); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @covers Component\Remote\BurzeDzisNet\BurzeDzisNet::verifyApiKey |
40
|
|
|
*/ |
41
|
|
|
public function testVerifyApiKey() |
42
|
|
|
{ |
43
|
|
|
$client = $this->getMockBuilder('SoapClient') |
44
|
|
|
->disableOriginalConstructor() |
45
|
|
|
->setMethods(['KeyApi']) |
46
|
|
|
->getMock(); |
47
|
|
|
$map = [ |
48
|
|
|
['4d36bcb5c40', true], |
49
|
|
|
['f892dbc042f3', false], |
50
|
|
|
]; |
51
|
|
|
$client->method('KeyApi')->will($this->returnValueMap($map)); |
52
|
|
|
$endpoint = $this->getMockBuilder("Component\Remote\BurzeDzisNet\Endpoint") |
53
|
|
|
->disableOriginalConstructor() |
54
|
|
|
->setMethods(['client', 'apiKey']) |
55
|
|
|
->getMock(); |
56
|
|
|
$endpoint->method('client')->willReturn($client); |
57
|
|
|
$endpoint->method('apiKey')->willReturn('4d36bcb5c40'); |
58
|
|
|
$burzeDzisNet = new BurzeDzisNet($endpoint); |
59
|
|
|
$this->assertTrue($burzeDzisNet->verifyApiKey('4d36bcb5c40')); |
60
|
|
|
$this->assertFalse($burzeDzisNet->verifyApiKey('f892dbc042f3')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @covers Component\Remote\BurzeDzisNet\BurzeDzisNet::locate |
65
|
|
|
*/ |
66
|
|
|
public function testLocate() |
67
|
|
|
{ |
68
|
|
|
$remoteLocation = new stdClass(); |
69
|
|
|
$remoteLocation->x = 25.17; |
70
|
|
|
$remoteLocation->y = 54.41; |
71
|
|
|
$client = $this->getMockBuilder('SoapClient') |
72
|
|
|
->disableOriginalConstructor() |
73
|
|
|
->setMethods(['miejscowosc']) |
74
|
|
|
->getMock(); |
75
|
|
|
$client->method('miejscowosc')->willReturn($remoteLocation); |
76
|
|
|
$client->expects($this->once())->method('miejscowosc')->with('Wrocław', '4d36bcb5c40'); |
77
|
|
|
$endpoint = $this->getMockBuilder("Component\Remote\BurzeDzisNet\Endpoint") |
78
|
|
|
->disableOriginalConstructor() |
79
|
|
|
->setMethods(['client', 'apiKey']) |
80
|
|
|
->getMock(); |
81
|
|
|
$endpoint->method('client')->willReturn($client); |
82
|
|
|
$endpoint->method('apiKey')->willReturn('4d36bcb5c40'); |
83
|
|
|
$coordinates = (new BurzeDzisNet($endpoint))->locate('Wrocław'); |
84
|
|
|
$this->assertInstanceOf("Component\Remote\BurzeDzisNet\Point", $coordinates); |
85
|
|
|
$this->assertSame(25.17, $coordinates->x()); |
86
|
|
|
$this->assertSame(54.41, $coordinates->y()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @covers Component\Remote\BurzeDzisNet\BurzeDzisNet::getStorm |
91
|
|
|
*/ |
92
|
|
|
public function testGetStormReport() |
93
|
|
|
{ |
94
|
|
|
$client = $this->getMockBuilder('SoapClient') |
95
|
|
|
->disableOriginalConstructor() |
96
|
|
|
->setMethods(['szukaj_burzy']) |
97
|
|
|
->getMock(); |
98
|
|
|
$client->method('szukaj_burzy')->willReturn($this->getStormTO()); |
99
|
|
|
$client->expects($this->once())->method('szukaj_burzy')->with(54.41, 25.17, 50, '4d36bcb5c40'); |
100
|
|
|
$endpoint = $this->getMockBuilder("Component\Remote\BurzeDzisNet\Endpoint") |
101
|
|
|
->disableOriginalConstructor() |
102
|
|
|
->setMethods(['client', 'apiKey']) |
103
|
|
|
->getMock(); |
104
|
|
|
$endpoint->method('client')->willReturn($client); |
105
|
|
|
$endpoint->method('apiKey')->willReturn('4d36bcb5c40'); |
106
|
|
|
$storm = (new BurzeDzisNet($endpoint))->getStorm(new Point(25.17, 54.41), 50); |
107
|
|
|
$this->assertSame('NE', $storm->direction()); |
108
|
|
|
$this->assertSame(50, $storm->radius()); |
109
|
|
|
$this->assertSame(80.72, $storm->distance()); |
110
|
|
|
$this->assertSame(14, $storm->lightnings()); |
111
|
|
|
$this->assertSame(10, $storm->period()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @covers Component\Remote\BurzeDzisNet\BurzeDzisNet::getWeatherAlert |
116
|
|
|
*/ |
117
|
|
|
public function testGetWeatherAlert() |
118
|
|
|
{ |
119
|
|
|
$client = $this->getMockBuilder('SoapClient') |
120
|
|
|
->disableOriginalConstructor() |
121
|
|
|
->setMethods(['ostrzezenia_pogodowe']) |
122
|
|
|
->getMock(); |
123
|
|
|
$client->method('ostrzezenia_pogodowe')->willReturn($this->getAlertTO()); |
124
|
|
|
$client->expects($this->once())->method('ostrzezenia_pogodowe')->with(54.41, 25.17, '4d36bcb5c40'); |
125
|
|
|
$endpoint = $this->getMockBuilder("Component\Remote\BurzeDzisNet\Endpoint") |
126
|
|
|
->disableOriginalConstructor() |
127
|
|
|
->setMethods(['client', 'apiKey']) |
128
|
|
|
->getMock(); |
129
|
|
|
$endpoint->method('client')->willReturn($client); |
130
|
|
|
$endpoint->method('apiKey')->willReturn('4d36bcb5c40'); |
131
|
|
|
$alert = (new BurzeDzisNet($endpoint))->getWeatherAlert(new Point(25.17, 54.41)); |
132
|
|
|
$this->assertFrost($this->getAlertTO(), $alert->getAlert('frost')); |
133
|
|
|
$this->assertHeat($this->getAlertTO(), $alert->getAlert('heat')); |
134
|
|
|
$this->assertWind($this->getAlertTO(), $alert->getAlert('wind')); |
135
|
|
|
$this->assertStorm($this->getAlertTO(), $alert->getAlert('storm')); |
136
|
|
|
$this->assertTornado($this->getAlertTO(), $alert->getAlert('tornado')); |
137
|
|
|
$this->assertPrecipitation($this->getAlertTO(), $alert->getAlert('precipitation')); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get Storm data object |
142
|
|
|
* |
143
|
|
|
* @return stdClass |
144
|
|
|
*/ |
145
|
|
|
protected function getStormTO(): stdClass |
146
|
|
|
{ |
147
|
|
|
$storm = new stdClass(); |
148
|
|
|
$storm->liczba = 14; |
149
|
|
|
$storm->odleglosc = 80.72; |
150
|
|
|
$storm->kierunek = 'NE'; |
151
|
|
|
$storm->okres = 10; |
152
|
|
|
|
153
|
|
|
return $storm; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get Alert transfer object |
158
|
|
|
* |
159
|
|
|
* @return stdClass alert transfer object |
160
|
|
|
*/ |
161
|
|
|
protected function getAlertTO(): stdClass |
162
|
|
|
{ |
163
|
|
|
$alert = new stdClass(); |
164
|
|
|
$alert->mroz = 1; |
165
|
|
|
$alert->mroz_od_dnia = '2015-12-10'; |
166
|
|
|
$alert->mroz_do_dnia = '2016-02-12'; |
167
|
|
|
$alert->upal = 2; |
168
|
|
|
$alert->upal_od_dnia = '2015-04-14'; |
169
|
|
|
$alert->upal_do_dnia = '2015-05-20'; |
170
|
|
|
$alert->wiatr = 3; |
171
|
|
|
$alert->wiatr_od_dnia = '2015-06-13'; |
172
|
|
|
$alert->wiatr_do_dnia = '2015-08-16'; |
173
|
|
|
$alert->burza = 2; |
174
|
|
|
$alert->burza_od_dnia = '2015-11-24'; |
175
|
|
|
$alert->burza_do_dnia = '2015-12-28'; |
176
|
|
|
$alert->traba = 5; |
177
|
|
|
$alert->traba_od_dnia = '2015-01-02'; |
178
|
|
|
$alert->traba_do_dnia = '2015-08-09'; |
179
|
|
|
$alert->opad = 2; |
180
|
|
|
$alert->opad_od_dnia = '2015-03-29'; |
181
|
|
|
$alert->opad_do_dnia = '2015-07-30'; |
182
|
|
|
|
183
|
|
|
return $alert; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Assert frost alert |
188
|
|
|
* |
189
|
|
|
* @param stdClass $alertTO alert transfer object |
190
|
|
|
* @param Alert $frost frost alert |
191
|
|
|
*/ |
192
|
|
|
protected function assertFrost(stdClass $alertTO, Alert $frost) |
193
|
|
|
{ |
194
|
|
|
$this->assertSame($alertTO->mroz, $frost->level()); |
195
|
|
|
$this->assertSame($alertTO->mroz_od_dnia, $frost->startDate()); |
196
|
|
|
$this->assertSame($alertTO->mroz_do_dnia, $frost->endDate()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Assert heat alert |
201
|
|
|
* |
202
|
|
|
* @param stdClass $alertTO alert transfer object |
203
|
|
|
* @param Alert $heat heat alert |
204
|
|
|
*/ |
205
|
|
|
protected function assertHeat(stdClass $alertTO, Alert $heat) |
206
|
|
|
{ |
207
|
|
|
$this->assertSame($alertTO->upal, $heat->level()); |
208
|
|
|
$this->assertSame($alertTO->upal_od_dnia, $heat->startDate()); |
209
|
|
|
$this->assertSame($alertTO->upal_do_dnia, $heat->endDate()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Assert wind alert |
214
|
|
|
* |
215
|
|
|
* @param stdClass $alertTO alert transfer object |
216
|
|
|
* @param Alert $wind wind alert |
217
|
|
|
*/ |
218
|
|
|
protected function assertWind(stdClass $alertTO, Alert $wind) |
219
|
|
|
{ |
220
|
|
|
$this->assertSame($alertTO->wiatr, $wind->level()); |
221
|
|
|
$this->assertSame($alertTO->wiatr_od_dnia, $wind->startDate()); |
222
|
|
|
$this->assertSame($alertTO->wiatr_do_dnia, $wind->endDate()); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Assert storm alert |
227
|
|
|
* |
228
|
|
|
* @param stdClass $alertTO alert transfer object |
229
|
|
|
* @param Alert $storm storm alert |
230
|
|
|
*/ |
231
|
|
|
protected function assertStorm(stdClass $alertTO, Alert $storm) |
232
|
|
|
{ |
233
|
|
|
$this->assertSame($alertTO->burza, $storm->level()); |
234
|
|
|
$this->assertSame($alertTO->burza_od_dnia, $storm->startDate()); |
235
|
|
|
$this->assertSame($alertTO->burza_do_dnia, $storm->endDate()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Assert tornado alert |
240
|
|
|
* |
241
|
|
|
* @param stdClass $alertTO alert transfer object |
242
|
|
|
* @param Alert $tornado tornado alert |
243
|
|
|
*/ |
244
|
|
|
protected function assertTornado(stdClass $alertTO, Alert $tornado) |
245
|
|
|
{ |
246
|
|
|
$this->assertSame($alertTO->traba, $tornado->level()); |
247
|
|
|
$this->assertSame($alertTO->traba_od_dnia, $tornado->startDate()); |
248
|
|
|
$this->assertSame($alertTO->traba_do_dnia, $tornado->endDate()); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Assert precipitation alert |
253
|
|
|
* |
254
|
|
|
* @param stdClass $alertTO alert transfer object |
255
|
|
|
* @param Alert $precipitation precipitation alert |
256
|
|
|
*/ |
257
|
|
|
protected function assertPrecipitation(stdClass $alertTO, Alert $precipitation) |
258
|
|
|
{ |
259
|
|
|
$this->assertSame($alertTO->opad, $precipitation->level()); |
260
|
|
|
$this->assertSame($alertTO->opad_od_dnia, $precipitation->startDate()); |
261
|
|
|
$this->assertSame($alertTO->opad_do_dnia, $precipitation->endDate()); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Get mocked endpoint with soap client |
266
|
|
|
* |
267
|
|
|
* @param SoapClient soap client |
268
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
269
|
|
|
*/ |
270
|
|
View Code Duplication |
protected function getEndpoint(SoapClient $client): Endpoint |
|
|
|
|
271
|
|
|
{ |
272
|
|
|
$endpoint = $this->getMockBuilder("Component\Remote\BurzeDzisNet\Endpoint") |
273
|
|
|
->disableOriginalConstructor() |
274
|
|
|
->setMethods(['client', 'apiKey']) |
275
|
|
|
->getMock(); |
276
|
|
|
$endpoint->method('client')->willReturn($client); |
277
|
|
|
$endpoint->method('apiKey')->willReturn('4d36bcb5c40'); |
278
|
|
|
return $endpoint; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
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.