Completed
Pull Request — master (#65)
by Alex
02:11
created

CurrentWeatherTest::testByCityID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
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 $owm;
29
30 View Code Duplication
    protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cmfcmf\OpenWeatherMap() of type object<Cmfcmf\OpenWeatherMap> is incompatible with the declared type object<OpenWeatherMap> of property $owm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        $this->owm->setApiKey($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
            $weather = $this->owm->getWeather('InvalidCity');
0 ignored issues
show
Unused Code introduced by
$weather is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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