NumberTest.php 6.8 KB

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