Skip to content

Commit 52ccd2e

Browse files
alexander-schranzfancyweb
authored andcommitted
[Validator] Fix minRatio and maxRatio when getting rounded
1 parent e0af98b commit 52ccd2e

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Constraints/ImageValidator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ public function validate($value, Constraint $constraint)
169169
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum ratio.', $constraint->minRatio));
170170
}
171171

172-
if ($ratio < $constraint->minRatio) {
172+
if ($ratio < round($constraint->minRatio, 2)) {
173173
$this->context->buildViolation($constraint->minRatioMessage)
174174
->setParameter('{{ ratio }}', $ratio)
175-
->setParameter('{{ min_ratio }}', $constraint->minRatio)
175+
->setParameter('{{ min_ratio }}', round($constraint->minRatio, 2))
176176
->setCode(Image::RATIO_TOO_SMALL_ERROR)
177177
->addViolation();
178178
}
@@ -183,10 +183,10 @@ public function validate($value, Constraint $constraint)
183183
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum ratio.', $constraint->maxRatio));
184184
}
185185

186-
if ($ratio > $constraint->maxRatio) {
186+
if ($ratio > round($constraint->maxRatio, 2)) {
187187
$this->context->buildViolation($constraint->maxRatioMessage)
188188
->setParameter('{{ ratio }}', $ratio)
189-
->setParameter('{{ max_ratio }}', $constraint->maxRatio)
189+
->setParameter('{{ max_ratio }}', round($constraint->maxRatio, 2))
190190
->setCode(Image::RATIO_TOO_BIG_ERROR)
191191
->addViolation();
192192
}
102 Bytes
Loading

Tests/Constraints/ImageValidatorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected function setUp(): void
4848
$this->imageLandscape = __DIR__.'/Fixtures/test_landscape.gif';
4949
$this->imagePortrait = __DIR__.'/Fixtures/test_portrait.gif';
5050
$this->image4By3 = __DIR__.'/Fixtures/test_4by3.gif';
51+
$this->image16By9 = __DIR__.'/Fixtures/test_16by9.gif';
5152
$this->imageCorrupted = __DIR__.'/Fixtures/test_corrupted.gif';
5253
}
5354

@@ -304,6 +305,28 @@ public function testMaxRatioUsesTwoDecimalsOnly()
304305
$this->assertNoViolation();
305306
}
306307

308+
public function testMinRatioUsesInputMoreDecimals()
309+
{
310+
$constraint = new Image([
311+
'minRatio' => 4 / 3,
312+
]);
313+
314+
$this->validator->validate($this->image4By3, $constraint);
315+
316+
$this->assertNoViolation();
317+
}
318+
319+
public function testMaxRatioUsesInputMoreDecimals()
320+
{
321+
$constraint = new Image([
322+
'maxRatio' => 16 / 9,
323+
]);
324+
325+
$this->validator->validate($this->image16By9, $constraint);
326+
327+
$this->assertNoViolation();
328+
}
329+
307330
public function testInvalidMinRatio()
308331
{
309332
$this->expectException(ConstraintDefinitionException::class);

0 commit comments

Comments
 (0)