From d3a4be4bde6ee39582a5bba21ca629ddb27ee84e Mon Sep 17 00:00:00 2001 From: Thomas Steur <thomas.steur@gmail.com> Date: Tue, 20 Jan 2015 01:20:24 +0000 Subject: [PATCH] fix getPrettySizeBytes does not return requested unit if a lower unit is more appropriate --- core/Metrics/Formatter.php | 7 ++++++- .../PHPUnit/Unit/Metrics/Formatter/HtmlTest.php | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/core/Metrics/Formatter.php b/core/Metrics/Formatter.php index 5d9eb73a35..bdf920e6a0 100644 --- a/core/Metrics/Formatter.php +++ b/core/Metrics/Formatter.php @@ -134,10 +134,15 @@ class Formatter } $units = array('B', 'K', 'M', 'G', 'T'); + $numUnits = count($units) - 1; $currentUnit = null; foreach ($units as $idx => $currentUnit) { - if ($size >= 1024 && $unit != $currentUnit && $idx != count($units) - 1) { + if ($unit && $unit !== $currentUnit) { + $size = $size / 1024; + } elseif ($unit && $unit === $currentUnit) { + break; + } elseif ($size >= 1024 && $idx != $numUnits) { $size = $size / 1024; } else { break; diff --git a/tests/PHPUnit/Unit/Metrics/Formatter/HtmlTest.php b/tests/PHPUnit/Unit/Metrics/Formatter/HtmlTest.php index dd0c0a106b..004e749e25 100644 --- a/tests/PHPUnit/Unit/Metrics/Formatter/HtmlTest.php +++ b/tests/PHPUnit/Unit/Metrics/Formatter/HtmlTest.php @@ -61,6 +61,22 @@ class HtmlTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $value); } + public function test_getPrettySizeFromBytes_InFixedUnitThatIsHigherThanBestUnit() + { + $expected = '0.001465 M'; + $value = $this->formatter->getPrettySizeFromBytes(1536, 'M', 6); + + $this->assertEquals($expected, $value); + } + + public function test_getPrettySizeFromBytes_InUnitThatIsLowerThanBestUnit() + { + $expected = '1536 B'; + $value = $this->formatter->getPrettySizeFromBytes(1536, 'B'); + + $this->assertEquals($expected, $value); + } + public function test_getPrettyMoney_UsesNonBreakingSpaces() { $expected = '1 €'; -- GitLab