From 3f321f935a0b7faa3d4ef788b4f69425db979095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Werner=20M=2E=20Krau=C3=9F?= Date: Wed, 7 Nov 2018 17:01:36 +0100 Subject: [PATCH] Convert::memstring2bytes should return integer value bytes are by nature an integer fixes #8572 --- src/Core/Convert.php | 6 +++--- tests/php/Core/ConvertTest.php | 32 ++++++++++++++++---------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Core/Convert.php b/src/Core/Convert.php index 7c8717724..3e4158111 100644 --- a/src/Core/Convert.php +++ b/src/Core/Convert.php @@ -556,7 +556,7 @@ class Convert * Preserves integer values like "1024" or "-1" * * @param string $memString A memory limit string, such as "64M" - * @return float + * @return int */ public static function memstring2bytes($memString) { @@ -568,10 +568,10 @@ class Convert if ($unit) { // Find the position of the unit in the ordered string which is the power // of magnitude to multiply a kilobyte by - return round($size * pow(1024, stripos('bkmgtpezy', $unit[0]))); + return (int)round($size * pow(1024, stripos('bkmgtpezy', $unit[0]))); } - return round($size); + return (int)round($size); } /** diff --git a/tests/php/Core/ConvertTest.php b/tests/php/Core/ConvertTest.php index 679c611e7..d1c1139d3 100644 --- a/tests/php/Core/ConvertTest.php +++ b/tests/php/Core/ConvertTest.php @@ -2,12 +2,12 @@ namespace SilverStripe\Core\Tests; +use Exception; +use InvalidArgumentException; use SilverStripe\Core\Convert; use SilverStripe\Dev\SapphireTest; use SilverStripe\View\Parsers\URLSegmentFilter; use stdClass; -use Exception; -use InvalidArgumentException; /** * Test various functions on the {@link Convert} class. @@ -576,15 +576,15 @@ XML public function memString2BytesProvider() { return [ - ['-1', (float)-1], - ['2048', (float)(2 * 1024)], - ['2k', (float)(2 * 1024)], - ['512M', (float)(512 * 1024 * 1024)], - ['512MiB', (float)(512 * 1024 * 1024)], - ['512 mbytes', (float)(512 * 1024 * 1024)], - ['512 megabytes', (float)(512 * 1024 * 1024)], - ['1024g', (float)(1024 * 1024 * 1024 * 1024)], - ['1024G', (float)(1024 * 1024 * 1024 * 1024)] + 'infinite' => ['-1', -1], + 'integer' => ['2048', 2 * 1024], + 'kilo' => ['2k', 2 * 1024], + 'mega' => ['512M', 512 * 1024 * 1024], + 'MiB' => ['512MiB', 512 * 1024 * 1024], + 'mbytes' => ['512 mbytes', 512 * 1024 * 1024], + 'megabytes' => ['512 megabytes', 512 * 1024 * 1024], + 'giga' => ['1024g', 1024 * 1024 * 1024 * 1024], + 'G' => ['1024G', 1024 * 1024 * 1024 * 1024] ]; } @@ -607,11 +607,11 @@ XML { return [ [200, '200B'], - [(2 * 1024), '2K'], - [(512 * 1024 * 1024), '512M'], - [(512 * 1024 * 1024 * 1024), '512G'], - [(512 * 1024 * 1024 * 1024 * 1024), '512T'], - [(512 * 1024 * 1024 * 1024 * 1024 * 1024), '512P'] + [2 * 1024, '2K'], + [512 * 1024 * 1024, '512M'], + [512 * 1024 * 1024 * 1024, '512G'], + [512 * 1024 * 1024 * 1024 * 1024, '512T'], + [512 * 1024 * 1024 * 1024 * 1024 * 1024, '512P'] ]; } }