統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Utility / CakeNumberTest.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (23.377 KB)

1
<?php
2
/**
3
 * CakeNumberTest 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
 * @package       Cake.Test.Case.View.Helper
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18

    
19
App::uses('View', 'View');
20
App::uses('CakeNumber', 'Utility');
21

    
22
/**
23
 * CakeNumberTest class
24
 *
25
 * @package       Cake.Test.Case.Utility
26
 */
27
class CakeNumberTest extends CakeTestCase {
28

    
29
/**
30
 * setUp method
31
 *
32
 * @return void
33
 */
34
        public function setUp() {
35
                parent::setUp();
36
                $this->Number = new CakeNumber();
37
        }
38

    
39
/**
40
 * tearDown method
41
 *
42
 * @return void
43
 */
44
        public function tearDown() {
45
                parent::tearDown();
46
                unset($this->Number);
47
        }
48

    
49
/**
50
 * testFormatAndCurrency method
51
 *
52
 * @return void
53
 */
54
        public function testFormat() {
55
                $value = '100100100';
56

    
57
                $result = $this->Number->format($value, '#');
58
                $expected = '#100,100,100';
59
                $this->assertEquals($expected, $result);
60

    
61
                $result = $this->Number->format($value, 3);
62
                $expected = '100,100,100.000';
63
                $this->assertEquals($expected, $result);
64

    
65
                $result = $this->Number->format($value);
66
                $expected = '100,100,100';
67
                $this->assertEquals($expected, $result);
68

    
69
                $result = $this->Number->format($value, '-');
70
                $expected = '100-100-100';
71
                $this->assertEquals($expected, $result);
72

    
73
                $value = 0.00001;
74
                $result = $this->Number->format($value, array('places' => 1));
75
                $expected = '$0.0';
76
                $this->assertEquals($expected, $result);
77

    
78
                $value = -0.00001;
79
                $result = $this->Number->format($value, array('places' => 1));
80
                $expected = '$0.0';
81
                $this->assertEquals($expected, $result);
82

    
83
                $value = 1.23;
84
                $options = array('decimals' => ',', 'thousands' => '.', 'before' => '', 'after' => '');
85
                $result = $this->Number->format($value, $options);
86
                $expected = '1,23 €';
87
                $this->assertEquals($expected, $result);
88
        }
89

    
90
/**
91
 * testFormatDelta method
92
 *
93
 * @return void
94
 */
95
        public function testFormatDelta() {
96
                $value = '100100100';
97

    
98
                $result = $this->Number->formatDelta($value);
99
                $expected = '+100,100,100.00';
100
                $this->assertEquals($expected, $result);
101

    
102
                $result = $this->Number->formatDelta($value, array('before' => '', 'after' => ''));
103
                $expected = '+100,100,100.00';
104
                $this->assertEquals($expected, $result);
105

    
106
                $result = $this->Number->formatDelta($value, array('before' => '[', 'after' => ']'));
107
                $expected = '[+100,100,100.00]';
108
                $this->assertEquals($expected, $result);
109

    
110
                $result = $this->Number->formatDelta(-$value, array('before' => '[', 'after' => ']'));
111
                $expected = '[-100,100,100.00]';
112
                $this->assertEquals($expected, $result);
113

    
114
                $result = $this->Number->formatDelta(-$value, array('before' => '[ ', 'after' => ' ]'));
115
                $expected = '[ -100,100,100.00 ]';
116
                $this->assertEquals($expected, $result);
117

    
118
                $value = 0;
119
                $result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']'));
120
                $expected = '[0.0]';
121
                $this->assertEquals($expected, $result);
122

    
123
                $value = 0.0001;
124
                $result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']'));
125
                $expected = '[0.0]';
126
                $this->assertEquals($expected, $result);
127

    
128
                $value = 9876.1234;
129
                $result = $this->Number->formatDelta($value, array('places' => 1, 'decimals' => ',', 'thousands' => '.'));
130
                $expected = '+9.876,1';
131
                $this->assertEquals($expected, $result);
132
        }
133

    
134
/**
135
 * testMultibyteFormat
136
 *
137
 * @return void
138
 */
