3 * Copyright Zikula Foundation 2014 - Zikula Application Framework
5 * This work is contributed to the Zikula Foundation under one or more
6 * Contributor Agreements and licensed to You under the following license:
8 * @license GNU/LGPv3 (or at your option any later version).
9 * @package OpenWeatherMap-PHP-Api
11 * Please see the NOTICE file distributed with this source code for further
12 * information regarding copyright and licensing.
15 namespace Cmfcmf\OpenWeatherMap\Tests\Util;
17 use \Cmfcmf\OpenWeatherMap\Util\Unit;
19 class UnitTest extends \PHPUnit_Framework_TestCase
26 const POSITIVE_INT_VALUE = 23;
28 const POSITIVE_FLOAT_VALUE = 48.23534;
30 const NEGATIVE_INT_VALUE = -30;
32 const NEGATIVE_FLOAT_VALUE = -93.45839;
34 const ZERO_INT_VALUE = 0;
36 const ZERO_FLOAT_VALUE = 0.0;
38 public function testGetValueWithPositiveIntValue()
40 $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE);
42 $this->assertSame((float)self::POSITIVE_INT_VALUE, $this->unit->getValue());
45 public function testGetValueWithPositiveFloatValue()
47 $this->givenThereIsAUnitWithValue(self::POSITIVE_FLOAT_VALUE);
49 $this->assertSame(self::POSITIVE_FLOAT_VALUE, $this->unit->getValue());
52 public function testGetValueWithNegativeIntValue()
54 $this->givenThereIsAUnitWithValue(self::NEGATIVE_INT_VALUE);
56 $this->assertSame((float)self::NEGATIVE_INT_VALUE, $this->unit->getValue());
59 public function testGetValueWithNegativeFloatValue()
61 $this->givenThereIsAUnitWithValue(self::NEGATIVE_FLOAT_VALUE);
63 $this->assertSame(self::NEGATIVE_FLOAT_VALUE, $this->unit->getValue());
66 public function testGetValueWithZeroIntValue()
68 $this->givenThereIsAUnitWithValue(self::ZERO_INT_VALUE);
70 $this->assertSame((float)self::ZERO_INT_VALUE, $this->unit->getValue());
73 public function testGetValueWithZeroFloatValue()
75 $this->givenThereIsAUnitWithValue(self::ZERO_FLOAT_VALUE);
77 $this->assertSame(self::ZERO_FLOAT_VALUE, $this->unit->getValue());
80 private function givenThereIsAUnitWithValue($value, $unit = null)
82 $this->unit = $unit === null ? new Unit($value) : new Unit($value, $unit);
85 public function testGetUnitWithEmptyUnit()
87 $this->givenThereIsAUnitWithUnit("");
89 $this->assertSame("", $this->unit->getUnit());
92 public function testGetUnitWithStringAsUnit()
94 $this->givenThereIsAUnitWithUnit("Hey! I'm cmfcmf");
96 $this->assertSame("Hey! I'm cmfcmf", $this->unit->getUnit());
99 public function testCelsiusFixture()
101 $this->givenThereIsAUnitWithUnit("celsius");
103 $this->assertSame("°C", $this->unit->getUnit());
106 public function testFahrenheitFixture()
108 $this->givenThereIsAUnitWithUnit("fahrenheit");
110 $this->assertSame("F", $this->unit->getUnit());
113 private function givenThereIsAUnitWithUnit($unit)
115 $this->unit = new Unit(0, $unit);
118 public function testGetDescriptionWithEmptyDescription()
120 $this->givenThereIsAUnitWithDescription("");
122 $this->assertSame("", $this->unit->getDescription());
125 public function testGetDescriptionWithStringAsDescription()
127 $this->givenThereIsAUnitWithDescription("Hey! I'm cmfcmf");
129 $this->assertSame("Hey! I'm cmfcmf", $this->unit->getDescription());
132 private function givenThereIsAUnitWithDescription($description)
134 $this->unit = new Unit(0, "", $description);
137 public function testGetFormattedWithoutUnit()
139 $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE);
141 $this->assertEquals(self::POSITIVE_INT_VALUE, $this->unit->getFormatted());
142 $this->assertEquals($this->unit->getValue(), $this->unit->getFormatted());
145 public function testGetFormattedWithUnit()
147 $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE, 'K');
149 $this->assertEquals(self::POSITIVE_INT_VALUE . ' K', $this->unit->getFormatted());
150 $this->assertEquals($this->unit->getValue() . ' ' . $this->unit->getUnit(), $this->unit->getFormatted());
153 public function testToString()
155 $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE, 'K');
157 $this->assertEquals($this->unit->getFormatted(), $this->unit);