NumberTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. /**
  3. * NumberTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since 1.2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Utility;
  18. use Cake\I18n\I18n;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\Utility\Number;
  21. /**
  22. * NumberTest class
  23. *
  24. */
  25. class NumberTest extends TestCase {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. parent::setUp();
  33. $this->Number = new Number();
  34. $this->locale = I18n::defaultLocale();
  35. }
  36. /**
  37. * tearDown method
  38. *
  39. * @return void
  40. */
  41. public function tearDown() {
  42. parent::tearDown();
  43. unset($this->Number);
  44. I18n::defaultLocale($this->locale);
  45. Number::defaultCurrency(false);
  46. }
  47. /**
  48. * testFormatAndCurrency method
  49. *
  50. * @return void
  51. */
  52. public function testFormat() {
  53. $value = '100100100';
  54. $result = $this->Number->format($value);
  55. $expected = '100,100,100';
  56. $this->assertEquals($expected, $result);
  57. $result = $this->Number->format($value, array('before' => '#'));
  58. $expected = '#100,100,100';
  59. $this->assertEquals($expected, $result);
  60. $result = $this->Number->format($value, array('places' => 3));
  61. $expected = '100,100,100.000';
  62. $this->assertEquals($expected, $result);
  63. $result = $this->Number->format($value, array('locale' => 'es_VE'));
  64. $expected = '100.100.100';
  65. $this->assertEquals($expected, $result);
  66. $value = 0.00001;
  67. $result = $this->Number->format($value, array('places' => 1, 'before' => '$'));
  68. $expected = '$0.0';
  69. $this->assertEquals($expected, $result);
  70. $value = -0.00001;
  71. $result = $this->Number->format($value, array('places' => 1, 'before' => '$'));
  72. $expected = '$-0.0';
  73. $this->assertEquals($expected, $result);
  74. $value = 1.23;
  75. $options = array('locale' => 'fr_FR', 'after' => ' €');
  76. $result = $this->Number->format($value, $options);
  77. $expected = '1,23 €';
  78. $this->assertEquals($expected, $result);
  79. }
  80. /**
  81. * testFormatDelta method
  82. *
  83. * @return void
  84. */
  85. public function testFormatDelta() {
  86. $value = '100100100';
  87. $result = $this->Number->formatDelta($value, array('places' => 0));
  88. $expected = '+100,100,100';
  89. $this->assertEquals($expected, $result);
  90. $result = $this->Number->formatDelta($value, array('before' => '', 'after' => ''));
  91. $expected = '+100,100,100';
  92. $this->assertEquals($expected, $result);
  93. $result = $this->Number->formatDelta($value, array('before' => '[', 'after' => ']'));
  94. $expected = '[+100,100,100]';
  95. $this->assertEquals($expected, $result);
  96. $result = $this->Number->formatDelta(-$value, array('before' => '[', 'after' => ']'));
  97. $expected = '[-100,100,100]';
  98. $this->assertEquals($expected, $result);
  99. $result = $this->Number->formatDelta(-$value, array('before' => '[ ', 'after' => ' ]'));
  100. $expected = '[ -100,100,100 ]';
  101. $this->assertEquals($expected, $result);
  102. $value = 0;
  103. $result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']'));
  104. $expected = '[0.0]';
  105. $this->assertEquals($expected, $result);
  106. $value = 0.0001;
  107. $result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']'));
  108. $expected = '[0.0]';
  109. $this->assertEquals($expected, $result);
  110. $value = 9876.1234;
  111. $result = $this->Number->formatDelta($value, array('places' => 1, 'locale' => 'de_DE'));
  112. $expected = '+9.876,1';
  113. $this->assertEquals($expected, $result);
  114. }
  115. /**
  116. * Test currency method.
  117. *
  118. * @return void
  119. */
  120. public function testCurrency() {
  121. $value = '100100100';
  122. $result = $this->Number->currency($value);
  123. $expected = '$100,100,100.00';
  124. $this->assertEquals($expected, $result);
  125. $result = $this->Number->currency($value, 'USD');
  126. $expected = '$100,100,100.00';
  127. $this->assertEquals($expected, $result);
  128. $result = $this->Number->currency($value, 'EUR');
  129. $expected = '€100,100,100.00';
  130. $this->assertEquals($expected, $result);
  131. $result = $this->Number->currency($value, 'EUR', ['locale' => 'de_DE']);
  132. $expected = '100.100.100,00 €';
  133. $this->assertEquals($expected, $result);
  134. $result = $this->Number->currency($value, 'USD', ['locale' => 'de_DE']);
  135. $expected = '100.100.100,00 $';
  136. $this->assertEquals($expected, $result);
  137. $result = $this->Number->currency($value, 'USD', ['locale' => 'en_US']);
  138. $expected = '$100,100,100.00';
  139. $this->assertEquals($expected, $result);
  140. $result = $this->Number->currency($value, 'USD', ['locale' => 'en_CA']);
  141. $expected = 'US$100,100,100.00';
  142. $this->assertEquals($expected, $result);
  143. $result = $this->Number->currency($value, 'INR', ['locale' => 'en_IN']);
  144. $expected = '₹ 10,01,00,100.00';
  145. $this->assertEquals($expected, $result);
  146. $options = ['locale' => 'en_IN', 'pattern' => "Rs'.' #,##,###"];
  147. $result = $this->Number->currency($value, 'INR', $options);
  148. $expected = 'Rs. 10,01,00,100';
  149. $this->assertEquals($expected, $result);
  150. $result = $this->Number->currency($value, 'GBP');
  151. $expected = '£100,100,100.00';
  152. $this->assertEquals($expected, $result);
  153. $result = $this->Number->currency($value, 'GBP', ['locale' => 'da_DK']);
  154. $expected = '100.100.100,00 £';
  155. $this->assertEquals($expected, $result);
  156. $options = ['locale' => 'fr_FR', 'pattern' => 'EUR #,###.00'];
  157. $result = $this->Number->currency($value, 'EUR', $options);
  158. $expected = 'EUR 100 100 100,00';
  159. $this->assertEquals($expected, $result);
  160. $options = ['locale' => 'fr_FR', 'pattern' => '#,###.00 ¤¤'];
  161. $result = $this->Number->currency($value, 'EUR', $options);
  162. $expected = '100 100 100,00 EUR';
  163. $this->assertEquals($expected, $result);
  164. $options = ['locale' => 'fr_FR', 'pattern' => '#,###.00;(¤#,###.00)'];
  165. $result = $this->Number->currency(-1235.03, 'EUR', $options);
  166. $expected = '(€1 235,03)';
  167. $this->assertEquals($expected, $result);
  168. $result = $this->Number->currency(0.5, 'USD', ['locale' => 'en_US', 'fractionSymbol' => 'c']);
  169. $expected = '50c';
  170. $this->assertEquals($expected, $result);
  171. $options = ['fractionSymbol' => ' cents'];
  172. $result = $this->Number->currency(0.2, 'USD', $options);
  173. $expected = '20 cents';
  174. $this->assertEquals($expected, $result);
  175. $options = ['fractionSymbol' => 'cents ', 'fractionPosition' => 'before'];
  176. $result = $this->Number->currency(0.2, null, $options);
  177. $expected = 'cents 20';
  178. $this->assertEquals($expected, $result);
  179. $result = $this->Number->currency(0.2, 'EUR');
  180. $expected = '€0.20';
  181. $this->assertEquals($expected, $result);
  182. $options = ['fractionSymbol' => false, 'fractionPosition' => 'before'];
  183. $result = $this->Number->currency(0.5, null, $options);
  184. $expected = '$0.50';
  185. $this->assertEquals($expected, $result);
  186. $result = $this->Number->currency(0, 'GBP');
  187. $expected = '£0.00';
  188. $this->assertEquals($expected, $result);
  189. $result = $this->Number->currency(0.00000, 'GBP');
  190. $expected = '£0.00';
  191. $this->assertEquals($expected, $result);
  192. $result = $this->Number->currency('0.00000', 'GBP');
  193. $expected = '£0.00';
  194. $this->assertEquals($expected, $result);
  195. $result = $this->Number->currency('22.389', 'CAD');
  196. $expected = 'CA$22.39';
  197. $this->assertEquals($expected, $result);
  198. }
  199. /**
  200. * Test currency format with places and fraction exponents.
  201. * Places should only matter for non fraction values and vice versa.
  202. *
  203. * @return void
  204. */
  205. public function testCurrencyWithFractionAndPlaces() {
  206. $result = $this->Number->currency('1.23', 'EUR', ['locale' => 'de_DE', 'places' => 3]);
  207. $expected = '1,230 €';
  208. $this->assertEquals($expected, $result);
  209. $result = $this->Number->currency('0.23', 'GBP', ['places' => 3, 'fractionSymbol' => 'p']);
  210. $expected = '23p';
  211. $this->assertEquals($expected, $result);
  212. $result = $this->Number->currency('0.001', 'GBP', ['places' => 3, 'fractionSymbol' => 'p']);
  213. $expected = '0p';
  214. $this->assertEquals($expected, $result);
  215. $result = $this->Number->currency('1.23', 'EUR', ['locale' => 'de_DE', 'precision' => 1]);
  216. $expected = '1,2 €';
  217. $this->assertEquals($expected, $result);
  218. }
  219. /**
  220. * Test default currency
  221. *
  222. * @return void
  223. */
  224. public function testDefaultCurrency() {
  225. $result = $this->Number->defaultCurrency();
  226. $this->assertEquals('USD', $result);
  227. $this->Number->defaultCurrency(false);
  228. I18n::defaultLocale('es_ES');
  229. $this->assertEquals('EUR', $this->Number->defaultCurrency());
  230. $this->Number->defaultCurrency('JPY');
  231. $this->assertEquals('JPY', $this->Number->defaultCurrency());
  232. }
  233. /**
  234. * testCurrencyCentsNegative method
  235. *
  236. * @return void
  237. */
  238. public function testCurrencyCentsNegative() {
  239. $value = '-0.99';
  240. $result = $this->Number->currency($value, 'EUR', ['locale' => 'de_DE']);
  241. $expected = '-0,99 €';
  242. $this->assertEquals($expected, $result);
  243. $result = $this->Number->currency($value, 'USD', ['fractionSymbol' => 'c']);
  244. $expected = '-99c';
  245. $this->assertEquals($expected, $result);
  246. }
  247. /**
  248. * testCurrencyZero method
  249. *
  250. * @return void
  251. */
  252. public function testCurrencyZero() {
  253. $value = '0';
  254. $result = $this->Number->currency($value, 'USD');
  255. $expected = '$0.00';
  256. $this->assertEquals($expected, $result);
  257. $result = $this->Number->currency($value, 'EUR', ['locale' => 'fr_FR']);
  258. $expected = '0,00 €';
  259. $this->assertEquals($expected, $result);
  260. }
  261. /**
  262. * testCurrencyOptions method
  263. *
  264. * @return void
  265. */
  266. public function testCurrencyOptions() {
  267. $value = '1234567.89';
  268. $result = $this->Number->currency($value, null, array('before' => 'Total: '));
  269. $expected = 'Total: $1,234,567.89';
  270. $this->assertEquals($expected, $result);
  271. $result = $this->Number->currency($value, null, array('after' => ' in Total'));
  272. $expected = '$1,234,567.89 in Total';
  273. $this->assertEquals($expected, $result);
  274. }
  275. /**
  276. * Tests that it is possible to use the international currency code instead of the whole
  277. * when using the currency method
  278. *
  279. * @return void
  280. */
  281. public function testCurrencyIntlCode() {
  282. $value = '123';
  283. $result = $this->Number->currency($value, 'USD', ['useIntlCode' => true]);
  284. $expected = 'USD 123.00';
  285. $this->assertEquals($expected, $result);
  286. $result = $this->Number->currency($value, 'EUR', ['useIntlCode' => true]);
  287. $expected = 'EUR 123.00';
  288. $this->assertEquals($expected, $result);
  289. $result = $this->Number->currency($value, 'EUR', ['useIntlCode' => true, 'locale' => 'da_DK']);
  290. $expected = '123,00 EUR';
  291. $this->assertEquals($expected, $result);
  292. }
  293. /**
  294. * testToReadableSize method
  295. *
  296. * @return void
  297. */
  298. public function testToReadableSize() {
  299. $result = $this->Number->toReadableSize(0);
  300. $expected = '0 Bytes';
  301. $this->assertEquals($expected, $result);
  302. $result = $this->Number->toReadableSize(1);
  303. $expected = '1 Byte';
  304. $this->assertEquals($expected, $result);
  305. $result = $this->Number->toReadableSize(45);
  306. $expected = '45 Bytes';
  307. $this->assertEquals($expected, $result);
  308. $result = $this->Number->toReadableSize(1023);
  309. $expected = '1,023 Bytes';
  310. $this->assertEquals($expected, $result);
  311. $result = $this->Number->toReadableSize(1024);
  312. $expected = '1 KB';
  313. $this->assertEquals($expected, $result);
  314. $result = $this->Number->toReadableSize(1024 + 123);
  315. $expected = '1.12 KB';
  316. $this->assertEquals($expected, $result);
  317. $result = $this->Number->toReadableSize(1024 * 512);
  318. $expected = '512 KB';
  319. $this->assertEquals($expected, $result);
  320. $result = $this->Number->toReadableSize(1024 * 1024 - 1);
  321. $expected = '1 MB';
  322. $this->assertEquals($expected, $result);
  323. $result = $this->Number->toReadableSize(512.05 * 1024 * 1024);
  324. $expected = '512.05 MB';
  325. $this->assertEquals($expected, $result);
  326. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 - 1);
  327. $expected = '1 GB';
  328. $this->assertEquals($expected, $result);
  329. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 512);
  330. $expected = '512 GB';
  331. $this->assertEquals($expected, $result);
  332. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 - 1);
  333. $expected = '1 TB';
  334. $this->assertEquals($expected, $result);
  335. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 512);
  336. $expected = '512 TB';
  337. $this->assertEquals($expected, $result);
  338. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 1024 - 1);
  339. $expected = '1,024 TB';
  340. $this->assertEquals($expected, $result);
  341. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  342. $expected = '1,048,576 TB';
  343. $this->assertEquals($expected, $result);
  344. }
  345. /**
  346. * test toReadableSize() with locales
  347. *
  348. * @return void
  349. */
  350. public function testReadableSizeLocalized() {
  351. I18n::defaultLocale('fr_FR');
  352. $result = $this->Number->toReadableSize(1321205);
  353. $this->assertEquals('1,26 MB', $result);
  354. $result = $this->Number->toReadableSize(512.05 * 1024 * 1024 * 1024);
  355. $this->assertEquals('512,05 GB', $result);
  356. }
  357. /**
  358. * test precision() with locales
  359. *
  360. * @return void
  361. */
  362. public function testPrecisionLocalized() {
  363. I18n::defaultLocale('fr_FR');
  364. $result = $this->Number->precision(1.234);
  365. $this->assertEquals('1,234', $result);
  366. }
  367. /**
  368. * testToPercentage method
  369. *
  370. * @return void
  371. */
  372. public function testToPercentage() {
  373. $result = $this->Number->toPercentage(45, 0);
  374. $expected = '45%';
  375. $this->assertEquals($expected, $result);
  376. $result = $this->Number->toPercentage(45, 2);
  377. $expected = '45.00%';
  378. $this->assertEquals($expected, $result);
  379. $result = $this->Number->toPercentage(0, 0);
  380. $expected = '0%';
  381. $this->assertEquals($expected, $result);
  382. $result = $this->Number->toPercentage(0, 4);
  383. $expected = '0.0000%';
  384. $this->assertEquals($expected, $result);
  385. $result = $this->Number->toPercentage(45, 0, array('multiply' => false));
  386. $expected = '45%';
  387. $this->assertEquals($expected, $result);
  388. $result = $this->Number->toPercentage(45, 2, array('multiply' => false));
  389. $expected = '45.00%';
  390. $this->assertEquals($expected, $result);
  391. $result = $this->Number->toPercentage(0, 0, array('multiply' => false));
  392. $expected = '0%';
  393. $this->assertEquals($expected, $result);
  394. $result = $this->Number->toPercentage(0, 4, array('multiply' => false));
  395. $expected = '0.0000%';
  396. $this->assertEquals($expected, $result);
  397. $result = $this->Number->toPercentage(0.456, 0, array('multiply' => true));
  398. $expected = '46%';
  399. $this->assertEquals($expected, $result);
  400. $result = $this->Number->toPercentage(0.456, 2, array('multiply' => true));
  401. $expected = '45.60%';
  402. $this->assertEquals($expected, $result);
  403. }
  404. /**
  405. * testFromReadableSize
  406. *
  407. * @dataProvider filesizes
  408. * @return void
  409. */
  410. public function testFromReadableSize($params, $expected) {
  411. $result = $this->Number->fromReadableSize($params['size'], $params['default']);
  412. $this->assertEquals($expected, $result);
  413. }
  414. /**
  415. * testFromReadableSize
  416. *
  417. * @expectedException \Cake\Error\Exception
  418. * @return void
  419. */
  420. public function testFromReadableSizeException() {
  421. $this->Number->fromReadableSize('bogus', false);
  422. }
  423. /**
  424. * filesizes dataprovider
  425. *
  426. * @return array
  427. */
  428. public function filesizes() {
  429. return array(
  430. array(array('size' => '512B', 'default' => false), 512),
  431. array(array('size' => '1KB', 'default' => false), 1024),
  432. array(array('size' => '1.5KB', 'default' => false), 1536),
  433. array(array('size' => '1MB', 'default' => false), 1048576),
  434. array(array('size' => '1mb', 'default' => false), 1048576),
  435. array(array('size' => '1.5MB', 'default' => false), 1572864),
  436. array(array('size' => '1GB', 'default' => false), 1073741824),
  437. array(array('size' => '1.5GB', 'default' => false), 1610612736),
  438. array(array('size' => '1K', 'default' => false), 1024),
  439. array(array('size' => '1.5K', 'default' => false), 1536),
  440. array(array('size' => '1M', 'default' => false), 1048576),
  441. array(array('size' => '1m', 'default' => false), 1048576),
  442. array(array('size' => '1.5M', 'default' => false), 1572864),
  443. array(array('size' => '1G', 'default' => false), 1073741824),
  444. array(array('size' => '1.5G', 'default' => false), 1610612736),
  445. array(array('size' => '512', 'default' => 'Unknown type'), 512),
  446. array(array('size' => '2VB', 'default' => 'Unknown type'), 'Unknown type')
  447. );
  448. }
  449. }