NumberLibTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * @return void
  46. */
  47. public function testToPercentage() {
  48. $is = NumberLib::toPercentage(22.11, 2, array('decimals' => '.'));
  49. $expected = '22.11%';
  50. $this->assertSame($expected, $is);
  51. $is = NumberLib::toPercentage(22.11, 2, array('decimals' => ','));
  52. $expected = '22,11%';
  53. $this->assertSame($expected, $is);
  54. $is = NumberLib::toPercentage(22.11, 0, array('decimals' => '.'));
  55. $expected = '22%';
  56. $this->assertSame($expected, $is);
  57. $is = NumberLib::toPercentage(0.2311, 0, array('multiply' => true, 'decimals' => '.'));
  58. $expected = '23%';
  59. $this->assertSame($expected, $is);
  60. }
  61. /**
  62. * @return void
  63. */
  64. public function testRoundTo() {
  65. //increment = 10
  66. $values = array(
  67. '22' => 20,
  68. '15' => 20,
  69. '3.4' => 0,
  70. '6' => 10,
  71. '-3.12' => 0,
  72. '-10' => -10
  73. );
  74. foreach ($values as $was => $expected) {
  75. $is = NumberLib::roundTo($was, 10);
  76. $this->assertSame($expected, $is, null, $was);
  77. }
  78. //increment = 0.1
  79. $values2 = array(
  80. '22' => 22.0,
  81. '15.234' => 15.2,
  82. '3.4' => 3.4,
  83. '6.131' => 6.1,
  84. '-3.17' => -3.2,
  85. '-10.99' => -11.0
  86. );
  87. foreach ($values2 as $was => $expected) {
  88. $is = NumberLib::roundTo($was, 0.1);
  89. $this->assertSame($expected, $is, null, $was);
  90. }
  91. }
  92. /**
  93. */
  94. public function testRoundUpTo() {
  95. //increment = 10
  96. $values = array(
  97. '22.765' => 30.0,
  98. '15.22' => 20.0,
  99. '3.4' => 10.0,
  100. '6' => 10.0,
  101. '-3.12' => -0.0,
  102. '-10' => -10.0
  103. );
  104. foreach ($values as $was => $expected) {
  105. $is = NumberLib::roundUpTo($was, 10);
  106. $this->assertSame($expected, $is, null, $was);
  107. }
  108. //increment = 5
  109. $values = array(
  110. '22' => 25.0,
  111. '15.234' => 20.0,
  112. '3.4' => 5.0,
  113. '6.131' => 10.0,
  114. '-3.17' => -0.0,
  115. '-10.99' => -10.0
  116. );
  117. foreach ($values as $was => $expected) {
  118. $is = NumberLib::roundUpTo($was, 5);
  119. $this->assertSame($expected, $is, null, $was);
  120. }
  121. }
  122. /**
  123. */
  124. public function testRoundDownTo() {
  125. //increment = 10
  126. $values = array(
  127. '22.765' => 20.0,
  128. '15.22' => 10.0,
  129. '3.4' => 0.0,
  130. '6' => 0.0,
  131. '-3.12' => -10.0,
  132. '-10' => -10.0
  133. );
  134. foreach ($values as $was => $expected) {
  135. $is = NumberLib::roundDownTo($was, 10);
  136. $this->assertSame($expected, $is, null, $was);
  137. }
  138. //increment = 3
  139. $values = array(
  140. '22' => 21.0,
  141. '15.234' => 15.0,
  142. '3.4' => 3.0,
  143. '6.131' => 6.0,
  144. '-3.17' => -6.0,
  145. '-10.99' => -12.0
  146. );
  147. foreach ($values as $was => $expected) {
  148. $is = NumberLib::roundDownTo($was, 3);
  149. $this->assertSame($expected, $is, null, $was);
  150. }
  151. }
  152. /**
  153. */
  154. public function testGetDecimalPlaces() {
  155. $values = array(
  156. '100' => -2,
  157. '0.0001' => 4,
  158. '10' => -1,
  159. '0.1' => 1,
  160. '1' => 0,
  161. '0.001' => 3
  162. );
  163. foreach ($values as $was => $expected) {
  164. $is = NumberLib::getDecimalPlaces($was, 10);
  165. $this->assertSame($expected, $is); //, null, $was
  166. }
  167. }
  168. /**
  169. * Test spacer format options for currency() method
  170. *
  171. * @return void
  172. */
  173. public function testCurrencySpacer() {
  174. if ((float)Configure::version() < 2.4) {
  175. $format = NumberLib::getFormat('GBP');
  176. $format['wholeSymbol'] = '£';
  177. NumberLib::addFormat('GBP', $format);
  178. }
  179. $result = NumberLib::currency('4.111', 'GBP');
  180. $expected = '£4.11';
  181. $this->assertEquals($expected, $result);
  182. $result = NumberLib::currency('4.111', 'GBP', array('spacer' => false));
  183. $expected = '£4.11';
  184. $this->assertEquals($expected, $result);
  185. $result = NumberLib::currency('4.111', 'GBP', array('spacer' => true));
  186. $expected = '£ 4.11';
  187. $this->assertEquals($expected, $result);
  188. $result = NumberLib::currency('-4.111', 'GBP', array('spacer' => false, 'negative' => '-'));
  189. $expected = '-£4.11';
  190. $this->assertEquals($expected, $result);
  191. $result = NumberLib::currency('-4.111', 'GBP', array('spacer' => true, 'negative' => '-'));
  192. $expected = '-£ 4.11';
  193. $this->assertEquals($expected, $result);
  194. $result = NumberLib::currency('4.111', 'GBP', array('spacer' => '&nbsp;', 'escape' => false));
  195. $expected = '£&nbsp;4.11';
  196. $this->assertEquals($expected, $result);
  197. }
  198. /**
  199. * NumberLibTest::testCurrencyUnknown()
  200. *
  201. * @return void
  202. */
  203. public function testCurrencyUnknown() {
  204. $result = NumberLib::currency('4.111', 'XYZ');
  205. $expected = 'XYZ 4,11';
  206. $this->assertEquals($expected, $result);
  207. }
  208. }