NumberTest.php 5.9 KB

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