UnitTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. namespace Cmfcmf\OpenWeatherMap\Tests\Util;
  15. use \Cmfcmf\OpenWeatherMap\Util\Unit;
  16. class UnitTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var Unit
  20. */
  21. private $unit;
  22. const POSITIVE_INT_VALUE = 23;
  23. const POSITIVE_FLOAT_VALUE = 48.23534;
  24. const NEGATIVE_INT_VALUE = -30;
  25. const NEGATIVE_FLOAT_VALUE = -93.45839;
  26. const ZERO_INT_VALUE = 0;
  27. const ZERO_FLOAT_VALUE = 0.0;
  28. public function testGetValueWithPositiveIntValue()
  29. {
  30. $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE);
  31. $this->assertSame((float)self::POSITIVE_INT_VALUE, $this->unit->getValue());
  32. }
  33. public function testGetValueWithPositiveFloatValue()
  34. {
  35. $this->givenThereIsAUnitWithValue(self::POSITIVE_FLOAT_VALUE);
  36. $this->assertSame(self::POSITIVE_FLOAT_VALUE, $this->unit->getValue());
  37. }
  38. public function testGetValueWithNegativeIntValue()
  39. {
  40. $this->givenThereIsAUnitWithValue(self::NEGATIVE_INT_VALUE);
  41. $this->assertSame((float)self::NEGATIVE_INT_VALUE, $this->unit->getValue());
  42. }
  43. public function testGetValueWithNegativeFloatValue()
  44. {
  45. $this->givenThereIsAUnitWithValue(self::NEGATIVE_FLOAT_VALUE);
  46. $this->assertSame(self::NEGATIVE_FLOAT_VALUE, $this->unit->getValue());
  47. }
  48. public function testGetValueWithZeroIntValue()
  49. {
  50. $this->givenThereIsAUnitWithValue(self::ZERO_INT_VALUE);
  51. $this->assertSame((float)self::ZERO_INT_VALUE, $this->unit->getValue());
  52. }
  53. public function testGetValueWithZeroFloatValue()
  54. {
  55. $this->givenThereIsAUnitWithValue(self::ZERO_FLOAT_VALUE);
  56. $this->assertSame(self::ZERO_FLOAT_VALUE, $this->unit->getValue());
  57. }
  58. private function givenThereIsAUnitWithValue($value, $unit = null)
  59. {
  60. $this->unit = $unit === null ? new Unit($value) : new Unit($value, $unit);
  61. }
  62. public function testGetUnitWithEmptyUnit()
  63. {
  64. $this->givenThereIsAUnitWithUnit("");
  65. $this->assertSame("", $this->unit->getUnit());
  66. }
  67. public function testGetUnitWithStringAsUnit()
  68. {
  69. $this->givenThereIsAUnitWithUnit("Hey! I'm cmfcmf");
  70. $this->assertSame("Hey! I'm cmfcmf", $this->unit->getUnit());
  71. }
  72. public function testCelsiusFix()
  73. {
  74. $this->givenThereIsAUnitWithUnit("celsius");
  75. $this->assertSame("&deg;C", $this->unit->getUnit());
  76. }
  77. public function testMetricFix()
  78. {
  79. $this->givenThereIsAUnitWithUnit("metric");
  80. $this->assertSame("&deg;C", $this->unit->getUnit());
  81. }
  82. public function testFahrenheitFix()
  83. {
  84. $this->givenThereIsAUnitWithUnit("fahrenheit");
  85. $this->assertSame("F", $this->unit->getUnit());
  86. }
  87. private function givenThereIsAUnitWithUnit($unit)
  88. {
  89. $this->unit = new Unit(0, $unit);
  90. }
  91. public function testGetDescriptionWithEmptyDescription()
  92. {
  93. $this->givenThereIsAUnitWithDescription("");
  94. $this->assertSame("", $this->unit->getDescription());
  95. }
  96. public function testGetDescriptionWithStringAsDescription()
  97. {
  98. $this->givenThereIsAUnitWithDescription("Hey! I'm cmfcmf");
  99. $this->assertSame("Hey! I'm cmfcmf", $this->unit->getDescription());
  100. }
  101. private function givenThereIsAUnitWithDescription($description)
  102. {
  103. $this->unit = new Unit(0, "", $description);
  104. }
  105. public function testGetFormattedWithoutUnit()
  106. {
  107. $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE);
  108. $this->assertEquals(self::POSITIVE_INT_VALUE, $this->unit->getFormatted());
  109. $this->assertEquals($this->unit->getValue(), $this->unit->getFormatted());
  110. }
  111. public function testGetFormattedWithUnit()
  112. {
  113. $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE, 'K');
  114. $this->assertEquals(self::POSITIVE_INT_VALUE . ' K', $this->unit->getFormatted());
  115. $this->assertEquals($this->unit->getValue() . ' ' . $this->unit->getUnit(), $this->unit->getFormatted());
  116. }
  117. public function testToString()
  118. {
  119. $this->givenThereIsAUnitWithValue(self::POSITIVE_INT_VALUE, 'K');
  120. $this->assertEquals($this->unit->getFormatted(), $this->unit);
  121. }
  122. }