1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright Zikula Foundation 2014 - Zikula Application Framework |
4
|
|
|
* |
5
|
|
|
* This work is contributed to the Zikula Foundation under one or more |
6
|
|
|
* Contributor Agreements and licensed to You under the following license: |
7
|
|
|
* |
8
|
|
|
* @license GNU/LGPv3 (or at your option any later version). |
9
|
|
|
* @package OpenWeatherMap-PHP-Api |
10
|
|
|
* |
11
|
|
|
* Please see the NOTICE file distributed with this source code for further |
12
|
|
|
* information regarding copyright and licensing. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Cmfcmf\OpenWeatherMap\Tests\Util; |
16
|
|
|
|
17
|
|
|
use \Cmfcmf\OpenWeatherMap\Util\Unit; |
18
|
|
|
|
19
|
|
|
class UnitTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Unit |
23
|
|
|
*/ |
24
|
|
|
private $unit; |
25
|
|
|
|
26
|
|
|
const POSITIVE_INT_VALUE = 23; |
27
|
|
|
|
28
|
|
|
const POSITIVE_FLOAT_VALUE = 48.23534; |
29
|
|
|
|
30
|
|
|
const NEGATIVE_INT_VALUE = -30; |
31
|
|
|
|
32
|
|
|
const NEGATIVE_FLOAT_VALUE = -93.45839; |
33
|
|
|
|
34
|
|
|
const ZERO_INT_VALUE = 0; |
35
|
|
|
|
36
|
|
|
const ZERO_FLOAT_VALUE = 0.0; |
37
|
|
|
|
38
|
|
|
public function testGetValueWithPositiveIntValue() |
39
|
|
|
{ |
40
|
|
|
$this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE); |
41
|
|
|
|
42
|
|
|
$this->assertSame((float)self::POSITIVE_INT_VALUE, $this->unit->getValue()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testGetValueWithPositiveFloatValue() |
46
|
|
|
{ |
47
|
|
|
$this->givenThereIsAUnitWithValue(self::POSITIVE_FLOAT_VALUE); |
48
|
|
|
|
49
|
|
|
$this->assertSame(self::POSITIVE_FLOAT_VALUE, $this->unit->getValue()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testGetValueWithNegativeIntValue() |
53
|
|
|
{ |
54
|
|
|
$this->givenThereIsAUnitWithValue(self::NEGATIVE_INT_VALUE); |
55
|
|
|
|
56
|
|
|
$this->assertSame((float)self::NEGATIVE_INT_VALUE, $this->unit->getValue()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetValueWithNegativeFloatValue() |
60
|
|
|
{ |
61
|
|
|
$this->givenThereIsAUnitWithValue(self::NEGATIVE_FLOAT_VALUE); |
62
|
|
|
|
63
|
|
|
$this->assertSame(self::NEGATIVE_FLOAT_VALUE, $this->unit->getValue()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testGetValueWithZeroIntValue() |
67
|
|
|
{ |
68
|
|
|
$this->givenThereIsAUnitWithValue(self::ZERO_INT_VALUE); |
69
|
|
|
|
70
|
|
|
$this->assertSame((float)self::ZERO_INT_VALUE, $this->unit->getValue()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testGetValueWithZeroFloatValue() |
74
|
|
|
{ |
75
|
|
|
$this->givenThereIsAUnitWithValue(self::ZERO_FLOAT_VALUE); |
76
|
|
|
|
77
|
|
|
$this->assertSame(self::ZERO_FLOAT_VALUE, $this->unit->getValue()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function givenThereIsAUnitWithValue($value, $unit = null) |
81
|
|
|
{ |
82
|
|
|
$this->unit = $unit === null ? new Unit($value) : new Unit($value, $unit); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testGetUnitWithEmptyUnit() |
86
|
|
|
{ |
87
|
|
|
$this->givenThereIsAUnitWithUnit(""); |
88
|
|
|
|
89
|
|
|
$this->assertSame("", $this->unit->getUnit()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testGetUnitWithStringAsUnit() |
93
|
|
|
{ |
94
|
|
|
$this->givenThereIsAUnitWithUnit("Hey! I'm cmfcmf"); |
95
|
|
|
|
96
|
|
|
$this->assertSame("Hey! I'm cmfcmf", $this->unit->getUnit()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testCelsiusFix() |
100
|
|
|
{ |
101
|
|
|
$this->givenThereIsAUnitWithUnit("celsius"); |
102
|
|
|
|
103
|
|
|
$this->assertSame("°C", $this->unit->getUnit()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testMetricFix() |
107
|
|
|
{ |
108
|
|
|
$this->givenThereIsAUnitWithUnit("metric"); |
109
|
|
|
|
110
|
|
|
$this->assertSame("°C", $this->unit->getUnit()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testFahrenheitFix() |
114
|
|
|
{ |
115
|
|
|
$this->givenThereIsAUnitWithUnit("fahrenheit"); |
116
|
|
|
|
117
|
|
|
$this->assertSame("F", $this->unit->getUnit()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function givenThereIsAUnitWithUnit($unit) |
121
|
|
|
{ |
122
|
|
|
$this->unit = new Unit(0, $unit); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testGetDescriptionWithEmptyDescription() |
126
|
|
|
{ |
127
|
|
|
$this->givenThereIsAUnitWithDescription(""); |
128
|
|
|
|
129
|
|
|
$this->assertSame("", $this->unit->getDescription()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testGetDescriptionWithStringAsDescription() |
133
|
|
|
{ |
134
|
|
|
$this->givenThereIsAUnitWithDescription("Hey! I'm cmfcmf"); |
135
|
|
|
|
136
|
|
|
$this->assertSame("Hey! I'm cmfcmf", $this->unit->getDescription()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function givenThereIsAUnitWithDescription($description) |
140
|
|
|
{ |
141
|
|
|
$this->unit = new Unit(0, "", $description); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testGetFormattedWithoutUnit() |
145
|
|
|
{ |
146
|
|
|
$this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE); |
147
|
|
|
|
148
|
|
|
$this->assertEquals(self::POSITIVE_INT_VALUE, $this->unit->getFormatted()); |
149
|
|
|
$this->assertEquals($this->unit->getValue(), $this->unit->getFormatted()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testGetFormattedWithUnit() |
153
|
|
|
{ |
154
|
|
|
$this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE, 'K'); |
155
|
|
|
|
156
|
|
|
$this->assertEquals(self::POSITIVE_INT_VALUE . ' K', $this->unit->getFormatted()); |
157
|
|
|
$this->assertEquals($this->unit->getValue() . ' ' . $this->unit->getUnit(), $this->unit->getFormatted()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testToString() |
161
|
|
|
{ |
162
|
|
|
$this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE, 'K'); |
163
|
|
|
|
164
|
|
|
$this->assertEquals($this->unit->getFormatted(), $this->unit); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|