Skip to content

Commit

Permalink
[Validator][Console][HttpFoundation] Use "KiB" everywhere (instead of…
Browse files Browse the repository at this point in the history
… "kB")
  • Loading branch information
snoob authored and webmozart committed May 22, 2014
1 parent b2855a5 commit bbe1045
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Component/Console/Helper/Helper.php
Expand Up @@ -92,15 +92,15 @@ public static function formatTime($secs)
public static function formatMemory($memory)
{
if ($memory >= 1024 * 1024 * 1024) {
return sprintf('%.1f GB', $memory / 1024 / 1024 / 1024);
return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
}

if ($memory >= 1024 * 1024) {
return sprintf('%.1f MB', $memory / 1024 / 1024);
return sprintf('%.1f MiB', $memory / 1024 / 1024);
}

if ($memory >= 1024) {
return sprintf('%d kB', $memory / 1024);
return sprintf('%d KiB', $memory / 1024);
}

return sprintf('%d B', $memory);
Expand Down
Expand Up @@ -367,17 +367,17 @@ public function testAnsiColorsAndEmojis()
$this->generateOutput(
" \033[44;37m Starting the demo... fingers crossed \033[0m\n".
" 0/15 ".$progress.str_repeat($empty, 26)." 0%\n".
" 🏁 1 sec \033[44;37m 0 B \033[0m"
" \xf0\x9f\x8f\x81 1 sec \033[44;37m 0 B \033[0m"
).
$this->generateOutput(
" \033[44;37m Looks good to me... \033[0m\n".
" 4/15 ".str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n".
" 🏁 1 sec \033[41;37m 97 kB \033[0m"
" \xf0\x9f\x8f\x81 1 sec \033[41;37m 97 KiB \033[0m"
).
$this->generateOutput(
" \033[44;37m Thanks, bye \033[0m\n".
" 15/15 ".str_repeat($done, 28)." 100%\n".
" 🏁 1 sec \033[41;37m 195 kB \033[0m"
" \xf0\x9f\x8f\x81 1 sec \033[41;37m 195 KiB \033[0m"
),
stream_get_contents($output->getStream())
);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/File/UploadedFile.php
Expand Up @@ -291,7 +291,7 @@ public static function getMaxFilesize()
public function getErrorMessage()
{
static $errors = array(
UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d kb).',
UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',
UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.',
UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.',
UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Validator/Constraints/FileValidator.php
Expand Up @@ -45,9 +45,9 @@ public function validate($value, Constraint $constraint)
if (ctype_digit((string) $constraint->maxSize)) {
$maxSize = (int) $constraint->maxSize;
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
$maxSize = $constraint->maxSize * 1024;
$maxSize = $constraint->maxSize * 1000;
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
$maxSize = $constraint->maxSize * 1048576;
$maxSize = $constraint->maxSize * 1000 * 1000;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
}
Expand Down Expand Up @@ -119,9 +119,9 @@ public function validate($value, Constraint $constraint)
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
$size = round(filesize($path) / 1000, 2);
$limit = (int) $constraint->maxSize;
$suffix = 'kB';
$suffix = 'KB';
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
$size = round(filesize($path) / 1000000, 2);
$size = round(filesize($path) / (1000 * 1000), 2);
$limit = (int) $constraint->maxSize;
$suffix = 'MB';
} else {
Expand Down
Expand Up @@ -116,8 +116,8 @@ public function testTooLargeKiloBytes()
->method('addViolation')
->with('myMessage', array(
'{{ limit }}' => '1',
'{{ size }}' => '1.4',
'{{ suffix }}' => 'kB',
'{{ size }}' => '1.37',
'{{ suffix }}' => 'KiB',
'{{ file }}' => $this->path,
));

Expand All @@ -137,14 +137,38 @@ public function testTooLargeMegaBytes()
->method('addViolation')
->with('myMessage', array(
'{{ limit }}' => '1',
'{{ size }}' => '1.4',
'{{ suffix }}' => 'MB',
'{{ size }}' => '1.34',
'{{ suffix }}' => 'MiB',
'{{ file }}' => $this->path,
));

$this->validator->validate($this->getFile($this->path), $constraint);
}

public function testMaxSizeKiloBytes()
{
fwrite($this->file, str_repeat('0', 1010));

$constraint = new File(array(
'maxSize' => '1k',
));

$this->context->expects($this->never())->method('addViolation');
$this->validator->validate($this->getFile($this->path), $constraint);
}

public function testMaxSizeMegaBytes()
{
fwrite($this->file, str_repeat('0', (1024 * 1022)));

$constraint = new File(array(
'maxSize' => '1M',
));

$this->context->expects($this->never())->method('addViolation');
$this->validator->validate($this->getFile($this->path), $constraint);
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
Expand Down

0 comments on commit bbe1045

Please sign in to comment.