This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split memory string formatting into a separate class (#4)
- Loading branch information
Showing
5 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
composer.lock | ||
vendor | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MinistryOfJustice\PHPUnit; | ||
|
||
class MemoryStringFormatter | ||
{ | ||
public static function roundToStandardUnits(int $bytes): string | ||
{ | ||
$unit = ['B','KB','MB','GB']; | ||
|
||
$magnitude = floor(log($bytes, 1024)); | ||
$value = round($bytes / pow(1024, $magnitude), 2); | ||
|
||
return $value . ' ' . $unit[$magnitude]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MinistryOfJustice\PHPUnit\Tests; | ||
|
||
use MinistryOfJustice\PHPUnit\MemoryStringFormatter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MemoryStringFormatterTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider memoryProvider | ||
*/ | ||
public function test_roundToStandardUnits(string $expectedValue, int $bytes): void | ||
{ | ||
$this->assertEquals($expectedValue, MemoryStringFormatter::roundToStandardUnits($bytes)); | ||
} | ||
|
||
public function memoryProvider(): array | ||
{ | ||
return [ | ||
'1 Byte' => ['1 B', 1], | ||
'Maximum Bytes' => ['1023 B', 1023], | ||
'1 Kilobyte' => ['1 KB', 1024], | ||
'Maximum Kilobytes' => ['1024 KB', 1048575], | ||
'1 Megabyte' => ['1 MB', 1048576], | ||
'Maximum Megabytes' => ['1024 MB', 1073741823], | ||
'1 Gigabyte' => ['1 GB', 1073741824], | ||
'1024 Gigabytes' => ['1024 GB', 1099511627775], | ||
]; | ||
} | ||
} |