Skip to content

Commit fc4c594

Browse files
committed
Fix ini bug, add test
1 parent 3c26f88 commit fc4c594

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/Helpers/files_helper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,19 @@ function return_bytes(string $value): int
8080
{
8181
$value = strtolower(trim($value));
8282
$unit = $value[strlen($value) - 1];
83-
$num = (int)rtrim($value, $unit);
83+
$num = (int) rtrim($value, $unit);
8484

8585
switch ($unit)
8686
{
8787
case 'g': $num *= 1024;
8888
case 'm': $num *= 1024;
8989
case 'k': $num *= 1024;
90+
91+
// If it is not one of those modifiers then it was numerical bytes, add the final digit back
92+
default:
93+
$num = (int) ((string) $num . $unit);
9094
}
95+
9196
return $num;
9297
}
9398
}

tests/unit/HelperTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
use Tatter\Files\Entities\File;
4+
use Tests\Support\Fakers\FileFaker;
5+
use Tests\Support\FilesTestCase;
6+
7+
class HelperTest extends FilesTestCase
8+
{
9+
10+
public static function setUpBeforeClass(): void
11+
{
12+
parent::setUpBeforeClass();
13+
14+
helper('files');
15+
}
16+
17+
/**
18+
* @dataProvider bytesProvider
19+
*/
20+
public function testBytesToHuman($bytes, $expected)
21+
{
22+
$this->assertEquals($expected, bytes2human($bytes));
23+
}
24+
25+
public function bytesProvider()
26+
{
27+
return [
28+
[1, '1 bytes'],
29+
[1024, '1024 bytes'],
30+
[1025, '1 KB'],
31+
[1024*1024, '1024 KB'],
32+
[1024*1025, '1 MB'],
33+
[1024*1024*1024, '1024 MB'],
34+
[1024*1024*1025, '1 GB'],
35+
[1024*1024*1024*1024, '1024 GB'],
36+
[1024*1024*1024*1025, '1 TB'],
37+
[1024*1024*1024*1024*1024, '1024 TB'],
38+
[1024*1024*1024*1024*1025, '1 PB'],
39+
];
40+
}
41+
42+
/**
43+
* @dataProvider iniProvider
44+
*/
45+
public function testReturnBytes($ini, $expected)
46+
{
47+
$this->assertEquals($expected, return_bytes($ini));
48+
}
49+
50+
public function iniProvider()
51+
{
52+
return [
53+
['1', 1],
54+
['1025', 1025],
55+
['1k', 1024],
56+
['1m', 1024*1024],
57+
['1g', 1024*1024*1024],
58+
];
59+
}
60+
}

0 commit comments

Comments
 (0)