NumberLibTest.php 6.7 KB

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