NumberTest.php 5.8 KB

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