NumberLibTest.php 6.0 KB

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