Completed
Pull Request — master (#145)
by Christian
01:18
created

UnitTest::testGetValueWithNegativeFloatValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Util;
20
21
use Cmfcmf\OpenWeatherMap\Util\Unit;
22
23
class UnitTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @var Unit
27
     */
28
    private $unit;
29
30
    const POSITIVE_INT_VALUE = 23;
31
32
    const POSITIVE_FLOAT_VALUE = 48.23534;
33
34
    const NEGATIVE_INT_VALUE = -30;
35
36
    const NEGATIVE_FLOAT_VALUE = -93.45839;
37
38
    const ZERO_INT_VALUE = 0;
39
40
    const ZERO_FLOAT_VALUE = 0.0;
41
42
    public function testGetValueWithPositiveIntValue()
43
    {
44
        $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE);
45
46
        $this->assertSame((float)self::POSITIVE_INT_VALUE, $this->unit->getValue());
47
    }
48
49
    public function testGetValueWithPositiveFloatValue()
50
    {
51
        $this->givenThereIsAUnitWithValue(self::POSITIVE_FLOAT_VALUE);
52
53
        $this->assertSame(self::POSITIVE_FLOAT_VALUE, $this->unit->getValue());
54
    }
55
56
    public function testGetValueWithNegativeIntValue()
57
    {
58
        $this->givenThereIsAUnitWithValue(self::NEGATIVE_INT_VALUE);
59
60
        $this->assertSame((float)self::NEGATIVE_INT_VALUE, $this->unit->getValue());
61
    }
62
63
    public function testGetValueWithNegativeFloatValue()
64
    {
65
        $this->givenThereIsAUnitWithValue(self::NEGATIVE_FLOAT_VALUE);
66
67
        $this->assertSame(self::NEGATIVE_FLOAT_VALUE, $this->unit->getValue());
68
    }
69
70
    public function testGetValueWithZeroIntValue()
71
    {
72
        $this->givenThereIsAUnitWithValue(self::ZERO_INT_VALUE);
73
74
        $this->assertSame((float)self::ZERO_INT_VALUE, $this->unit->getValue());
75
    }
76
77
    public function testGetValueWithZeroFloatValue()
78
    {
79
        $this->givenThereIsAUnitWithValue(self::ZERO_FLOAT_VALUE);
80
81
        $this->assertSame(self::ZERO_FLOAT_VALUE, $this->unit->getValue());
82
    }
83
84
    private function givenThereIsAUnitWithValue($value, $unit = null)
85
    {
86
        $this->unit = $unit === null ? new Unit($value) : new Unit($value, $unit);
87
    }
88
89
    public function testGetUnitWithEmptyUnit()
90
    {
91
        $this->givenThereIsAUnitWithUnit("");
92
93
        $this->assertSame("", $this->unit->getUnit());
94
    }
95
96
    public function testGetUnitWithStringAsUnit()
97
    {
98
        $this->givenThereIsAUnitWithUnit("Hey! I'm cmfcmf");
99
100
        $this->assertSame("Hey! I'm cmfcmf", $this->unit->getUnit());
101
    }
102
103
    public function testCelsiusFix()
104
    {
105
        $this->givenThereIsAUnitWithUnit("celsius");
106
107
        $this->assertSame("°C", $this->unit->getUnit());
108
    }
109
110
    public function testMetricFix()
111
    {
112
        $this->givenThereIsAUnitWithUnit("metric");
113
114
        $this->assertSame("°C", $this->unit->getUnit());
115
    }
116
117
    public function testFahrenheitFix()
118
    {
119
        $this->givenThereIsAUnitWithUnit("fahrenheit");
120
121
        $this->assertSame("°F", $this->unit->getUnit());
122
    }
123
124
    private function givenThereIsAUnitWithUnit($unit)
125
    {
126
        $this->unit = new Unit(0, $unit);
127
    }
128
129
    public function testGetDescriptionWithEmptyDescription()
130
    {
131
        $this->givenThereIsAUnitWithDescription("");
132
133
        $this->assertSame("", $this->unit->getDescription());
134
    }
135
136
    public function testGetDescriptionWithStringAsDescription()
137
    {
138
        $this->givenThereIsAUnitWithDescription("Hey! I'm cmfcmf");
139
140
        $this->assertSame("Hey! I'm cmfcmf", $this->unit->getDescription());
141
    }
142
143
    private function givenThereIsAUnitWithDescription($description)
144
    {
145
        $this->unit = new Unit(0, "", $description);
146
    }
147
148
    public function testGetFormattedWithoutUnit()
149
    {
150
        $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE);
151
152
        $this->assertEquals(self::POSITIVE_INT_VALUE, $this->unit->getFormatted());
153
        $this->assertEquals($this->unit->getValue(), $this->unit->getFormatted());
154
    }
155
156
    public function testGetFormattedWithUnit()
157
    {
158
        $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE, 'K');
159
160
        $this->assertEquals(self::POSITIVE_INT_VALUE . ' K', $this->unit->getFormatted());
161
        $this->assertEquals($this->unit->getValue() . ' ' . $this->unit->getUnit(), $this->unit->getFormatted());
162
    }
163
164
    public function testToString()
165
    {
166
        $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE, 'K');
167
168
        $this->assertEquals($this->unit->getFormatted(), $this->unit);
169
    }
170
171
    public function testToJSON()
172
    {
173
        $unit = new Unit(42.5, "°C", "hot", "2.5");
174
        $this->assertEquals(json_encode($unit), '{"value":42.5,"unit":"\u00b0C","description":"hot","precision":2.5}');
175
    }
176
}
177