139
        public function testMultibyteFormat() {
140
                $value = '5199100.0006';
141
                $result = $this->Number->format($value, array(
142
                        'thousands' => '&nbsp;',
143
                        'decimals' => '&amp;',
144
                        'places' => 3,
145
                        'escape' => false,
146
                        'before' => '',
147
                ));
148
                $expected = '5&nbsp;199&nbsp;100&amp;001';
149
                $this->assertEquals($expected, $result);
150

    
151
                $value = 1000.45;
152
                $result = $this->Number->format($value, array(
153
                        'thousands' => ',,',
154
                        'decimals' => '.a',
155
                        'escape' => false,
156
                ));
157
                $expected = '$1,,000.a45';
158
                $this->assertEquals($expected, $result);
159

    
160
                $value = 519919827593784.00;
161
                $this->Number->addFormat('RUR', array(
162
                        'thousands' => 'ø€ƒ‡™',
163
                        'decimals' => '(§.§)',
164
                        'escape' => false,
165
                        'wholeSymbol' => '',
166
                        'wholePosition' => 'after',
167
                ));
168
                $result = $this->Number->currency($value, 'RUR');
169
                $expected = '519ø€ƒ‡™919ø€ƒ‡™827ø€ƒ‡™593ø€ƒ‡™784(§.§)00€';
170
                $this->assertEquals($expected, $result);
171

    
172
                $value = '13371337.1337';
173
                $result = CakeNumber::format($value, array(
174
                        'thousands' => '- |-| /-\ >< () |2 -',
175
                        'decimals' => '- £€€† -',
176
                        'before' => ''
177
                ));
178
                $expected = '13- |-| /-\ &gt;&lt; () |2 -371- |-| /-\ &gt;&lt; () |2 -337- £€€† -13';
179
                $this->assertEquals($expected, $result);
180
        }
181

    
182
/**
183
 * Test currency method.
184
 *
185
 * @return void
186
 */
187
        public function testCurrency() {
188
                $value = '100100100';
189

    
190
                $result = $this->Number->currency($value);
191
                $expected = '$100,100,100.00';
192
                $this->assertEquals($expected, $result);
193

    
194
                $result = $this->Number->currency($value, '#');
195
                $expected = '#100,100,100.00';
196
                $this->assertEquals($expected, $result);
197

    
198
                $result = $this->Number->currency($value, false);
199
                $expected = '100,100,100.00';
200
                $this->assertEquals($expected, $result);
201

    
202
                $result = $this->Number->currency($value, 'USD');
203
                $expected = '$100,100,100.00';
204
                $this->assertEquals($expected, $result);
205

    
206
                $result = $this->Number->currency($value, 'EUR');
207
                $expected = '€100.100.100,00';
208
                $this->assertEquals($expected, $result);
209

    
210
                $result = $this->Number->currency($value, 'GBP');
211
                $expected = '£100,100,100.00';
212
                $this->assertEquals($expected, $result);
213

    
214
                $options = array('thousands' => ' ', 'wholeSymbol' => 'EUR ', 'wholePosition' => 'before',
215
                        'decimals' => ',', 'zero' => 'Gratuit');
216
                $result = $this->Number->currency($value, '', $options);
217
                $expected = 'EUR 100 100 100,00';
218
                $this->assertEquals($expected, $result);
219

    
220
                $options = array('after' => 'øre', 'before' => 'Kr.', 'decimals' => ',', 'thousands' => '.');
221
                $result = $this->Number->currency(1000.45, null, $options);
222
                $expected = 'Kr.1.000,45';
223
                $this->assertEquals($expected, $result);
224

    
225
                $result = $this->Number->currency(0.5, 'USD');
226
                $expected = '50c';
227
                $this->assertEquals($expected, $result);
228

    
229
                $result = $this->Number->currency(0.5, null, array('after' => 'øre'));
230
                $expected = '50øre';
231
                $this->assertEquals($expected, $result);
232

    
233
                $result = $this->Number->currency(1, null, array('wholeSymbol' => '$ '));
234
                $expected = '$ 1.00';
235
                $this->assertEquals($expected, $result);
236

    
237
                $result = $this->Number->currency(1, null, array('wholeSymbol' => ' $', 'wholePosition' => 'after'));
238
                $expected = '1.00 $';
239
                $this->assertEquals($expected, $result);
240

    
241
                $options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => ' cents');
