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