Convert::memstring2bytes should return integer value

bytes are by nature an integer

fixes #8572
This commit is contained in:
Werner M. Krauß 2018-11-07 17:01:36 +01:00
parent 11fe5b3adf
commit 3f321f935a
2 changed files with 19 additions and 19 deletions

View File

@ -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);
}
/**

View File

@ -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']
];
}
}