242
                $result = $this->Number->currency(0.2, null, $options);
243
                $expected = '20 cents';
244
                $this->assertEquals($expected, $result);
245

    
246
                $options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => 'cents ',
247
                        'fractionPosition' => 'before');
248
                $result = $this->Number->currency(0.2, null, $options);
249
                $expected = 'cents 20';
250
                $this->assertEquals($expected, $result);
251

    
252
                $result = $this->Number->currency(311, 'USD', array('wholePosition' => 'after'));
253
                $expected = '311.00$';
254
                $this->assertEquals($expected, $result);
255

    
256
                $result = $this->Number->currency(0.2, 'EUR');
257
                $expected = '€0,20';
258
                $this->assertEquals($expected, $result);
259

    
260
                $options = array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents',
261
                        'fractionPosition' => 'after');
262
                $result = $this->Number->currency(12, null, $options);
263
                $expected = '12.00 dollars';
264
                $this->assertEquals($expected, $result);
265

    
266
                $options = array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents',
267
                        'fractionPosition' => 'after');
268
                $result = $this->Number->currency(0.12, null, $options);
269
                $expected = '12 cents';
270
                $this->assertEquals($expected, $result);
271

    
272
                $options = array('fractionSymbol' => false, 'fractionPosition' => 'before', 'wholeSymbol' => '$');
273
                $result = $this->Number->currency(0.5, null, $options);
274
                $expected = '$0.50';
275
                $this->assertEquals($expected, $result);
276

    
277
                $result = $this->Number->currency(0, 'GBP');
278
                $expected = '£0.00';
279
                $this->assertEquals($expected, $result);
280

    
281
                $result = $this->Number->currency(0.00000, 'GBP');
282
                $expected = '£0.00';
283
                $this->assertEquals($expected, $result);
284

    
285
                $result = $this->Number->currency('0.00000', 'GBP');
286
                $expected = '£0.00';
287
                $this->assertEquals($expected, $result);
288

    
289
                $result = $this->Number->currency('-2.23300', 'JPY');
290
                $expected = '(¥2.23)';
291
                $this->assertEquals($expected, $result);
292

    
293
                $result = $this->Number->currency('22.389', 'CAD');
294
                $expected = '$22.39';
295
                $this->assertEquals($expected, $result);
296

    
297
                $result = $this->Number->currency('4.111', 'AUD');
298
                $expected = '$4.11';
299
                $this->assertEquals($expected, $result);
300
        }
301

    
302
/**
303
 * Test currency format with places and fraction exponents.
304
 * Places should only matter for non fraction values and vice versa.
305
 *
306
 * @return void
307
 */
308
        public function testCurrencyWithFractionAndPlaces() {
309
                $result = $this->Number->currency('1.23', 'GBP', array('places' => 3));
310
                $expected = '£1.230';
311
                $this->assertEquals($expected, $result);
312

    
313
                $result = $this->Number->currency('0.23', 'GBP', array('places' => 3));
314
                $expected = '23p';
315
                $this->assertEquals($expected, $result);
316

    
317
                $result = $this->Number->currency('0.001', 'GBP', array('places' => 3));
318
                $expected = '0p';
319
                $this->assertEquals($expected, $result);
320

    
321
                $this->Number->addFormat('BHD', array('before' => 'BD ', 'fractionSymbol' => ' fils',
322
                        'fractionExponent' => 3));
323
                $result = $this->Number->currency('1.234', 'BHD', array('places' => 2));
324
                $expected = 'BD 1.23';
325
                $this->assertEquals($expected, $result);
326

    
327
                $result = $this->Number->currency('0.234', 'BHD', array('places' => 2));
328
                $expected = '234 fils';
329
                $this->assertEquals($expected, $result);
330

    
331
                $result = $this->Number->currency('0.001', 'BHD', array('places' => 2));
332
                $expected = '1 fils';
333
                $this->assertEquals($expected, $result);
334
        }
335

    
336
/**
337
 * Test that the default fraction handling does not cause issues.
338
 *
339
 * @return void
340
 */
