Skip to content

Commit 9a14f7d

Browse files
Alex Seriycmfcmf
authored andcommitted
Squashed commit of the following:
- Fixed day average temperature calculation issue. - Added the daily forecast integration test.
1 parent fef2e04 commit 9a14f7d

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

Cmfcmf/OpenWeatherMap/Forecast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(\SimpleXMLElement $xml, $units)
5454
$temperatureUnit = 'F';
5555
}
5656

57-
$xml->temperature['value'] = ($xml->temperature['max'] + $xml->temperature['min']) / 2;
57+
$xml->temperature['value'] = round((floatval($xml->temperature['max']) + floatval($xml->temperature['min'])) / 2, 2);
5858

5959
$this->temperature = new Temperature(new Unit($xml->temperature['value'], $temperatureUnit), new Unit($xml->temperature['min'], $temperatureUnit), new Unit($xml->temperature['max'], $temperatureUnit), new Unit($xml->temperature['day'], $temperatureUnit), new Unit($xml->temperature['morn'], $temperatureUnit), new Unit($xml->temperature['eve'], $temperatureUnit), new Unit($xml->temperature['night'], $temperatureUnit));
6060
$this->humidity = new Unit($xml->humidity['value'], $xml->humidity['unit']);

integTests/ForecastDailyTest.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of
11+
* OpenWeatherMap before using this class:
12+
*
13+
* @see http://www.OpenWeatherMap.org
14+
* @see http://www.OpenWeatherMap.org/terms
15+
* @see http://openweathermap.org/appid
16+
*/
17+
18+
namespace Cmfcmf\OpenWeatherMap\IntegTests;
19+
20+
use Cmfcmf\OpenWeatherMap;
21+
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
22+
23+
class ForecastDailyTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @var \OpenWeatherMap
27+
*/
28+
protected $owm;
29+
30+
protected function setUp()
31+
{
32+
$ini = parse_ini_file(__DIR__ . '/ApiKey.ini');
33+
$apiKey = $ini['api_key'];
34+
35+
$this->owm = new OpenWeatherMap();
36+
$this->owm->setApiKey($apiKey);
37+
}
38+
39+
public function testTemperatureMetric()
40+
{
41+
$forecast = $this->owm->getWeatherForecast('Berlin', 'metric', 'de', '', 10);
42+
43+
$now = new \DateTime();
44+
$this->assertEquals($now->format('d.m.Y H:i'), $forecast->lastUpdate->format('d.m.Y H:i'));
45+
46+
$this->assertEquals('Berlin', $forecast->city->name);
47+
$this->assertEquals('05:00:53', $forecast->sun->rise->format("H:i:s"));
48+
$this->assertEquals('17:25:40', $forecast->sun->set->format("H:i:s"));
49+
50+
$this->assertEquals(10, iterator_count($forecast));
51+
52+
$forecast_arr = iterator_to_array($forecast);
53+
54+
$this->assertEquals('4.59 &deg;C', $forecast_arr[0]->temperature);
55+
$this->assertEquals('7.34 &deg;C', $forecast_arr[1]->temperature);
56+
$this->assertEquals('5.58 &deg;C', $forecast_arr[2]->temperature->now);
57+
$this->assertEquals('6.14 &deg;C', $forecast_arr[3]->temperature);
58+
$this->assertEquals('7.56 &deg;C', $forecast_arr[4]->temperature);
59+
$this->assertEquals('10.24 &deg;C', $forecast_arr[5]->temperature);
60+
$this->assertEquals('9.34 &deg;C', $forecast_arr[6]->temperature);
61+
$this->assertEquals('10.93 &deg;C', $forecast_arr[7]->temperature->now);
62+
$this->assertEquals('8.8 &deg;C', $forecast_arr[8]->temperature);
63+
$this->assertEquals('8.02 &deg;C', $forecast_arr[9]->temperature);
64+
65+
$this->assertEquals('2.71 &deg;C', $forecast_arr[0]->temperature->min);
66+
$this->assertEquals('9.07 &deg;C', $forecast_arr[1]->temperature->max);
67+
$this->assertEquals('8.31 &deg;C', $forecast_arr[2]->temperature->day);
68+
$this->assertEquals('2.93 &deg;C', $forecast_arr[3]->temperature->morning);
69+
$this->assertEquals('8.99 &deg;C', $forecast_arr[4]->temperature->evening);
70+
$this->assertEquals('8.91 &deg;C', $forecast_arr[5]->temperature->night);
71+
72+
$this->assertEquals('&deg;C', $forecast_arr[6]->temperature->getUnit());
73+
$this->assertEquals('10.93', $forecast_arr[7]->temperature->getValue());
74+
$this->assertEmpty($forecast_arr[8]->temperature->getDescription());
75+
$this->assertEquals('8.02 &deg;C', $forecast_arr[9]->temperature->getFormatted());
76+
}
77+
78+
public function testTemperatureImperial()
79+
{
80+
$forecast = $this->owm->getWeatherForecast('New York', 'imperial', 'en', '', 10);
81+
82+
$now = new \DateTime();
83+
$this->assertEquals($now->format('m-d-Y H:i'), $forecast->lastUpdate->format('m-d-Y H:i'));
84+
85+
$this->assertEquals('New York', $forecast->city->name);
86+
$this->assertEquals('10:53:25', $forecast->sun->rise->format("H:i:s"));
87+
$this->assertEquals('23:11:57', $forecast->sun->set->format("H:i:s"));
88+
89+
$this->assertEquals(10, iterator_count($forecast));
90+
91+
$forecast_arr = iterator_to_array($forecast);
92+
93+
$this->assertEquals('48.88 F', $forecast_arr[0]->temperature);
94+
$this->assertEquals('55.56 F', $forecast_arr[1]->temperature);
95+
$this->assertEquals('57.08 F', $forecast_arr[2]->temperature->now);
96+
$this->assertEquals('50.85 F', $forecast_arr[3]->temperature);
97+
$this->assertEquals('47.79 F', $forecast_arr[4]->temperature);
98+
$this->assertEquals('46.21 F', $forecast_arr[5]->temperature);
99+
$this->assertEquals('47.73 F', $forecast_arr[6]->temperature);
100+
$this->assertEquals('43.52 F', $forecast_arr[7]->temperature->now);
101+
$this->assertEquals('34.79 F', $forecast_arr[8]->temperature);
102+
$this->assertEquals('30.35 F', $forecast_arr[9]->temperature);
103+
104+
$this->assertEquals('47.75 F', $forecast_arr[0]->temperature->min);
105+
$this->assertEquals('64.53 F', $forecast_arr[1]->temperature->max);
106+
$this->assertEquals('69.4 F', $forecast_arr[2]->temperature->day);
107+
$this->assertEquals('58.71 F', $forecast_arr[3]->temperature->morning);
108+
$this->assertEquals('49.5 F', $forecast_arr[4]->temperature->evening);
109+
$this->assertEquals('43.63 F', $forecast_arr[5]->temperature->night);
110+
111+
$this->assertEquals('F', $forecast_arr[6]->temperature->getUnit());
112+
$this->assertEquals('43.52', $forecast_arr[7]->temperature->getValue());
113+
$this->assertEmpty($forecast_arr[8]->temperature->getDescription());
114+
$this->assertEquals('30.35 F', $forecast_arr[9]->temperature->getFormatted());
115+
}
116+
117+
public function testWindMetric()
118+
{
119+
$forecast = $this->owm->getWeatherForecast('Moscow', 'metric', 'ru', '', 9);
120+
121+
$this->assertEquals('Moscow', $forecast->city->name);
122+
$this->assertEquals('RU', $forecast->city->country);
123+
/*
124+
This assertion fails right now because the Daily Forecase XML provides
125+
the city ID as 'geobaseid' attribute, not as 'id' in Current Weather.
126+
$this->assertEquals(524901, $forecast->city->id);
127+
*/
128+
$this->assertEquals('37.615555', $forecast->city->lon);
129+
$this->assertEquals('55.75222', $forecast->city->lat);
130+
131+
$this->assertEquals('03:22:56', $forecast->sun->rise->format("H:i:s"));
132+
$this->assertEquals('15:50:08', $forecast->sun->set->format("H:i:s"));
133+
134+
$this->assertEquals(9, iterator_count($forecast));
135+
136+
$forecast_arr = iterator_to_array($forecast);
137+
138+
$this->assertEquals('5.41 m/s', $forecast_arr[0]->wind->speed);
139+
$this->assertEquals('61 ENE', $forecast_arr[1]->wind->direction);
140+
}
141+
}

0 commit comments

Comments
 (0)