From b97d632c093a87a8e594c3a9c668401c0a573881 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Mon, 30 Dec 2019 22:34:40 -0700 Subject: [PATCH] Refactored sizeForHumans to take an SplFileInfo object instead of an integer file size as it's parameter --- app/Bootstrap/ViewFunctions/SizeForHumans.php | 12 ++++--- app/resources/views/components/file.twig | 2 +- .../ViewFunctions/SizeForHumansTest.php | 36 +++++++++++++++---- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/app/Bootstrap/ViewFunctions/SizeForHumans.php b/app/Bootstrap/ViewFunctions/SizeForHumans.php index 908351d..f107a5f 100644 --- a/app/Bootstrap/ViewFunctions/SizeForHumans.php +++ b/app/Bootstrap/ViewFunctions/SizeForHumans.php @@ -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]; } } diff --git a/app/resources/views/components/file.twig b/app/resources/views/components/file.twig index adc0434..a6c8338 100644 --- a/app/resources/views/components/file.twig +++ b/app/resources/views/components/file.twig @@ -35,7 +35,7 @@ {% if parentDir or file.isDir %} — {% else %} - {{ sizeForHumans(file.getSize) }} + {{ sizeForHumans(file) }} {% endif %} diff --git a/tests/Unit/Bootstrap/ViewFunctions/SizeForHumansTest.php b/tests/Unit/Bootstrap/ViewFunctions/SizeForHumansTest.php index 89c5b72..8ab4920 100644 --- a/tests/Unit/Bootstrap/ViewFunctions/SizeForHumansTest.php +++ b/tests/Unit/Bootstrap/ViewFunctions/SizeForHumansTest.php @@ -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)); } }