Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
f3520187
Commit
f3520187
authored
Jul 24, 2014
by
Kai Mindermann
Committed by
Alexander Makarov
Jul 24, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
formatter: adds unit tests for base 1000, fixes #4412
parent
42517942
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
2 deletions
+45
-2
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
Formatter.php
framework/base/Formatter.php
+22
-2
FormatterTest.php
tests/unit/framework/base/FormatterTest.php
+22
-0
No files found.
framework/CHANGELOG.md
View file @
f3520187
...
...
@@ -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)
...
...
framework/base/Formatter.php
View file @
f3520187
...
...
@@ -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.
*
...
...
tests/unit/framework/base/FormatterTest.php
View file @
f3520187
...
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment