Skip to content

Commit 2147a7e

Browse files
snoobwebmozart
authored andcommitted
[Validator][Console][HttpFoundation] Use "KiB" everywhere (instead of "kB")
1 parent f03d97e commit 2147a7e

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

Constraints/FileValidator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public function validate($value, Constraint $constraint)
4545
if (ctype_digit((string) $constraint->maxSize)) {
4646
$maxSize = (int) $constraint->maxSize;
4747
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
48-
$maxSize = $constraint->maxSize * 1024;
48+
$maxSize = $constraint->maxSize * 1000;
4949
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
50-
$maxSize = $constraint->maxSize * 1048576;
50+
$maxSize = $constraint->maxSize * 1000 * 1000;
5151
} else {
5252
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
5353
}
@@ -119,9 +119,9 @@ public function validate($value, Constraint $constraint)
119119
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
120120
$size = round(filesize($path) / 1000, 2);
121121
$limit = (int) $constraint->maxSize;
122-
$suffix = 'kB';
122+
$suffix = 'KB';
123123
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
124-
$size = round(filesize($path) / 1000000, 2);
124+
$size = round(filesize($path) / (1000 * 1000), 2);
125125
$limit = (int) $constraint->maxSize;
126126
$suffix = 'MB';
127127
} else {

Tests/Constraints/FileValidatorTest.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public function testTooLargeKiloBytes()
116116
->method('addViolation')
117117
->with('myMessage', array(
118118
'{{ limit }}' => '1',
119-
'{{ size }}' => '1.4',
120-
'{{ suffix }}' => 'kB',
119+
'{{ size }}' => '1.37',
120+
'{{ suffix }}' => 'KiB',
121121
'{{ file }}' => $this->path,
122122
));
123123

@@ -137,14 +137,38 @@ public function testTooLargeMegaBytes()
137137
->method('addViolation')
138138
->with('myMessage', array(
139139
'{{ limit }}' => '1',
140-
'{{ size }}' => '1.4',
141-
'{{ suffix }}' => 'MB',
140+
'{{ size }}' => '1.34',
141+
'{{ suffix }}' => 'MiB',
142142
'{{ file }}' => $this->path,
143143
));
144144

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

148+
public function testMaxSizeKiloBytes()
149+
{
150+
fwrite($this->file, str_repeat('0', 1010));
151+
152+
$constraint = new File(array(
153+
'maxSize' => '1k',
154+
));
155+
156+
$this->context->expects($this->never())->method('addViolation');
157+
$this->validator->validate($this->getFile($this->path), $constraint);
158+
}
159+
160+
public function testMaxSizeMegaBytes()
161+
{
162+
fwrite($this->file, str_repeat('0', (1024 * 1022)));
163+
164+
$constraint = new File(array(
165+
'maxSize' => '1M',
166+
));
167+
168+
$this->context->expects($this->never())->method('addViolation');
169+
$this->validator->validate($this->getFile($this->path), $constraint);
170+
}
171+
148172
/**
149173
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
150174
*/

0 commit comments

Comments
 (0)