NumberLibTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * 2012-04-06 ms
  46. */
  47. public function testToPercentage() {
  48. $is = NumberLib::toPercentage(22.11, 2, '.');
  49. $expected = '22.11%';
  50. $this->assertSame($expected, $is);
  51. $is = NumberLib::toPercentage(22.11, 2, ',');
  52. $expected = '22,11%';
  53. $this->assertSame($expected, $is);
  54. $is = NumberLib::toPercentage(22.11, 0, ',');
  55. $expected = '22%';
  56. $this->assertSame($expected, $is);
  57. }
  58. /**
  59. *2011-04-14 lb
  60. */
  61. public function testRoundTo() {
  62. //increment = 10
  63. $values = array(
  64. '22' => 20,
  65. '15' => 20,
  66. '3.4' => 0,
  67. '6' => 10,
  68. '-3.12' => 0,
  69. '-10' => -10
  70. );
  71. foreach ($values as $was => $expected) {
  72. $is = NumberLib::roundTo($was, 10);
  73. $this->assertSame($expected, $is, null, $was);
  74. }
  75. //increment = 0.1
  76. $values2 = array(
  77. '22' => 22.0,
  78. '15.234' => 15.2,
  79. '3.4' => 3.4,
  80. '6.131' => 6.1,
  81. '-3.17' => -3.2,
  82. '-10.99' => -11.0
  83. );
  84. foreach ($values2 as $was => $expected) {
  85. $is = NumberLib::roundTo($was, 0.1);
  86. $this->assertSame($expected, $is, null, $was);
  87. }
  88. }
  89. /**
  90. *2011-04-14 lb
  91. */
  92. public function testRoundUpTo() {
  93. //increment = 10
  94. $values = array(
  95. '22.765' => 30.0,
  96. '15.22' => 20.0,
  97. '3.4' => 10.0,
  98. '6' => 10.0,
  99. '-3.12' => -0.0,
  100. '-10' => -10.0
  101. );
  102. foreach ($values as $was => $expected) {
  103. $is = NumberLib::roundUpTo($was, 10);
  104. $this->assertSame($expected, $is, null, $was);
  105. }
  106. //increment = 5
  107. $values = array(
  108. '22' => 25.0,
  109. '15.234' => 20.0,
  110. '3.4' => 5.0,
  111. '6.131' => 10.0,
  112. '-3.17' => -0.0,
  113. '-10.99' => -10.0
  114. );
  115. foreach ($values as $was => $expected) {
  116. $is = NumberLib::roundUpTo($was, 5);
  117. $this->assertSame($expected, $is, null, $was);
  118. }
  119. }
  120. /**
  121. *2011-04-14 lb
  122. */
  123. public function testRoundDownTo() {
  124. //increment = 10
  125. $values = array(
  126. '22.765' => 20.0,
  127. '15.22' => 10.0,
  128. '3.4' => 0.0,
  129. '6' => 0.0,
  130. '-3.12' => -10.0,
  131. '-10' => -10.0
  132. );
  133. foreach ($values as $was => $expected) {
  134. $is = NumberLib::roundDownTo($was, 10);
  135. $this->assertSame($expected, $is, null, $was);
  136. }
  137. //increment = 3
  138. $values = array(
  139. '22' => 21.0,
  140. '15.234' => 15.0,
  141. '3.4' => 3.0,
  142. '6.131' => 6.0,
  143. '-3.17' => -6.0,
  144. '-10.99' => -12.0
  145. );
  146. foreach ($values as $was => $expected) {
  147. $is = NumberLib::roundDownTo($was, 3);
  148. $this->assertSame($expected, $is, null, $was);
  149. }
  150. }
  151. /**
  152. *2011-04-15 lb
  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. }