NumberTest.php 23 KB

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