Skip to content

Commit d55e066

Browse files
committed
Handle wind direction of null in weather groups, closes #137
1 parent 20039fe commit d55e066

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

Cmfcmf/OpenWeatherMap/CurrentWeather.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function __construct($data, $units)
124124
$this->pressure = new Unit($data->main->pressure, 'hPa');
125125
$this->wind = new Wind(
126126
new Unit($data->wind->speed, $windSpeedUnit),
127-
property_exists($data->wind, 'deg') ? new Unit($data->wind->deg) : null
127+
property_exists($data->wind, 'deg') && $data->wind->deg !== null ? new Unit($data->wind->deg) : null
128128
);
129129
$this->clouds = new Unit($data->clouds->all, '%');
130130

Cmfcmf/OpenWeatherMap/Util/Wind.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Wind
2929
public $speed;
3030

3131
/**
32-
* @var Unit The wind direction.
32+
* @var Unit|null The wind direction.
3333
*/
3434
public $direction;
3535

tests/FakeData.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class FakeData
4141
"wind":{"speed":5.71,"deg":229.501},
4242
"dt_txt":"2014-07-23 09:00:00"
4343
},{
44-
"id":1851632,
44+
"id":1851633,
4545
"dt":1406106000,
4646
"coord":{"lon":138.933334,"lat":34.966671},
4747
"sys":{"type":3,"id":168940,"message":0.0297,"country":"US","sunrise":1427723751,"sunset":1427768967},
@@ -57,7 +57,7 @@ class FakeData
5757
},
5858
"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
5959
"clouds":{"all":88},
60-
"wind":{"speed":5.71,"deg":229.501},
60+
"wind":{"speed":5.71,"deg":null},
6161
"dt_txt":"2014-07-23 09:00:00"
6262
}]
6363
}';
@@ -127,4 +127,26 @@ public static function forecastXML()
127127
<lastupdate value="2017-01-02T12:20:00"></lastupdate>
128128
</current>
129129
XML;
130+
131+
const CURRENT_WEATHER_XML_NO_WIND_DIRECTION = <<<XML
132+
<current>
133+
<city id="2950159" name="Berlin">
134+
<coord lon="13.41" lat="52.52"></coord>
135+
<country>DE</country>
136+
<sun rise="2017-01-02T07:16:51" set="2017-01-02T15:04:50"></sun>
137+
</city>
138+
<temperature value="36.48" min="35.6" max="37.4" unit="fahrenheit"></temperature>
139+
<humidity value="86" unit="%"></humidity>
140+
<pressure value="1014" unit="hPa"></pressure>
141+
<wind>
142+
<speed value="9.17" name="Fresh Breeze"></speed>
143+
<gusts></gusts>
144+
</wind>
145+
<clouds value="75" name="broken clouds"></clouds>
146+
<visibility value="8000"></visibility>
147+
<precipitation mode="no"></precipitation>
148+
<weather number="500" value="light rain" icon="10d"></weather>
149+
<lastupdate value="2017-01-02T12:20:00"></lastupdate>
150+
</current>
151+
XML;
130152
}

tests/OpenWeatherMap/CurrentWeatherGroupTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public function setUp()
3232
$this->currentWeatherGroup = new CurrentWeatherGroup($this->fakeJson, 'metric');
3333
}
3434

35+
public function testWindDirection()
36+
{
37+
$this->assertSame($this->currentWeatherGroup->current()->wind->direction->getValue(), 229.501);
38+
$this->currentWeatherGroup->next();
39+
$this->assertNull($this->currentWeatherGroup->current()->wind->direction);
40+
}
41+
3542
public function testRewind()
3643
{
3744
$expectIndex = 1851632;
@@ -50,7 +57,7 @@ public function testCurrent()
5057
}
5158
public function testNext()
5259
{
53-
$expectIndex = 1851632;
60+
$expectIndex = 1851633;
5461
$this->currentWeatherGroup->next();
5562
$position = $this->currentWeatherGroup->key();
5663

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\CurrentWeather;
22+
use Cmfcmf\OpenWeatherMap\Tests\FakeData;
23+
24+
class CurrentWeatherTest extends \PHPUnit_Framework_TestCase
25+
{
26+
public function testWindDirection()
27+
{
28+
$fakeXml = new \SimpleXMLElement(FakeData::CURRENT_WEATHER_XML);
29+
$weather = new CurrentWeather($fakeXml, "metric");
30+
$this->assertSame($weather->wind->direction->getValue(), 300.0);
31+
32+
$fakeXml = new \SimpleXMLElement(FakeData::CURRENT_WEATHER_XML_NO_WIND_DIRECTION);
33+
$weather = new CurrentWeather($fakeXml, "metric");
34+
$this->assertNull($weather->wind->direction);
35+
}
36+
}

0 commit comments

Comments
 (0)