Completed
Pull Request — master (#78)
by Alex
02:15
created

CurrentWeatherTest::testCurrentBulkCheck()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 21
rs 9.3142
cc 2
eloc 16
nc 2
nop 0
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 $apiKey;
29
    protected $owm;
30
31
    protected function setUp()
32
    {
33
        // Load the app configuration
34
        $ini = parse_ini_file(__DIR__ . '/ApiKey.ini');
35
        $this->apiKey = $ini['api_key'];
36
37
        $this->owm = new OpenWeatherMap();
38
        $this->owm->setApiKey($this->apiKey);
39
    }
40
41
    public function testByCity()
42
    {
43
        // Default units (imperial) and language (English)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
        $weather = $this->owm->getWeather('Paris');
45
        $this->assertEquals('49.39 F', $weather->temperature);
46
47
        // Default language (English)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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('&deg;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 &deg;C', $weather->temperature->max->getFormatted());
61
    }
62
63
    public function testCityNotFound()
64
    {
65
        // City doesn't exist
66
        try {
67
            $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,CA');
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('RU', $weather->city->country);
96
    }
97
98
    public function testCurrentBulkCheck()
99
    {
100
        // Download the test data set
101
        $testDataUrl =
102
            "https://raw.githubusercontent.com/aseriy/OpenWeatherMap-VirtSrvc/master/testdata/" .
103
            "$this->apiKey/current/00.json";
104
        $testDataJson = file_get_contents($testDataUrl);
105
        $testData = json_decode($testDataJson, true);
106
107
        foreach ($testData as $c) {
108
            $weather = $this->owm->getWeather($c["city_id"], $c["units"], 'en');
109
            $this->assertEquals($c["city_name"], $weather->city->name, "city name");
110
            $this->assertEquals($c["country"], $weather->city->country, "country in " . $c["city_name"]);
111
            $this->assertEquals($c["temp"], $weather->temperature->getValue(), "temperature in " . $c["city_name"]);
112
            $this->assertEquals($c["temp_min"], $weather->temperature->min->getValue(), "minimum temperature in " . $c["city_name"]);
113
            $this->assertEquals($c["temp_max"], $weather->temperature->max->getValue(), "maximum temperature in " . $c["city_name"]);
114
            $this->assertEquals($c["wind_speed"], $weather->wind->speed->getValue(), "wind speed in " . $c["city_name"]);
115
            $this->assertEquals($c["pressure"], $weather->pressure->getValue(), "pressure in " . $c["city_name"]);
116
            $this->assertEquals($c["humidity"], $weather->humidity->getValue(), "humidity in " . $c["city_name"]);
117
        }
118
    }
119
}
120