Skip to content

Commit

Permalink
add tests #3701
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinx13 committed Feb 11, 2025
1 parent c30ddd7 commit b755f50
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Validator/SanitizedLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class SanitizedLength extends Constraint
public string $message = 'Le texte doit contenir au moins {{ limit }} caractères après sanitation.';
public int $min;

public function __construct(int $min, ?string $message = null, ?array $groups = null, mixed $payload = null)
public function __construct(int $min, ?string $message = null)
{
parent::__construct([], $groups, $payload);
parent::__construct([]);
$this->min = $min;
if ($message) {
$this->message = $message;
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/SanitizedLengthValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function validate($value, Constraint $constraint): void
if (mb_strlen(trim(strip_tags($sanitizedText))) < $constraint->min) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ limit }}', $constraint->min)
->setParameter('{{ limit }}', (string) $constraint->min)
->addViolation();
}
}
Expand Down
58 changes: 58 additions & 0 deletions tests/Unit/Validator/SanitizedLengthValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Tests\Unit\Validator;

use App\Validator\SanitizedLength;
use App\Validator\SanitizedLengthValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

class SanitizedLengthValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): SanitizedLengthValidator
{
return new SanitizedLengthValidator();
}

/**
* @dataProvider provideValidValues
*/
public function testValueIsValid(mixed $value): void
{
$constraint = new SanitizedLength(10, 'Text too short.');
$this->validator->validate($value, $constraint);
$this->assertNoViolation();
}

/**
* @dataProvider provideInvalidValues
*/
public function testSanitizedTextTooShort(string $value): void
{
$constraint = new SanitizedLength(10, 'Text too short.');
$this->validator->validate($value, $constraint);
$this->buildViolation('Text too short.')
->setParameter('{{ limit }}', '10')
->assertRaised();
}

public function provideValidValues(): \Generator
{
yield 'null value' => [null];
yield 'empty value' => [''];
yield 'valid value' => ['<p>Lorem ipsum dolor sit amet</p>'];
}

public function provideInvalidValues(): \Generator
{
yield 'too short value with no html' => ['Hi buddy!'];
yield 'too short value with html' => ['<b>Hi buddy!</b>'];
}

public function testNonStringThrowsException(): void
{
$this->expectException(UnexpectedValueException::class);
$constraint = new SanitizedLength(10, 'Text too short.');
$this->validator->validate(12345, $constraint);
}
}

0 comments on commit b755f50

Please sign in to comment.