341
        public function testCurrencyFractionSymbol() {
342
                $result = $this->Number->currency(0.2, '', array(
343
                        'places' => 2,
344
                        'decimal' => '.'
345
                ));
346
                $this->assertEquals('0.2', $result);
347
        }
348

    
349
/**
350
 * Test adding currency format options to the number helper
351
 *
352
 * @return void
353
 */
354
        public function testCurrencyAddFormat() {
355
                $this->Number->addFormat('NOK', array('before' => 'Kr. '));
356
                $result = $this->Number->currency(1000, 'NOK');
357
                $expected = 'Kr. 1,000.00';
358
                $this->assertEquals($expected, $result);
359

    
360
                $this->Number->addFormat('Other', array('before' => '$$ ', 'after' => 'c!'));
361
                $result = $this->Number->currency(0.22, 'Other');
362
                $expected = '22c!';
363
                $this->assertEquals($expected, $result);
364

    
365
                $result = $this->Number->currency(-10, 'Other');
366
                $expected = '($$ 10.00)';
367
                $this->assertEquals($expected, $result);
368

    
369
                $this->Number->addFormat('Other2', array('before' => '$ ', 'after' => false));
370
                $result = $this->Number->currency(0.22, 'Other2');
371
                $expected = '$ 0.22';
372
                $this->assertEquals($expected, $result);
373
        }
374

    
375
/**
376
 * Test default currency
377
 *
378
 * @return void
379
 */
380
        public function testDefaultCurrency() {
381
                $result = $this->Number->defaultCurrency();
382
                $this->assertEquals('USD', $result);
383
                $this->Number->addFormat('NOK', array('before' => 'Kr. '));
384

    
385
                $this->Number->defaultCurrency('NOK');
386
                $result = $this->Number->defaultCurrency();
387
                $this->assertEquals('NOK', $result);
388

    
389
                $result = $this->Number->currency(1000);
390
                $expected = 'Kr. 1,000.00';
391
                $this->assertEquals($expected, $result);
392

    
393
                $result = $this->Number->currency(2000);
394
                $expected = 'Kr. 2,000.00';
395
                $this->assertEquals($expected, $result);
396
                $this->Number->defaultCurrency('EUR');
397
                $result = $this->Number->currency(1000);
398
                $expected = '€1.000,00';
399
                $this->assertEquals($expected, $result);
400

    
401
                $result = $this->Number->currency(2000);
402
                $expected = '€2.000,00';
403
                $this->assertEquals($expected, $result);
404

    
405
                $this->Number->defaultCurrency('USD');
406
        }
407

    
408
/**
409
 * testCurrencyPositive method
410
 *
411
 * @return void
412
 */
413
        public function testCurrencyPositive() {
414
                $value = '100100100';
415

    
416
                $result = $this->Number->currency($value);
417
                $expected = '$100,100,100.00';
418
                $this->assertEquals($expected, $result);
419

    
420
                $result = $this->Number->currency($value, 'USD', array('before' => '#'));
421
                $expected = '#100,100,100.00';
422
                $this->assertEquals($expected, $result);
423

    
424
                $result = $this->Number->currency($value, false);
425
                $expected = '100,100,100.00';
426
                $this->assertEquals($expected, $result);
427

    
428
                $result = $this->Number->currency($value, 'USD');
429
                $expected = '$100,100,100.00';
430
                $this->assertEquals($expected, $result);
431

    
432
                $result = $this->Number->currency($value, 'EUR');
433
                $expected = '€100.100.100,00';
434
                $this->assertEquals($expected, $result);
435

    
436
                $result = $this->Number->currency($value, 'GBP');
437
                $expected = '£100,100,100.00';
438
                $this->assertEquals($expected, $result);
439
        }
440

    
441
/**
442
 * testCurrencyNegative method
443
 *
444
 * @return void
445
 */
