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 CurrentWeatherTest extends \PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \OpenWeatherMap |
27
|
|
|
*/ |
28
|
|
|
protected $owm; |
29
|
|
|
|
30
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
// Load the app configuration |
34
|
|
|
$ini = parse_ini_file(__DIR__ . '/ApiKey.ini'); |
35
|
|
|
$apiKey = $ini['api_key']; |
36
|
|
|
|
37
|
|
|
$this->owm = new OpenWeatherMap(); |
|
|
|
|
38
|
|
|
$this->owm->setApiKey($apiKey); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testByCity() |
42
|
|
|
{ |
43
|
|
|
// Default units (imperial) and language (English) |
|
|
|
|
44
|
|
|
$weather = $this->owm->getWeather('Paris'); |
45
|
|
|
$this->assertEquals('49.39 F', $weather->temperature); |
46
|
|
|
|
47
|
|
|
// Default language (English) |
|
|
|
|
48
|
|
|
$weather = $this->owm->getWeather('Atlanta', 'imperial'); |
49
|
|
|
$this->assertEquals(38.18, $weather->temperature->getValue()); |
50
|
|
|
$weather = $this->owm->getWeather('London', 'metric'); |
51
|
|
|
$this->assertEquals(4.66, $weather->temperature->getValue()); |
52
|
|
|
$this->assertEquals('°C', $weather->temperature->getUnit()); |
53
|
|
|
|
54
|
|
|
// No defaults |
55
|
|
|
$weather = $this->owm->getWeather('Chicago', 'imperial', 'en'); |
56
|
|
|
$this->assertEquals('13.88 F', $weather->temperature->getFormatted()); |
57
|
|
|
$weather = $this->owm->getWeather('Prague', 'metric', 'en'); |
58
|
|
|
$this->assertEquals(3.58, $weather->temperature->now->getValue()); |
59
|
|
|
$this->assertEmpty($weather->temperature->min->getDescription()); |
60
|
|
|
$this->assertEquals('3.58 °C', $weather->temperature->max->getFormatted()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testCityNotFound() |
64
|
|
|
{ |
65
|
|
|
// City doesn't exist |
66
|
|
|
try { |
67
|
|
|
$weather = $this->owm->getWeather('InvalidCity'); |
|
|
|
|
68
|
|
|
} catch (OWMException $e) { |
69
|
|
|
$this->assertEquals(404, $e->getCode()); |
70
|
|
|
$this->assertEquals('Error: Not found city', $e->getMessage()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testByCityCountry() |
75
|
|
|
{ |
76
|
|
|
$weather = $this->owm->getWeather('London,ON'); |
77
|
|
|
|
78
|
|
|
// Geo coordinates |
79
|
|
|
$this->assertEquals('-81.23', $weather->city->lon); |
80
|
|
|
$this->assertEquals('42.98', $weather->city->lat); |
81
|
|
|
|
82
|
|
|
// Country |
83
|
|
|
$this->assertEquals('CA', $weather->city->country); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testByCityID() |
87
|
|
|
{ |
88
|
|
|
$weather = $this->owm->getWeather(4930956); |
89
|
|
|
$this->assertEquals('Boston', $weather->city->name); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testByCoordinates() |
93
|
|
|
{ |
94
|
|
|
$weather = $this->owm->getWeather(array('lon' => 37.62, 'lat' => 55.75)); |
95
|
|
|
$this->assertEquals($weather->city->country, 'RU'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.