-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |