NumberLibTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. App::uses('NumberLib', 'Tools.Utility');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class NumberLibTest extends MyCakeTestCase {
  5. public $NumberLib = null;
  6. public function setUp() {
  7. parent::setUp();
  8. Configure::write('Localization', array(
  9. 'decimals' => ',',
  10. 'thousands' => '.'
  11. ));
  12. NumberLib::config();
  13. }
  14. public function testMoney() {
  15. $is = NumberLib::money(22.11);
  16. $expected = '22,11 €';
  17. $this->assertSame($expected, $is);
  18. $is = NumberLib::money(-22.11);
  19. $expected = '-22,11 €';
  20. $this->assertSame($expected, $is);
  21. }
  22. public function testPrice() {
  23. $is = NumberLib::price(22.11);
  24. $expected = '22,11 €';
  25. $this->assertSame($expected, $is);
  26. $is = NumberLib::price(-22.11);
  27. $expected = '0,00 €';
  28. $this->assertSame($expected, $is);
  29. }
  30. public function testCurrency() {
  31. $is = NumberLib::currency(22.11);
  32. $expected = '22,11 €';
  33. $this->assertSame($expected, $is);
  34. $is = NumberLib::currency(-22.11);
  35. $expected = '-22,11 €';
  36. $this->assertSame($expected, $is);
  37. $is = NumberLib::currency(-22.11, 'EUR', array('signed'=>true));
  38. $expected = '-22,11 €';
  39. $this->assertSame($expected, $is);
  40. $is = NumberLib::currency(22.11, 'EUR', array('signed'=>true));
  41. $expected = '+22,11 €';
  42. $this->assertSame($expected, $is);
  43. }
  44. /**
  45. */
  46. public function testToPercentage() {
  47. $is = NumberLib::toPercentage(22.11, 2, '.');
  48. $expected = '22.11%';
  49. $this->assertSame($expected, $is);
  50. $is = NumberLib::toPercentage(22.11, 2, ',');
  51. $expected = '22,11%';
  52. $this->assertSame($expected, $is);
  53. $is = NumberLib::toPercentage(22.11, 0, ',');
  54. $expected = '22%';
  55. $this->assertSame($expected, $is);
  56. }
  57. /**
  58. */
  59. public function testRoundTo() {
  60. //increment = 10
  61. $values = array(
  62. '22' => 20,
  63. '15' => 20,
  64. '3.4' => 0,
  65. '6' => 10,
  66. '-3.12' => 0,
  67. '-10' => -10
  68. );
  69. foreach ($values as $was => $expected) {
  70. $is = NumberLib::roundTo($was, 10);
  71. $this->assertSame($expected, $is, null, $was);
  72. }
  73. //increment = 0.1
  74. $values2 = array(
  75. '22' => 22.0,
  76. '15.234' => 15.2,
  77. '3.4' => 3.4,
  78. '6.131' => 6.1,
  79. '-3.17' => -3.2,
  80. '-10.99' => -11.0
  81. );
  82. foreach ($values2 as $was => $expected) {
  83. $is = NumberLib::roundTo($was, 0.1);
  84. $this->assertSame($expected, $is, null, $was);
  85. }
  86. }
  87. /**
  88. */
  89. public function testRoundUpTo() {
  90. //increment = 10
  91. $values = array(
  92. '22.765' => 30.0,
  93. '15.22' => 20.0,
  94. '3.4' => 10.0,
  95. '6' => 10.0,
  96. '-3.12' => -0.0,
  97. '-10' => -10.0
  98. );
  99. foreach ($values as $was => $expected) {
  100. $is = NumberLib::roundUpTo($was, 10);
  101. $this->assertSame($expected, $is, null, $was);
  102. }
  103. //increment = 5
  104. $values = array(
  105. '22' => 25.0,
  106. '15.234' => 20.0,
  107. '3.4' => 5.0,
  108. '6.131' => 10.0,
  109. '-3.17' => -0.0,
  110. '-10.99' => -10.0
  111. );
  112. foreach ($values as $was => $expected) {
  113. $is = NumberLib::roundUpTo($was, 5);
  114. $this->assertSame($expected, $is, null, $was);
  115. }
  116. }
  117. /**
  118. */
  119. public function testRoundDownTo() {
  120. //increment = 10
  121. $values = array(
  122. '22.765' => 20.0,
  123. '15.22' => 10.0,
  124. '3.4' => 0.0,
  125. '6' => 0.0,
  126. '-3.12' => -10.0,
  127. '-10' => -10.0
  128. );
  129. foreach ($values as $was => $expected) {
  130. $is = NumberLib::roundDownTo($was, 10);
  131. $this->assertSame($expected, $is, null, $was);
  132. }
  133. //increment = 3
  134. $values = array(
  135. '22' => 21.0,
  136. '15.234' => 15.0,
  137. '3.4' => 3.0,
  138. '6.131' => 6.0,
  139. '-3.17' => -6.0,
  140. '-10.99' => -12.0
  141. );
  142. foreach ($values as $was => $expected) {
  143. $is = NumberLib::roundDownTo($was, 3);
  144. $this->assertSame($expected, $is, null, $was);
  145. }
  146. }
  147. /**
  148. */
  149. public function testGetDecimalPlaces() {
  150. $values = array(
  151. '100' => -2,
  152. '0.0001' => 4,
  153. '10' => -1,
  154. '0.1' => 1,
  155. '1' => 0,
  156. '0.001' => 3
  157. );
  158. foreach ($values as $was => $expected) {
  159. $is = NumberLib::getDecimalPlaces($was, 10);
  160. $this->assertSame($expected, $is); //, null, $was
  161. }
  162. }
  163. /**
  164. * Test spacer format options for currency() method
  165. *
  166. * @return void
  167. */
  168. public function testCurrencySpacer() {
  169. if ((float)Configure::version() < 2.4) {
  170. $format = NumberLib::getFormat('GBP');
  171. $format['wholeSymbol'] = '£';
  172. NumberLib::addFormat('GBP', $format);
  173. }
  174. $result = NumberLib::currency('4.111', 'GBP');
  175. $expected = '£4.11';
  176. $this->assertEquals($expected, $result);
  177. $result = NumberLib::currency('4.111', 'GBP', array('spacer' => false));
  178. $expected = '£4.11';
  179. $this->assertEquals($expected, $result);
  180. $result = NumberLib::currency('4.111', 'GBP', array('spacer' => true));
  181. $expected = '£ 4.11';
  182. $this->assertEquals($expected, $result);
  183. $result = NumberLib::currency('-4.111', 'GBP', array('spacer' => false, 'negative' => '-'));
  184. $expected = '-£4.11';
  185. $this->assertEquals($expected, $result);
  186. $result = NumberLib::currency('-4.111', 'GBP', array('spacer' => true, 'negative' => '-'));
  187. $expected = '-£ 4.11';
  188. $this->assertEquals($expected, $result);
  189. $result = NumberLib::currency('4.111', 'GBP', array('spacer' => '&nbsp;', 'escape' => false));
  190. $expected = '£&nbsp;4.11';
  191. $this->assertEquals($expected, $result);
  192. }
  193. /**
  194. * NumberLibTest::testCurrencyUnknown()
  195. *
  196. * @return void
  197. */
  198. public function testCurrencyUnknown() {
  199. $result = NumberLib::currency('4.111', 'XYZ');
  200. $expected = 'XYZ 4,11';
  201. $this->assertEquals($expected, $result);
  202. }
  203. }