446
        public function testCurrencyNegative() {
447
                $value = '-100100100';
448

    
449
                $result = $this->Number->currency($value);
450
                $expected = '($100,100,100.00)';
451
                $this->assertEquals($expected, $result);
452

    
453
                $result = $this->Number->currency($value, 'EUR');
454
                $expected = '(€100.100.100,00)';
455
                $this->assertEquals($expected, $result);
456

    
457
                $result = $this->Number->currency($value, 'GBP');
458
                $expected = '(£100,100,100.00)';
459
                $this->assertEquals($expected, $result);
460

    
461
                $result = $this->Number->currency($value, 'USD', array('negative' => '-'));
462
                $expected = '-$100,100,100.00';
463
                $this->assertEquals($expected, $result);
464

    
465
                $result = $this->Number->currency($value, 'EUR', array('negative' => '-'));
466
                $expected = '-€100.100.100,00';
467
                $this->assertEquals($expected, $result);
468

    
469
                $result = $this->Number->currency($value, 'GBP', array('negative' => '-'));
470
                $expected = '-£100,100,100.00';
471
                $this->assertEquals($expected, $result);
472
        }
473

    
474
/**
475
 * testCurrencyCentsPositive method
476
 *
477
 * @return void
478
 */
479
        public function testCurrencyCentsPositive() {
480
                $value = '0.99';
481

    
482
                $result = $this->Number->currency($value, 'USD');
483
                $expected = '99c';
484
                $this->assertEquals($expected, $result);
485

    
486
                $result = $this->Number->currency($value, 'EUR');
487
                $expected = '€0,99';
488
                $this->assertEquals($expected, $result);
489

    
490
                $result = $this->Number->currency($value, 'GBP');
491
                $expected = '99p';
492
                $this->assertEquals($expected, $result);
493
        }
494

    
495
/**
496
 * testCurrencyCentsNegative method
497
 *
498
 * @return void
499
 */
500
        public function testCurrencyCentsNegative() {
501
                $value = '-0.99';
502

    
503
                $result = $this->Number->currency($value, 'USD');
504
                $expected = '(99c)';
505
                $this->assertEquals($expected, $result);
506

    
507
                $result = $this->Number->currency($value, 'EUR');
508
                $expected = '(€0,99)';
509
                $this->assertEquals($expected, $result);
510

    
511
                $result = $this->Number->currency($value, 'GBP');
512
                $expected = '(99p)';
513
                $this->assertEquals($expected, $result);
514

    
515
                $result = $this->Number->currency($value, 'USD', array('negative' => '-'));
516
                $expected = '-99c';
517
                $this->assertEquals($expected, $result);
518

    
519
                $result = $this->Number->currency($value, 'EUR', array('negative' => '-'));
520
                $expected = '-€0,99';
521
                $this->assertEquals($expected, $result);
522

    
523
                $result = $this->Number->currency($value, 'GBP', array('negative' => '-'));
524
                $expected = '-99p';
525
                $this->assertEquals($expected, $result);
526
        }
527

    
528
/**
529
 * testCurrencyZero method
530
 *
531
 * @return void
532
 */
533
        public function testCurrencyZero() {
534
                $value = '0';
535

    
536
                $result = $this->Number->currency($value, 'USD');
537
                $expected = '$0.00';
538
                $this->assertEquals($expected, $result);
539

    
540
                $result = $this->Number->currency($value, 'EUR');
541
                $expected = '€0,00';
542
                $this->assertEquals($expected, $result);
543

    
544
                $result = $this->Number->currency($value, 'GBP');
545
                $expected = '£0.00';
546
                $this->assertEquals($expected, $result);
547

    
548
                $result = $this->Number->currency($value, 'GBP', array('zero' => 'FREE!'));
549
                $expected = 'FREE!';
550
                $this->assertEquals($expected, $result);
551
        }
552

    
553
/**
554
 * testCurrencyOptions method
555
 *
556
 * @return void
557
 */
