NumberLibTest.php 6.1 KB

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