NumberTest.php 6.4 KB

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