-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstraintValidatorTestCase.php
More file actions
72 lines (60 loc) · 2.18 KB
/
ConstraintValidatorTestCase.php
File metadata and controls
72 lines (60 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace AssoConnect\ValidatorBundle\Test;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase as SymfonyConstraintValidatorTestCase;
/**
* @template T of ConstraintValidator
* @template-extends SymfonyConstraintValidatorTestCase<T>
*/
abstract class ConstraintValidatorTestCase extends SymfonyConstraintValidatorTestCase
{
abstract protected function getConstraint(): Constraint;
abstract public function createValidator(): ConstraintValidator;
/**
* @param mixed[] $array1
* @param mixed[] $array2
*/
protected static function assertArrayContainsSameObjects(array $array1, array $array2, string $message = ''): void
{
self::assertThat($array1, new ArrayContainSameObjectsConstraint($array2), $message);
}
public function testUnknownConstraintThrowsAnException(): void
{
$this->expectException(UnexpectedTypeException::class);
$this->validator->validate(0, new NotBlank());
}
/**
* @dataProvider providerValidValues
*/
public function testValidValues(mixed $value): void
{
$this->validator->validate($value, $this->getConstraint());
self::assertNoViolation();
}
/**
* @return iterable<mixed>
*/
abstract public function providerValidValues(): iterable;
/**
* @dataProvider providerInvalidValues
* @param array<string, mixed>|null $parameters
*/
public function testInvalidValues(mixed $value, string $code, string $message, array $parameters = null): void
{
$this->validator->validate($value, $this->getConstraint());
$this->buildViolation($message)
->setCode($code)
->setParameters($parameters ?? [
'{{ value }}' => is_string($value) ? '"' . $value . '"' : (string) $value,
])
->assertRaised();
}
/**
* @return iterable<mixed>
*/
abstract public function providerInvalidValues(): iterable;
}