forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberTest.php
More file actions
71 lines (58 loc) · 2.97 KB
/
NumberTest.php
File metadata and controls
71 lines (58 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
use App\Helpers\Number;
test('can convert file size strings to bytes', function () {
// Test basic units
expect(Number::fileSizeToBytes('100 B'))->toBe(100);
expect(Number::fileSizeToBytes('1 KB'))->toBe(1000);
expect(Number::fileSizeToBytes('1 MB'))->toBe(1000000);
expect(Number::fileSizeToBytes('1 GB'))->toBe(1000000000);
expect(Number::fileSizeToBytes('1 TB'))->toBe(1000000000000);
expect(Number::fileSizeToBytes('1 PB'))->toBe(1000000000000000);
});
test('can convert file size strings with decimals to bytes', function () {
expect(Number::fileSizeToBytes('1.5 KB'))->toBe(1500);
expect(Number::fileSizeToBytes('2.5 MB'))->toBe(2500000);
expect(Number::fileSizeToBytes('0.5 GB'))->toBe(500000000);
expect(Number::fileSizeToBytes('1.25 TB'))->toBe(1250000000000);
});
test('handles case insensitive units', function () {
expect(Number::fileSizeToBytes('100 mb'))->toBe(100000000);
expect(Number::fileSizeToBytes('1 Gb'))->toBe(1000000000);
expect(Number::fileSizeToBytes('500 KB'))->toBe(500000);
expect(Number::fileSizeToBytes('2 tb'))->toBe(2000000000000);
});
test('handles whitespace variations', function () {
expect(Number::fileSizeToBytes('100MB'))->toBe(100000000);
expect(Number::fileSizeToBytes(' 1 GB '))->toBe(1000000000);
expect(Number::fileSizeToBytes('500 KB'))->toBe(500000);
});
test('handles large units correctly', function () {
// Use string comparison for very large numbers to avoid PHP integer overflow
expect(Number::fileSizeToBytes('1 EB'))->toBe(1000000000000000000);
// For ZB and YB, we'll test smaller values due to PHP integer limits
expect(Number::fileSizeToBytes('1 PB'))->toBe(1000000000000000);
});
test('throws exception for invalid format', function () {
expect(fn() => Number::fileSizeToBytes('invalid'))
->toThrow(InvalidArgumentException::class, 'Invalid file size format: invalid');
expect(fn() => Number::fileSizeToBytes('100'))
->toThrow(InvalidArgumentException::class, 'Invalid file size format: 100');
expect(fn() => Number::fileSizeToBytes('MB'))
->toThrow(InvalidArgumentException::class, 'Invalid file size format: MB');
// XB is not a valid unit according to our regex pattern
expect(fn() => Number::fileSizeToBytes('100 XB'))
->toThrow(InvalidArgumentException::class, 'Invalid file size format: 100 XB');
});
test('handles edge cases', function () {
expect(Number::fileSizeToBytes('0 B'))->toBe(0);
expect(Number::fileSizeToBytes('0 MB'))->toBe(0);
expect(Number::fileSizeToBytes('1 B'))->toBe(1);
});
test('handles realistic file sizes', function () {
// Common file sizes
expect(Number::fileSizeToBytes('5 MB'))->toBe(5000000);
expect(Number::fileSizeToBytes('100 MB'))->toBe(100000000);
expect(Number::fileSizeToBytes('4.7 GB'))->toBe(4700000000);
expect(Number::fileSizeToBytes('25 GB'))->toBe(25000000000);
expect(Number::fileSizeToBytes('1.5 TB'))->toBe(1500000000000);
});