558
        public function testCurrencyOptions() {
559
                $value = '1234567.89';
560

    
561
                $result = $this->Number->currency($value, null, array('before' => 'GBP'));
562
                $expected = 'GBP1,234,567.89';
563
                $this->assertEquals($expected, $result);
564

    
565
                $result = $this->Number->currency($value, 'GBP', array('places' => 0));
566
                $expected = '£1,234,568';
567
                $this->assertEquals($expected, $result);
568

    
569
                $result = $this->Number->currency('1234567.8912345', null, array('before' => 'GBP', 'places' => 3));
570
                $expected = 'GBP1,234,567.891';
571
                $this->assertEquals($expected, $result);
572

    
573
                $result = $this->Number->currency('650.120001', null, array('before' => 'GBP', 'places' => 4));
574
                $expected = 'GBP650.1200';
575
                $this->assertEquals($expected, $result);
576

    
577
                $result = $this->Number->currency($value, 'GBP', array('before' => '&#163; ', 'escape' => true));
578
                $expected = '&amp;#163; 1,234,567.89';
579
                $this->assertEquals($expected, $result);
580

    
581
                $result = $this->Number->currency('0.35', 'USD', array('after' => false));
582
                $expected = '$0.35';
583
                $this->assertEquals($expected, $result);
584

    
585
                $result = $this->Number->currency('0.35', 'GBP', array('before' => '&#163;', 'after' => false, 'escape' => false));
586
                $expected = '&#163;0.35';
587
                $this->assertEquals($expected, $result);
588

    
589
                $result = $this->Number->currency('0.35', 'GBP');
590
                $expected = '35p';
591
                $this->assertEquals($expected, $result);
592

    
593
                $result = $this->Number->currency('0.35', 'EUR');
594
                $expected = '€0,35';
595
                $this->assertEquals($expected, $result);
596
        }
597

    
598
/**
599
 * testToReadableSize method
600
 *
601
 * @return void
602
 */
603
        public function testToReadableSize() {
604
                $result = $this->Number->toReadableSize(0);
605
                $expected = '0 Bytes';
606
                $this->assertEquals($expected, $result);
607

    
608
                $result = $this->Number->toReadableSize(1);
609
                $expected = '1 Byte';
610
                $this->assertEquals($expected, $result);
611

    
612
                $result = $this->Number->toReadableSize(45);
613
                $expected = '45 Bytes';
614
                $this->assertEquals($expected, $result);
615

    
616
                $result = $this->Number->toReadableSize(1023);
617
                $expected = '1023 Bytes';
618
                $this->assertEquals($expected, $result);
619

    
620
                $result = $this->Number->toReadableSize(1024);
621
                $expected = '1 KB';
622
                $this->assertEquals($expected, $result);
623

    
624
                $result = $this->Number->toReadableSize(1024 * 512);
625
                $expected = '512 KB';
626
                $this->assertEquals($expected, $result);
627

    
628
                $result = $this->Number->toReadableSize(1024 * 1024 - 1);
629
                $expected = '1.00 MB';
630
                $this->assertEquals($expected, $result);
631

    
632
                $result = $this->Number->toReadableSize(1024 * 1024 * 512);
633
                $expected = '512.00 MB';
634
                $this->assertEquals($expected, $result);
635

    
636
                $result = $this->Number->toReadableSize(1024 * 1024 * 1024 - 1);
637
                $expected = '1.00 GB';
638
                $this->assertEquals($expected, $result);
639

    
640
                $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 512);
641
                $expected = '512.00 GB';
642
                $this->assertEquals($expected, $result);
643

    
644
                $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 - 1);
645
                $expected = '1.00 TB';
646
                $this->assertEquals($expected, $result);
647

    
648
                $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 512);
649
                $expected = '512.00 TB';
650
                $this->assertEquals($expected, $result);
651

    
652
                $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 1024 - 1);
653
                $expected = '1024.00 TB';
654
                $this->assertEquals($expected, $result);
655

    
656
                $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 1024 * 1024);
657
                $expected = (1024 * 1024) . '.00 TB';
658
                $this->assertEquals($expected, $result);
659
        }
660

    
661
/**
662
 * test toReadableSize() with locales
663
 *
664
 * @return void
665
 */
666
        public function testReadableSizeLocalized() {
667
                $restore = setlocale(LC_NUMERIC, 0);
668

    
669
                $this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");
670

    
671
                $result = $this->Number->toReadableSize(1321205);
672
                $this->assertEquals('1,26 MB', $result);
673

    
674
                $result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 512);
675
                $this->assertEquals('512,00 GB', $result);
676
                setlocale(LC_NUMERIC, $restore);
677
        }
678

    
679
/**
680
 * test precision() with locales
681
 *
682
 * @return void
683
 */
