NumberTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. }
  56. /**
  57. * NumberTest::testPrice()
  58. *
  59. * @return void
  60. */
  61. public function testPrice() {
  62. $is = Number::price(22.11);
  63. $expected = '22,11 €';
  64. $this->assertSame($expected, $is);
  65. $is = Number::price(-22.11);
  66. $expected = '0,00 €';
  67. $this->assertSame($expected, $is);
  68. }
  69. /**
  70. * NumberTest::testCurrency()
  71. *
  72. * @return void
  73. */
  74. public function testCurrency() {
  75. $is = Number::currency(22.11);
  76. $expected = '22,11 €';
  77. $this->assertSame($expected, $is);
  78. $is = Number::currency(-22.11);
  79. $expected = '-22,11 €';
  80. $this->assertSame($expected, $is);
  81. $is = Number::currency(-22.11, 'EUR', array('signed' => true));
  82. $expected = '-22,11 €';
  83. $this->assertSame($expected, $is);
  84. $is = Number::currency(22.11, 'EUR', array('signed' => true));
  85. $expected = '+22,11 €';
  86. $this->assertSame($expected, $is);
  87. }
  88. /**
  89. * NumberTest::testFormat()
  90. *
  91. * @return void
  92. */
  93. public function testFormat() {
  94. $is = Number::format(22.11);
  95. $expected = '22,11';
  96. $this->assertSame($expected, $is);
  97. $is = Number::format(22933773);
  98. $expected = '22.933.773,00';
  99. $this->assertSame($expected, $is);
  100. $is = Number::format(-0.895, array('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('decimals' => ','));
  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. */
  154. public function testRoundUpTo() {
  155. //increment = 10
  156. $values = array(
  157. '22.765' => 30.0,
  158. '15.22' => 20.0,
  159. '3.4' => 10.0,
  160. '6' => 10.0,
  161. '-3.12' => -0.0,
  162. '-10' => -10.0
  163. );
  164. foreach ($values as $was => $expected) {
  165. $is = Number::roundUpTo($was, 10);
  166. $this->assertSame($expected, $is, null, $was);
  167. }
  168. //increment = 5
  169. $values = array(
  170. '22' => 25.0,
  171. '15.234' => 20.0,
  172. '3.4' => 5.0,
  173. '6.131' => 10.0,
  174. '-3.17' => -0.0,
  175. '-10.99' => -10.0
  176. );
  177. foreach ($values as $was => $expected) {
  178. $is = Number::roundUpTo($was, 5);
  179. $this->assertSame($expected, $is, null, $was);
  180. }
  181. }
  182. /**
  183. */
  184. public function testRoundDownTo() {
  185. //increment = 10
  186. $values = array(
  187. '22.765' => 20.0,
  188. '15.22' => 10.0,
  189. '3.4' => 0.0,
  190. '6' => 0.0,
  191. '-3.12' => -10.0,
  192. '-10' => -10.0
  193. );
  194. foreach ($values as $was => $expected) {
  195. $is = Number::roundDownTo($was, 10);
  196. $this->assertSame($expected, $is, null, $was);
  197. }
  198. //increment = 3
  199. $values = array(
  200. '22' => 21.0,
  201. '15.234' => 15.0,
  202. '3.4' => 3.0,
  203. '6.131' => 6.0,
  204. '-3.17' => -6.0,
  205. '-10.99' => -12.0
  206. );
  207. foreach ($values as $was => $expected) {
  208. $is = Number::roundDownTo($was, 3);
  209. $this->assertSame($expected, $is, null, $was);
  210. }
  211. }
  212. /**
  213. */
  214. public function testGetDecimalPlaces() {
  215. $values = array(
  216. '100' => -2,
  217. '0.0001' => 4,
  218. '10' => -1,
  219. '0.1' => 1,
  220. '1' => 0,
  221. '0.001' => 3
  222. );
  223. foreach ($values as $was => $expected) {
  224. $is = Number::getDecimalPlaces($was, 10);
  225. $this->assertSame($expected, $is); //, null, $was
  226. }
  227. }
  228. /**
  229. * Test spacer format options for currency() method
  230. *
  231. * @return void
  232. */
  233. public function testCurrencySpacer() {
  234. if ((float)Configure::version() < 2.4) {
  235. $format = Number::getFormat('GBP');
  236. $format['wholeSymbol'] = '£';
  237. Number::addFormat('GBP', $format);
  238. }
  239. $result = Number::currency('4.111', 'GBP');
  240. $expected = '£4.11';
  241. $this->assertEquals($expected, $result);
  242. $result = Number::currency('4.111', 'GBP', array('spacer' => false));
  243. $expected = '£4.11';
  244. $this->assertEquals($expected, $result);
  245. $result = Number::currency('4.111', 'GBP', array('spacer' => true));
  246. $expected = '£ 4.11';
  247. $this->assertEquals($expected, $result);
  248. $result = Number::currency('-4.111', 'GBP', array('spacer' => false, 'negative' => '-'));
  249. $expected = '-£4.11';
  250. $this->assertEquals($expected, $result);
  251. $result = Number::currency('-4.111', 'GBP', array('spacer' => true, 'negative' => '-'));
  252. $expected = '-£ 4.11';
  253. $this->assertEquals($expected, $result);
  254. $result = Number::currency('4.111', 'GBP', array('spacer' => '&nbsp;', 'escape' => false));
  255. $expected = '£&nbsp;4.11';
  256. $this->assertEquals($expected, $result);
  257. }
  258. /**
  259. * NumberTest::testCurrencyUnknown()
  260. *
  261. * @return void
  262. */
  263. public function testCurrencyUnknown() {
  264. $result = Number::currency('4.111', 'XYZ');
  265. $expected = 'XYZ 4,11';
  266. $this->assertEquals($expected, $result);
  267. }
  268. }