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 \Util ;
16+
17+ class UnitTest extends \PHPUnit_Framework_TestCase
18+ {
19+ /**
20+ * @var Unit
21+ */
22+ private $ unit ;
23+
24+ const POSITIVE_INT_VALUE = 23 ;
25+ const POSITIVE_FLOAT_VALUE = 48.23534 ;
26+ const NEGATIVE_INT_VALUE = -30 ;
27+ const NEGATIVE_FLOAT_VALUE = -93.45839 ;
28+ const ZERO_INT_VALUE = 0 ;
29+ const ZERO_FLOAT_VALUE = 0.0 ;
30+
31+ public function testGetValueWithPositiveIntValue ()
32+ {
33+ $ this ->givenThereIsAUnitWithValue (self ::POSITIVE_INT_VALUE );
34+
35+ $ this ->assertSame ((float )self ::POSITIVE_INT_VALUE , $ this ->unit ->getValue ());
36+ }
37+
38+ public function testGetValueWithPositiveFloatValue ()
39+ {
40+ $ this ->givenThereIsAUnitWithValue (self ::POSITIVE_FLOAT_VALUE );
41+
42+ $ this ->assertSame (self ::POSITIVE_FLOAT_VALUE , $ this ->unit ->getValue ());
43+ }
44+
45+ public function testGetValueWithNegativeIntValue ()
46+ {
47+ $ this ->givenThereIsAUnitWithValue (self ::NEGATIVE_INT_VALUE );
48+
49+ $ this ->assertSame ((float )self ::NEGATIVE_INT_VALUE , $ this ->unit ->getValue ());
50+ }
51+
52+ public function testGetValueWithNegativeFloatValue ()
53+ {
54+ $ this ->givenThereIsAUnitWithValue (self ::NEGATIVE_FLOAT_VALUE );
55+
56+ $ this ->assertSame (self ::NEGATIVE_FLOAT_VALUE , $ this ->unit ->getValue ());
57+ }
58+
59+ public function testGetValueWithZeroIntValue ()
60+ {
61+ $ this ->givenThereIsAUnitWithValue (self ::ZERO_INT_VALUE );
62+
63+ $ this ->assertSame ((float )self ::ZERO_INT_VALUE , $ this ->unit ->getValue ());
64+ }
65+
66+ public function testGetValueWithZeroFloatValue ()
67+ {
68+ $ this ->givenThereIsAUnitWithValue (self ::ZERO_FLOAT_VALUE );
69+
70+ $ this ->assertSame (self ::ZERO_FLOAT_VALUE , $ this ->unit ->getValue ());
71+ }
72+
73+ private function givenThereIsAUnitWithValue ($ value , $ unit = null )
74+ {
75+ $ this ->unit = $ unit === null ? new Unit ($ value ) : new Unit ($ value , $ unit );
76+ }
77+
78+ public function testGetUnitWithEmptyUnit ()
79+ {
80+ $ this ->givenThereIsAUnitWithUnit ("" );
81+
82+ $ this ->assertSame ("" , $ this ->unit ->getUnit ());
83+ }
84+
85+ public function testGetUnitWithStringAsUnit ()
86+ {
87+ $ this ->givenThereIsAUnitWithUnit ("Hey! I'm cmfcmf " );
88+
89+ $ this ->assertSame ("Hey! I'm cmfcmf " , $ this ->unit ->getUnit ());
90+ }
91+
92+ public function testCelsiusFixture ()
93+ {
94+ $ this ->givenThereIsAUnitWithUnit ("celsius " );
95+
96+ $ this ->assertSame ("\xB0C " , $ this ->unit ->getUnit ());
97+ }
98+
99+ public function testFahrenheitFixture ()
100+ {
101+ $ this ->givenThereIsAUnitWithUnit ("fahrenheit " );
102+
103+ $ this ->assertSame ("F " , $ this ->unit ->getUnit ());
104+ }
105+
106+ private function givenThereIsAUnitWithUnit ($ unit )
107+ {
108+ $ this ->unit = new Unit (0 , $ unit );
109+ }
110+
111+ public function testGetDescriptionWithEmptyDescription ()
112+ {
113+ $ this ->givenThereIsAUnitWithDescription ("" );
114+
115+ $ this ->assertSame ("" , $ this ->unit ->getDescription ());
116+ }
117+
118+ public function testGetDescriptionWithStringAsDescription ()
119+ {
120+ $ this ->givenThereIsAUnitWithDescription ("Hey! I'm cmfcmf " );
121+
122+ $ this ->assertSame ("Hey! I'm cmfcmf " , $ this ->unit ->getDescription ());
123+ }
124+
125+ private function givenThereIsAUnitWithDescription ($ description )
126+ {
127+ $ this ->unit = new Unit (0 , "" , $ description );
128+ }
129+
130+ public function testGetFormattedWithoutUnit ()
131+ {
132+ $ this ->givenThereIsAUnitWithValue (self ::POSITIVE_INT_VALUE );
133+
134+ $ this ->assertEquals (self ::POSITIVE_INT_VALUE , $ this ->unit ->getFormatted ());
135+ $ this ->assertEquals ($ this ->unit ->getValue (), $ this ->unit ->getFormatted ());
136+ }
137+
138+ public function testGetFormattedWithUnit ()
139+ {
140+ $ this ->givenThereIsAUnitWithValue (self ::POSITIVE_INT_VALUE , 'K ' );
141+
142+ $ this ->assertEquals (self ::POSITIVE_INT_VALUE . ' K ' , $ this ->unit ->getFormatted ());
143+ $ this ->assertEquals ($ this ->unit ->getValue () . ' ' . $ this ->unit ->getUnit (), $ this ->unit ->getFormatted ());
144+ }
145+
146+ public function testToString ()
147+ {
148+ $ this ->givenThereIsAUnitWithValue (self ::POSITIVE_INT_VALUE , 'K ' );
149+
150+ $ this ->assertEquals ($ this ->unit ->getFormatted (), $ this ->unit );
151+ }
152+ }
0 commit comments