NumberTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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\TestSuite\TestCase;
  19. use Cake\Utility\Number;
  20. /**
  21. * NumberTest class
  22. *
  23. */
  24. class NumberTest extends TestCase {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $this->Number = new Number();
  33. $this->locale = ini_get('intl.default_locale');
  34. }
  35. /**
  36. * tearDown method
  37. *
  38. * @return void
  39. */
  40. public function tearDown() {
  41. parent::tearDown();
  42. unset($this->Number);
  43. ini_set('intl.default_locale', $this->locale);
  44. }
  45. /**
  46. * testFormatAndCurrency method
  47. *
  48. * @return void
  49. */
  50. public function testFormat() {
  51. $value = '100100100';
  52. $result = $this->Number->format($value);
  53. $expected = '100,100,100.00';
  54. $this->assertEquals($expected, $result);
  55. $result = $this->Number->format($value, array('before' => '#'));
  56. $expected = '#100,100,100.00';
  57. $this->assertEquals($expected, $result);
  58. $result = $this->Number->format($value, array('places' => 3));
  59. $expected = '100,100,100.000';
  60. $this->assertEquals($expected, $result);
  61. $result = $this->Number->format($value, array('thousands' => '-'));
  62. $expected = '100-100-100.00';
  63. $this->assertEquals($expected, $result);
  64. $result = $this->Number->format($value, array('decimals' => ',', 'thousands' => '.'));
  65. $expected = '100.100.100,00';
  66. $this->assertEquals($expected, $result);
  67. $value = 0.00001;
  68. $result = $this->Number->format($value, array('places' => 1, 'before' => '$'));
  69. $expected = '$0.0';
  70. $this->assertEquals($expected, $result);
  71. $value = -0.00001;
  72. $result = $this->Number->format($value, array('places' => 1, 'before' => '$'));
  73. $expected = '$-0.0';
  74. $this->assertEquals($expected, $result);
  75. $value = 1.23;
  76. $options = array('decimals' => ',', 'thousands' => '.', 'after' => ' €');
  77. $result = $this->Number->format($value, $options);
  78. $expected = '1,23 €';
  79. $this->assertEquals($expected, $result);
  80. }
  81. /**
  82. * testFormatDelta method
  83. *
  84. * @return void
  85. */
  86. public function testFormatDelta() {
  87. $value = '100100100';
  88. $result = $this->Number->formatDelta($value);
  89. $expected = '+100,100,100.00';
  90. $this->assertEquals($expected, $result);
  91. $result = $this->Number->formatDelta($value, array('before' => '', 'after' => ''));
  92. $expected = '+100,100,100.00';
  93. $this->assertEquals($expected, $result);
  94. $result = $this->Number->formatDelta($value, array('before' => '[', 'after' => ']'));
  95. $expected = '[+100,100,100.00]';
  96. $this->assertEquals($expected, $result);
  97. $result = $this->Number->formatDelta(-$value, array('before' => '[', 'after' => ']'));
  98. $expected = '[-100,100,100.00]';
  99. $this->assertEquals($expected, $result);
  100. $result = $this->Number->formatDelta(-$value, array('before' => '[ ', 'after' => ' ]'));
  101. $expected = '[ -100,100,100.00 ]';
  102. $this->assertEquals($expected, $result);
  103. $value = 0;
  104. $result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']'));
  105. $expected = '[0.0]';
  106. $this->assertEquals($expected, $result);
  107. $value = 0.0001;
  108. $result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']'));
  109. $expected = '[0.0]';
  110. $this->assertEquals($expected, $result);
  111. $value = 9876.1234;
  112. $result = $this->Number->formatDelta($value, array('places' => 1, 'decimals' => ',', 'thousands' => '.'));
  113. $expected = '+9.876,1';
  114. $this->assertEquals($expected, $result);
  115. }
  116. /**
  117. * testMultibyteFormat
  118. *
  119. * @return void
  120. */
  121. public function testMultibyteFormat() {
  122. $value = '5199100.0006';
  123. $result = $this->Number->format($value, array(
  124. 'thousands' => '&nbsp;',
  125. 'decimals' => '&amp;',
  126. 'places' => 3,
  127. 'escape' => false,
  128. ));
  129. $expected = '5&nbsp;199&nbsp;100&amp;001';
  130. $this->assertEquals($expected, $result);
  131. $value = 1000.45;
  132. $result = $this->Number->format($value, array(
  133. 'thousands' => ',,',
  134. 'decimals' => '.a',
  135. 'escape' => false,
  136. ));
  137. $expected = '1,,000.a45';
  138. $this->assertEquals($expected, $result);
  139. $value = 519919827593784.00;
  140. $this->Number->addFormat('RUR', array(
  141. 'thousands' => 'ø€ƒ‡™',
  142. 'decimals' => '(§.§)',
  143. 'escape' => false,
  144. 'wholeSymbol' => '€',
  145. 'wholePosition' => 'after',
  146. ));
  147. $result = $this->Number->currency($value, 'RUR');
  148. $expected = '519ø€ƒ‡™919ø€ƒ‡™827ø€ƒ‡™593ø€ƒ‡™784(§.§)00€';
  149. $this->assertEquals($expected, $result);
  150. $value = '13371337.1337';
  151. $result = Number::format($value, array(
  152. 'thousands' => '- |-| /-\ >< () |2 -',
  153. 'decimals' => '- £€€† -',
  154. ));
  155. $expected = '13- |-| /-\ &gt;&lt; () |2 -371- |-| /-\ &gt;&lt; () |2 -337- £€€† -13';
  156. $this->assertEquals($expected, $result);
  157. }
  158. /**
  159. * Test currency method.
  160. *
  161. * @return void
  162. */
  163. public function testCurrency() {
  164. $value = '100100100';
  165. $result = $this->Number->currency($value);
  166. $expected = '$100,100,100.00';
  167. $this->assertEquals($expected, $result);
  168. $result = $this->Number->currency($value, '#');
  169. $expected = '#100,100,100.00';
  170. $this->assertEquals($expected, $result);
  171. $result = $this->Number->currency($value, false);
  172. $expected = '100,100,100.00';
  173. $this->assertEquals($expected, $result);
  174. $result = $this->Number->currency($value, 'USD');
  175. $expected = '$100,100,100.00';
  176. $this->assertEquals($expected, $result);
  177. $result = $this->Number->currency($value, 'EUR');
  178. $expected = '€100.100.100,00';
  179. $this->assertEquals($expected, $result);
  180. $result = $this->Number->currency($value, 'GBP');
  181. $expected = '£100,100,100.00';
  182. $this->assertEquals($expected, $result);
  183. $options = array('thousands' => ' ', 'wholeSymbol' => 'EUR ', 'wholePosition' => 'before',
  184. 'decimals' => ',', 'zero' => 'Gratuit');
  185. $result = $this->Number->currency($value, '', $options);
  186. $expected = 'EUR 100 100 100,00';
  187. $this->assertEquals($expected, $result);
  188. $options = array('after' => 'øre', 'before' => 'Kr.', 'decimals' => ',', 'thousands' => '.');
  189. $result = $this->Number->currency(1000.45, null, $options);
  190. $expected = 'Kr.1.000,45';
  191. $this->assertEquals($expected, $result);
  192. $result = $this->Number->currency(0.5, 'USD');
  193. $expected = '50c';
  194. $this->assertEquals($expected, $result);
  195. $result = $this->Number->currency(0.5, null, array('after' => 'øre'));
  196. $expected = '50øre';
  197. $this->assertEquals($expected, $result);
  198. $result = $this->Number->currency(1, null, array('wholeSymbol' => '$ '));
  199. $expected = '$ 1.00';
  200. $this->assertEquals($expected, $result);
  201. $result = $this->Number->currency(1, null, array('wholeSymbol' => ' $', 'wholePosition' => 'after'));
  202. $expected = '1.00 $';
  203. $this->assertEquals($expected, $result);
  204. $options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => ' cents');
  205. $result = $this->Number->currency(0.2, null, $options);
  206. $expected = '20 cents';
  207. $this->assertEquals($expected, $result);
  208. $options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => 'cents ',
  209. 'fractionPosition' => 'before');
  210. $result = $this->Number->currency(0.2, null, $options);
  211. $expected = 'cents 20';
  212. $this->assertEquals($expected, $result);
  213. $result = $this->Number->currency(311, 'USD', array('wholePosition' => 'after'));
  214. $expected = '311.00$';
  215. $this->assertEquals($expected, $result);
  216. $result = $this->Number->currency(0.2, 'EUR');
  217. $expected = '€0,20';
  218. $this->assertEquals($expected, $result);
  219. $options = array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents',
  220. 'fractionPosition' => 'after');
  221. $result = $this->Number->currency(12, null, $options);
  222. $expected = '12.00 dollars';
  223. $this->assertEquals($expected, $result);
  224. $options = array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents',
  225. 'fractionPosition' => 'after');
  226. $result = $this->Number->currency(0.12, null, $options);
  227. $expected = '12 cents';
  228. $this->assertEquals($expected, $result);
  229. $options = array('fractionSymbol' => false, 'fractionPosition' => 'before', 'wholeSymbol' => '$');
  230. $result = $this->Number->currency(0.5, null, $options);
  231. $expected = '$0.50';
  232. $this->assertEquals($expected, $result);
  233. $result = $this->Number->currency(0, 'GBP');
  234. $expected = '£0.00';
  235. $this->assertEquals($expected, $result);
  236. $result = $this->Number->currency(0.00000, 'GBP');
  237. $expected = '£0.00';
  238. $this->assertEquals($expected, $result);
  239. $result = $this->Number->currency('0.00000', 'GBP');
  240. $expected = '£0.00';
  241. $this->assertEquals($expected, $result);
  242. $result = $this->Number->currency('-2.23300', 'JPY');
  243. $expected = '(¥2.23)';
  244. $this->assertEquals($expected, $result);
  245. $result = $this->Number->currency('22.389', 'CAD');
  246. $expected = '$22.39';
  247. $this->assertEquals($expected, $result);
  248. $result = $this->Number->currency('4.111', 'AUD');
  249. $expected = '$4.11';
  250. $this->assertEquals($expected, $result);
  251. }
  252. /**
  253. * Test currency format with places and fraction exponents.
  254. * Places should only matter for non fraction values and vice versa.
  255. *
  256. * @return void
  257. */
  258. public function testCurrencyWithFractionAndPlaces() {
  259. $result = $this->Number->currency('1.23', 'GBP', array('places' => 3));
  260. $expected = '£1.230';
  261. $this->assertEquals($expected, $result);
  262. $result = $this->Number->currency('0.23', 'GBP', array('places' => 3));
  263. $expected = '23p';
  264. $this->assertEquals($expected, $result);
  265. $result = $this->Number->currency('0.001', 'GBP', array('places' => 3));
  266. $expected = '0p';
  267. $this->assertEquals($expected, $result);
  268. $this->Number->addFormat('BHD', array('before' => 'BD ', 'fractionSymbol' => ' fils',
  269. 'fractionExponent' => 3));
  270. $result = $this->Number->currency('1.234', 'BHD', array('places' => 2));
  271. $expected = 'BD 1.23';
  272. $this->assertEquals($expected, $result);
  273. $result = $this->Number->currency('0.234', 'BHD', array('places' => 2));
  274. $expected = '234 fils';
  275. $this->assertEquals($expected, $result);
  276. $result = $this->Number->currency('0.001', 'BHD', array('places' => 2));
  277. $expected = '1 fils';
  278. $this->assertEquals($expected, $result);
  279. }
  280. /**
  281. * Test that the default fraction handling does not cause issues.
  282. *
  283. * @return void
  284. */
  285. public function testCurrencyFractionSymbol() {
  286. $result = $this->Number->currency(0.2, '', array(
  287. 'places' => 2,
  288. 'decimal' => '.'
  289. ));
  290. $this->assertEquals('0.2', $result);
  291. }
  292. /**
  293. * Test adding currency format options to the number helper
  294. *
  295. * @return void
  296. */
  297. public function testCurrencyAddFormat() {
  298. $this->Number->addFormat('NOK', array('before' => 'Kr. '));
  299. $result = $this->Number->currency(1000, 'NOK');
  300. $expected = 'Kr. 1,000.00';
  301. $this->assertEquals($expected, $result);
  302. $this->Number->addFormat('Other', array('before' => '$$ ', 'after' => 'c!'));
  303. $result = $this->Number->currency(0.22, 'Other');
  304. $expected = '22c!';
  305. $this->assertEquals($expected, $result);
  306. $result = $this->Number->currency(-10, 'Other');
  307. $expected = '($$ 10.00)';
  308. $this->assertEquals($expected, $result);
  309. $this->Number->addFormat('Other2', array('before' => '$ ', 'after' => false));
  310. $result = $this->Number->currency(0.22, 'Other2');
  311. $expected = '$ 0.22';
  312. $this->assertEquals($expected, $result);
  313. }
  314. /**
  315. * Test default currency
  316. *
  317. * @return void
  318. */
  319. public function testDefaultCurrency() {
  320. $result = $this->Number->defaultCurrency();
  321. $this->assertEquals('USD', $result);
  322. $this->Number->addFormat('NOK', array('before' => 'Kr. '));
  323. $this->Number->defaultCurrency('NOK');
  324. $result = $this->Number->defaultCurrency();
  325. $this->assertEquals('NOK', $result);
  326. $result = $this->Number->currency(1000);
  327. $expected = 'Kr. 1,000.00';
  328. $this->assertEquals($expected, $result);
  329. $result = $this->Number->currency(2000);
  330. $expected = 'Kr. 2,000.00';
  331. $this->assertEquals($expected, $result);
  332. $this->Number->defaultCurrency('EUR');
  333. $result = $this->Number->currency(1000);
  334. $expected = '€1.000,00';
  335. $this->assertEquals($expected, $result);
  336. $result = $this->Number->currency(2000);
  337. $expected = '€2.000,00';
  338. $this->assertEquals($expected, $result);
  339. $this->Number->defaultCurrency('USD');
  340. }
  341. /**
  342. * testCurrencyPositive method
  343. *
  344. * @return void
  345. */
  346. public function testCurrencyPositive() {
  347. $value = '100100100';
  348. $result = $this->Number->currency($value);
  349. $expected = '$100,100,100.00';
  350. $this->assertEquals($expected, $result);
  351. $result = $this->Number->currency($value, 'USD', array('before' => '#'));
  352. $expected = '#100,100,100.00';
  353. $this->assertEquals($expected, $result);
  354. $result = $this->Number->currency($value, false);
  355. $expected = '100,100,100.00';
  356. $this->assertEquals($expected, $result);
  357. $result = $this->Number->currency($value, 'USD');
  358. $expected = '$100,100,100.00';
  359. $this->assertEquals($expected, $result);
  360. $result = $this->Number->currency($value, 'EUR');
  361. $expected = '€100.100.100,00';
  362. $this->assertEquals($expected, $result);
  363. $result = $this->Number->currency($value, 'GBP');
  364. $expected = '£100,100,100.00';
  365. $this->assertEquals($expected, $result);
  366. }
  367. /**
  368. * testCurrencyNegative method
  369. *
  370. * @return void
  371. */
  372. public function testCurrencyNegative() {
  373. $value = '-100100100';
  374. $result = $this->Number->currency($value);
  375. $expected = '($100,100,100.00)';
  376. $this->assertEquals($expected, $result);
  377. $result = $this->Number->currency($value, 'EUR');
  378. $expected = '(€100.100.100,00)';
  379. $this->assertEquals($expected, $result);
  380. $result = $this->Number->currency($value, 'GBP');
  381. $expected = '(£100,100,100.00)';
  382. $this->assertEquals($expected, $result);
  383. $result = $this->Number->currency($value, 'USD', array('negative' => '-'));
  384. $expected = '-$100,100,100.00';
  385. $this->assertEquals($expected, $result);
  386. $result = $this->Number->currency($value, 'EUR', array('negative' => '-'));
  387. $expected = '-€100.100.100,00';
  388. $this->assertEquals($expected, $result);
  389. $result = $this->Number->currency($value, 'GBP', array('negative' => '-'));
  390. $expected = '-£100,100,100.00';
  391. $this->assertEquals($expected, $result);
  392. }
  393. /**
  394. * testCurrencyCentsPositive method
  395. *
  396. * @return void
  397. */
  398. public function testCurrencyCentsPositive() {
  399. $value = '0.99';
  400. $result = $this->Number->currency($value, 'USD');
  401. $expected = '99c';
  402. $this->assertEquals($expected, $result);
  403. $result = $this->Number->currency($value, 'EUR');
  404. $expected = '€0,99';
  405. $this->assertEquals($expected, $result);
  406. $result = $this->Number->currency($value, 'GBP');
  407. $expected = '99p';
  408. $this->assertEquals($expected, $result);
  409. }
  410. /**
  411. * testCurrencyCentsNegative method
  412. *
  413. * @return void
  414. */
  415. public function testCurrencyCentsNegative() {
  416. $value = '-0.99';
  417. $result = $this->Number->currency($value, 'USD');
  418. $expected = '(99c)';
  419. $this->assertEquals($expected, $result);
  420. $result = $this->Number->currency($value, 'EUR');
  421. $expected = '(€0,99)';
  422. $this->assertEquals($expected, $result);
  423. $result = $this->Number->currency($value, 'GBP');
  424. $expected = '(99p)';
  425. $this->assertEquals($expected, $result);
  426. $result = $this->Number->currency($value, 'USD', array('negative' => '-'));
  427. $expected = '-99c';
  428. $this->assertEquals($expected, $result);
  429. $result = $this->Number->currency($value, 'EUR', array('negative' => '-'));
  430. $expected = '-€0,99';
  431. $this->assertEquals($expected, $result);
  432. $result = $this->Number->currency($value, 'GBP', array('negative' => '-'));
  433. $expected = '-99p';
  434. $this->assertEquals($expected, $result);
  435. }
  436. /**
  437. * testCurrencyZero method
  438. *
  439. * @return void
  440. */
  441. public function testCurrencyZero() {
  442. $value = '0';
  443. $result = $this->Number->currency($value, 'USD');
  444. $expected = '$0.00';
  445. $this->assertEquals($expected, $result);
  446. $result = $this->Number->currency($value, 'EUR');
  447. $expected = '€0,00';
  448. $this->assertEquals($expected, $result);
  449. $result = $this->Number->currency($value, 'GBP');
  450. $expected = '£0.00';
  451. $this->assertEquals($expected, $result);
  452. $result = $this->Number->currency($value, 'GBP', array('zero' => 'FREE!'));
  453. $expected = 'FREE!';
  454. $this->assertEquals($expected, $result);
  455. }
  456. /**
  457. * testCurrencyOptions method
  458. *
  459. * @return void
  460. */
  461. public function testCurrencyOptions() {
  462. $value = '1234567.89';
  463. $result = $this->Number->currency($value, null, array('before' => 'GBP'));
  464. $expected = 'GBP1,234,567.89';
  465. $this->assertEquals($expected, $result);
  466. $result = $this->Number->currency($value, 'GBP', array('places' => 0));
  467. $expected = '£1,234,568';
  468. $this->assertEquals($expected, $result);
  469. $result = $this->Number->currency('1234567.8912345', null, array('before' => 'GBP', 'places' => 3));
  470. $expected = 'GBP1,234,567.891';
  471. $this->assertEquals($expected, $result);
  472. $result = $this->Number->currency('650.120001', null, array('before' => 'GBP', 'places' => 4));
  473. $expected = 'GBP650.1200';
  474. $this->assertEquals($expected, $result);
  475. $result = $this->Number->currency($value, 'GBP', array('before' => '&#163; ', 'escape' => true));
  476. $expected = '&amp;#163; 1,234,567.89';
  477. $this->assertEquals($expected, $result);
  478. $result = $this->Number->currency('0.35', 'USD', array('after' => false));
  479. $expected = '$0.35';
  480. $this->assertEquals($expected, $result);
  481. $result = $this->Number->currency('0.35', 'GBP', array('before' => '&#163;', 'after' => false, 'escape' => false));
  482. $expected = '&#163;0.35';
  483. $this->assertEquals($expected, $result);
  484. $result = $this->Number->currency('0.35', 'GBP');
  485. $expected = '35p';
  486. $this->assertEquals($expected, $result);
  487. $result = $this->Number->currency('0.35', 'EUR');
  488. $expected = '€0,35';
  489. $this->assertEquals($expected, $result);
  490. }
  491. /**
  492. * testToReadableSize method
  493. *
  494. * @return void
  495. */
  496. public function testToReadableSize() {
  497. $result = $this->Number->toReadableSize(0);
  498. $expected = '0 Bytes';
  499. $this->assertEquals($expected, $result);
  500. $result = $this->Number->toReadableSize(1);
  501. $expected = '1 Byte';
  502. $this->assertEquals($expected, $result);
  503. $result = $this->Number->toReadableSize(45);
  504. $expected = '45 Bytes';
  505. $this->assertEquals($expected, $result);
  506. $result = $this->Number->toReadableSize(1023);
  507. $expected = '1,023 Bytes';
  508. $this->assertEquals($expected, $result);
  509. $result = $this->Number->toReadableSize(1024);
  510. $expected = '1 KB';
  511. $this->assertEquals($expected, $result);
  512. $result = $this->Number->toReadableSize(1024 + 123);
  513. $expected = '1.12 KB';
  514. $this->assertEquals($expected, $result);
  515. $result = $this->Number->toReadableSize(1024 * 512);
  516. $expected = '512 KB';
  517. $this->assertEquals($expected, $result);
  518. $result = $this->Number->toReadableSize(1024 * 1024 - 1);
  519. $expected = '1 MB';
  520. $this->assertEquals($expected, $result);
  521. $result = $this->Number->toReadableSize(512.05 * 1024 * 1024);
  522. $expected = '512.05 MB';
  523. $this->assertEquals($expected, $result);
  524. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 - 1);
  525. $expected = '1 GB';
  526. $this->assertEquals($expected, $result);
  527. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 512);
  528. $expected = '512 GB';
  529. $this->assertEquals($expected, $result);
  530. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 - 1);
  531. $expected = '1 TB';
  532. $this->assertEquals($expected, $result);
  533. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 512);
  534. $expected = '512 TB';
  535. $this->assertEquals($expected, $result);
  536. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 1024 - 1);
  537. $expected = '1,024 TB';
  538. $this->assertEquals($expected, $result);
  539. $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 1024 * 1024);
  540. $expected = '1,048,576 TB';
  541. $this->assertEquals($expected, $result);
  542. }
  543. /**
  544. * test toReadableSize() with locales
  545. *
  546. * @return void
  547. */
  548. public function testReadableSizeLocalized() {
  549. ini_set('intl.default_locale', 'fr_FR');
  550. $result = $this->Number->toReadableSize(1321205);
  551. $this->assertEquals('1,26 MB', $result);
  552. $result = $this->Number->toReadableSize(512.05 * 1024 * 1024 * 1024);
  553. $this->assertEquals('512,05 GB', $result);
  554. }
  555. /**
  556. * test precision() with locales
  557. *
  558. * @return void
  559. */
  560. public function testPrecisionLocalized() {
  561. ini_set('intl.default_locale', 'fr_FR');
  562. $result = $this->Number->precision(1.234);
  563. $this->assertEquals('1,234', $result);
  564. }
  565. /**
  566. * testToPercentage method
  567. *
  568. * @return void
  569. */
  570. public function testToPercentage() {
  571. $result = $this->Number->toPercentage(45, 0);
  572. $expected = '45%';
  573. $this->assertEquals($expected, $result);
  574. $result = $this->Number->toPercentage(45, 2);
  575. $expected = '45.00%';
  576. $this->assertEquals($expected, $result);
  577. $result = $this->Number->toPercentage(0, 0);
  578. $expected = '0%';
  579. $this->assertEquals($expected, $result);
  580. $result = $this->Number->toPercentage(0, 4);
  581. $expected = '0.0000%';
  582. $this->assertEquals($expected, $result);
  583. $result = $this->Number->toPercentage(45, 0, array('multiply' => false));
  584. $expected = '45%';
  585. $this->assertEquals($expected, $result);
  586. $result = $this->Number->toPercentage(45, 2, array('multiply' => false));
  587. $expected = '45.00%';
  588. $this->assertEquals($expected, $result);
  589. $result = $this->Number->toPercentage(0, 0, array('multiply' => false));
  590. $expected = '0%';
  591. $this->assertEquals($expected, $result);
  592. $result = $this->Number->toPercentage(0, 4, array('multiply' => false));
  593. $expected = '0.0000%';
  594. $this->assertEquals($expected, $result);
  595. $result = $this->Number->toPercentage(0.456, 0, array('multiply' => true));
  596. $expected = '46%';
  597. $this->assertEquals($expected, $result);
  598. $result = $this->Number->toPercentage(0.456, 2, array('multiply' => true));
  599. $expected = '45.60%';
  600. $this->assertEquals($expected, $result);
  601. }
  602. /**
  603. * testFromReadableSize
  604. *
  605. * @dataProvider filesizes
  606. * @return void
  607. */
  608. public function testFromReadableSize($params, $expected) {
  609. $result = $this->Number->fromReadableSize($params['size'], $params['default']);
  610. $this->assertEquals($expected, $result);
  611. }
  612. /**
  613. * testFromReadableSize
  614. *
  615. * @expectedException \Cake\Error\Exception
  616. * @return void
  617. */
  618. public function testFromReadableSizeException() {
  619. $this->Number->fromReadableSize('bogus', false);
  620. }
  621. /**
  622. * filesizes dataprovider
  623. *
  624. * @return array
  625. */
  626. public function filesizes() {
  627. return array(
  628. array(array('size' => '512B', 'default' => false), 512),
  629. array(array('size' => '1KB', 'default' => false), 1024),
  630. array(array('size' => '1.5KB', 'default' => false), 1536),
  631. array(array('size' => '1MB', 'default' => false), 1048576),
  632. array(array('size' => '1mb', 'default' => false), 1048576),
  633. array(array('size' => '1.5MB', 'default' => false), 1572864),
  634. array(array('size' => '1GB', 'default' => false), 1073741824),
  635. array(array('size' => '1.5GB', 'default' => false), 1610612736),
  636. array(array('size' => '1K', 'default' => false), 1024),
  637. array(array('size' => '1.5K', 'default' => false), 1536),
  638. array(array('size' => '1M', 'default' => false), 1048576),
  639. array(array('size' => '1m', 'default' => false), 1048576),
  640. array(array('size' => '1.5M', 'default' => false), 1572864),
  641. array(array('size' => '1G', 'default' => false), 1073741824),
  642. array(array('size' => '1.5G', 'default' => false), 1610612736),
  643. array(array('size' => '512', 'default' => 'Unknown type'), 512),
  644. array(array('size' => '2VB', 'default' => 'Unknown type'), 'Unknown type')
  645. );
  646. }
  647. }