Commit f3520187 by Kai Mindermann Committed by Alexander Makarov

formatter: adds unit tests for base 1000, fixes #4412

parent 42517942
......@@ -70,6 +70,7 @@ Yii Framework 2 Change Log
- Bug #4276: Added check for UPLOAD_ERR_NO_FILE in `yii\web\UploadedFile` and return null if no file was uploaded (OmgDef)
- Bug #4342: mssql (dblib) driver does not support getting attributes (tof06)
- Bug #4409: Upper case letters in subdirectory prefixes of controller IDs were not properly handled (qiangxue)
- Bug #4412: Formatter used SI Prefixes for base 1024, now uses binary prefixes (kmindi)
- Bug: Fixed inconsistent return of `\yii\console\Application::runAction()` (samdark)
- Bug: URL encoding for the route parameter added to `\yii\web\UrlManager` (klimov-paul)
- Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue)
......
......@@ -444,10 +444,13 @@ class Formatter extends Component
* @param integer $value value in bytes to be formatted
* @param boolean $verbose if full names should be used (e.g. bytes, kilobytes, ...).
* Defaults to false meaning that short names will be used (e.g. B, KB, ...).
* @param boolean $binaryPrefix if binary prefixes should be used for base 1024
* Defaults to true meaning that binary prefixes are used (e.g. kibibyte/KiB, mebibyte/MiB, ...).
* @link http://en.wikipedia.org/wiki/Binary_prefix
* @return string the formatted result
* @see sizeFormat
*/
public function asSize($value, $verbose = false)
public function asSize($value, $verbose = false, $binaryPrefix = true)
{
$position = 0;
......@@ -464,6 +467,23 @@ class Formatter extends Component
$formattedValue = isset($this->sizeFormat['decimalSeparator']) ? str_replace('.', $this->sizeFormat['decimalSeparator'], $value) : $value;
$params = ['n' => $formattedValue];
if ($binaryPrefix && $this->sizeFormat['base'] === 1024) {
switch ($position) {
case 0:
return $verbose ? Yii::t('yii', '{n, plural, =1{# byte} other{# bytes}}', $params) : Yii::t('yii', '{n} B', $params);
case 1:
return $verbose ? Yii::t('yii', '{n, plural, =1{# kibibyte} other{# kibibytes}}', $params) : Yii::t('yii', '{n} KiB', $params);
case 2:
return $verbose ? Yii::t('yii', '{n, plural, =1{# mebibyte} other{# mebibytes}}', $params) : Yii::t('yii', '{n} MiB', $params);
case 3:
return $verbose ? Yii::t('yii', '{n, plural, =1{# gibibyte} other{# gibibytes}}', $params) : Yii::t('yii', '{n} GiB', $params);
case 4:
return $verbose ? Yii::t('yii', '{n, plural, =1{# tebibyte} other{# tebibytes}}', $params) : Yii::t('yii', '{n} TiB', $params);
default:
return $verbose ? Yii::t('yii', '{n, plural, =1{# pebibyte} other{# pebibytes}}', $params) : Yii::t('yii', '{n} PiB', $params);
}
}
switch ($position) {
case 0:
return $verbose ? Yii::t('yii', '{n, plural, =1{# byte} other{# bytes}}', $params) : Yii::t('yii', '{n} B', $params);
......@@ -479,7 +499,7 @@ class Formatter extends Component
return $verbose ? Yii::t('yii', '{n, plural, =1{# petabyte} other{# petabytes}}', $params) : Yii::t('yii', '{n} PB', $params);
}
}
/**
* Formats the value as the time interval between a date and now in human readable form.
*
......
......@@ -190,6 +190,28 @@ class FormatterTest extends TestCase
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asNumber(null));
}
public function testAsSize() {
// tests for base 1000
$this->formatter->sizeFormat['base'] = 1000;
$this->assertSame("1.05 MB", $this->formatter->asSize(1024 * 1024));
$this->assertSame("1.05 MB", $this->formatter->asSize(1024 * 1024, false, false));
$this->assertSame("1 KB", $this->formatter->asSize(1000));
$this->assertSame("1.02 KB", $this->formatter->asSize(1023));
$this->assertSame("3 gigabytes", $this->formatter->asSize(3 * 1000 * 1000 * 1000, true));
// tests for base 1024
$this->formatter->sizeFormat['base'] = 1024;
$this->assertSame("1 KiB", $this->formatter->asSize(1024));
$this->assertSame("1 MB", $this->formatter->asSize(1024 * 1024, false, false));
$this->assertSame("1023 B", $this->formatter->asSize(1023));
$this->assertSame("5 GiB", $this->formatter->asSize(5 * 1024 * 1024 * 1024));
//$this->assertSame("1 YiB", $this->formatter->asSize(pow(2, 80)));
$this->assertSame("2 GiB", $this->formatter->asSize(2147483647)); // round 1.999 up to 2
$this->formatter->sizeFormat['decimalSeparator'] = ',';
$this->formatter->sizeFormat['decimals'] = 3;
$this->assertSame("1,001 KiB", $this->formatter->asSize(1025));
}
public function testFormat()
{
$value = time();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment