Refactored sizeForHumans to take an SplFileInfo object instead of an integer file size as it's parameter

This commit is contained in:
Chris Kankiewicz
2019-12-30 22:34:40 -07:00
parent 8353d80c89
commit b97d632c09
3 changed files with 37 additions and 13 deletions

View File

@@ -2,23 +2,25 @@
namespace App\Bootstrap\ViewFunctions;
use Symfony\Component\Finder\SplFileInfo;
class SizeForHumans extends ViewFunction
{
/** @var string The function name */
protected $name = 'sizeForHumans';
/**
* Convert file size from bytes to a readable size for humans.
* Get the human readable file size from a file object.
*
* @param int $bytes File size in bytes
* @param SplFileInfo $file A file object
*
* @return string
*/
public function __invoke(int $bytes): string
public function __invoke(SplFileInfo $file): string
{
$sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$factor = (int) floor((strlen((string) $bytes) - 1) / 3);
$factor = (int) floor((strlen((string) $file->getSize()) - 1) / 3);
return sprintf('%.2f', $bytes / pow(1024, $factor)) . $sizes[$factor];
return sprintf('%.2f', $file->getSize() / pow(1024, $factor)) . $sizes[$factor];
}
}

View File

@@ -35,7 +35,7 @@
{% if parentDir or file.isDir %}
{% else %}
{{ sizeForHumans(file.getSize) }}
{{ sizeForHumans(file) }}
{% endif %}
</div>

View File

@@ -5,55 +5,77 @@ namespace Tests\Unit\Bootstrap\ViewFunctions;
use App\Bootstrap\ViewFunctions\SizeForHumans;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\SplFileInfo;
class SizeForHumansTest extends TestCase
{
public function test_it_can_convert_bytes_to_bytes(): void
{
$file = $this->createMock(SplFileInfo::class);
$file->method('getSize')->willReturn(13);
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.00B', $sizeForHumans(13));
$this->assertEquals('13.00B', $sizeForHumans($file));
}
public function test_it_can_convert_bytes_to_kibibytes(): void
{
$file = $this->createMock(SplFileInfo::class);
$file->method('getSize')->willReturn(13690);
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.37KB', $sizeForHumans(13690));
$this->assertEquals('13.37KB', $sizeForHumans($file));
}
public function test_it_can_convert_bytes_to_mebibytes(): void
{
$file = $this->createMock(SplFileInfo::class);
$file->method('getSize')->willReturn(14019461);
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.37MB', $sizeForHumans(14019461));
$this->assertEquals('13.37MB', $sizeForHumans($file));
}
public function test_it_can_convert_bytes_to_gibibytes(): void
{
$file = $this->createMock(SplFileInfo::class);
$file->method('getSize')->willReturn(14355900000);
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.37GB', $sizeForHumans(14355900000));
$this->assertEquals('13.37GB', $sizeForHumans($file));
}
public function test_it_can_convert_bytes_to_tebibytes(): void
{
$file = $this->createMock(SplFileInfo::class);
$file->method('getSize')->willReturn(14700500000000);
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.37TB', $sizeForHumans(14700500000000));
$this->assertEquals('13.37TB', $sizeForHumans($file));
}
public function test_it_can_convert_bytes_to_pebibytes(): void
{
$file = $this->createMock(SplFileInfo::class);
$file->method('getSize')->willReturn(15053300000000000);
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.37PB', $sizeForHumans(15053300000000000));
$this->assertEquals('13.37PB', $sizeForHumans($file));
}
public function test_it_can_convert_bytes_to_exbibytes(): void
{
$file = $this->createMock(SplFileInfo::class);
$file->method('getSize')->willReturn(PHP_INT_MAX);
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('8.00EB', $sizeForHumans(PHP_INT_MAX));
$this->assertEquals('8.00EB', $sizeForHumans($file));
}
}