684
        public function testPrecisionLocalized() {
685
                $restore = setlocale(LC_NUMERIC, 0);
686

    
687
                $this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");
688

    
689
                $result = $this->Number->precision(1.234);
690
                $this->assertEquals('1,234', $result);
691
                setlocale(LC_NUMERIC, $restore);
692
        }
693

    
694
/**
695
 * testToPercentage method
696
 *
697
 * @return void
698
 */
699
        public function testToPercentage() {
700
                $result = $this->Number->toPercentage(45, 0);
701
                $expected = '45%';
702
                $this->assertEquals($expected, $result);
703

    
704
                $result = $this->Number->toPercentage(45, 2);
705
                $expected = '45.00%';
706
                $this->assertEquals($expected, $result);
707

    
708
                $result = $this->Number->toPercentage(0, 0);
709
                $expected = '0%';
710
                $this->assertEquals($expected, $result);
711

    
712
                $result = $this->Number->toPercentage(0, 4);
713
                $expected = '0.0000%';
714
                $this->assertEquals($expected, $result);
715

    
716
                $result = $this->Number->toPercentage(45, 0, array('multiply' => false));
717
                $expected = '45%';
718
                $this->assertEquals($expected, $result);
719

    
720
                $result = $this->Number->toPercentage(45, 2, array('multiply' => false));
721
                $expected = '45.00%';
722
                $this->assertEquals($expected, $result);
723

    
724
                $result = $this->Number->toPercentage(0, 0, array('multiply' => false));
725
                $expected = '0%';
726
                $this->assertEquals($expected, $result);
727

    
728
                $result = $this->Number->toPercentage(0, 4, array('multiply' => false));
729
                $expected = '0.0000%';
730
                $this->assertEquals($expected, $result);
731

    
732
                $result = $this->Number->toPercentage(0.456, 0, array('multiply' => true));
733
                $expected = '46%';
734
                $this->assertEquals($expected, $result);
735

    
736
                $result = $this->Number->toPercentage(0.456, 2, array('multiply' => true));
737
                $expected = '45.60%';
738
                $this->assertEquals($expected, $result);
739
        }
740

    
741
/**
742
 * testFromReadableSize
743
 *
744
 * @dataProvider filesizes
745
 * @return void
746
 */
747
        public function testFromReadableSize($params, $expected) {
748
                $result = $this->Number->fromReadableSize($params['size'], $params['default']);
749
                $this->assertEquals($expected, $result);
750
        }
751

    
752
/**
753
 * testFromReadableSize
754
 *
755
 * @expectedException CakeException
756
 * @return void
757
 */
758
        public function testFromReadableSizeException() {
759
                $this->Number->fromReadableSize('bogus', false);
760
        }
761

    
762
/**
763
 * filesizes dataprovider
764
 *
765
 * @return array
766
 */
767
        public function filesizes() {
768
                return array(
769
                        array(array('size' => '512B', 'default' => false), 512),
770
                        array(array('size' => '1KB', 'default' => false), 1024),
771
                        array(array('size' => '1.5KB', 'default' => false), 1536),
772
                        array(array('size' => '1MB', 'default' => false), 1048576),
773
                        array(array('size' => '1mb', 'default' => false), 1048576),
774
                        array(array('size' => '1.5MB', 'default' => false), 1572864),
775
                        array(array('size' => '1GB', 'default' => false), 1073741824),
776
                        array(array('size' => '1.5GB', 'default' => false), 1610612736),
777
                        array(array('size' => '1K', 'default' => false), 1024),
778
                        array(array('size' => '1.5K', 'default' => false), 1536),
779
                        array(array('size' => '1M', 'default' => false), 1048576),
780
                        array(array('size' => '1m', 'default' => false), 1048576),
781
                        array(array('size' => '1.5M', 'default' => false), 1572864),
782
                        array(array('size' => '1G', 'default' => false), 1073741824),
783
                        array(array('size' => '1.5G', 'default' => false), 1610612736),
784
                        array(array('size' => '512', 'default' => 'Unknown type'), 512),
785
                        array(array('size' => '2VB', 'default' => 'Unknown type'), 'Unknown type')
786
                );
787
        }
788

    
789
}