|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\Enum\Tests\Bridge\Symfony\Validator\Constraints; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Validator\Constraints\Choice; |
|
6
|
|
|
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest; |
|
7
|
|
|
use Yokai\Enum\Bridge\Symfony\Validator\Constraints\Enum; |
|
8
|
|
|
use Yokai\Enum\Bridge\Symfony\Validator\Constraints\EnumValidator; |
|
9
|
|
|
use Yokai\Enum\EnumRegistry; |
|
10
|
|
|
use Yokai\Enum\Tests\Fixtures\GenderEnum; |
|
11
|
|
|
use Yokai\Enum\Tests\Fixtures\TypeEnum; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Yann Eugoné <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class EnumValidatorTest extends AbstractConstraintValidatorTest |
|
17
|
|
|
{ |
|
18
|
|
|
protected function createValidator() |
|
19
|
|
|
{ |
|
20
|
|
|
$registry = $this->prophesize(EnumRegistry::class); |
|
21
|
|
|
$registry->has('state')->willReturn(false); |
|
22
|
|
|
$registry->has(GenderEnum::class)->willReturn(true); |
|
23
|
|
|
$registry->has('type')->willReturn(true); |
|
24
|
|
|
$registry->get('type')->willReturn(new TypeEnum); |
|
25
|
|
|
|
|
26
|
|
|
return new EnumValidator($registry->reveal()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testAcceptOnlyEnum() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->expectException('Symfony\Component\Validator\Exception\UnexpectedTypeException'); |
|
32
|
|
|
$this->validator->validate(null, new Choice); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testEnumIsRequired() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); |
|
38
|
|
|
$this->validator->validate('foo', new Enum); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testValidEnumIsRequired() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); |
|
44
|
|
|
$this->validator->validate('foo', new Enum('state')); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testNullIsValid() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->validator->validate(null, new Enum('type')); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertNoViolation(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testValidSingleEnum() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->validator->validate('customer', new Enum('type')); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertNoViolation(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testInvalidSingleEnum() |
|
62
|
|
|
{ |
|
63
|
|
|
$constraint = new Enum(['enum' => 'type', 'message' => 'myMessage']); |
|
64
|
|
|
|
|
65
|
|
|
$this->validator->validate('foo', $constraint); |
|
66
|
|
|
|
|
67
|
|
|
$this->buildViolation('myMessage') |
|
68
|
|
|
->setParameter('{{ value }}', '"foo"') |
|
69
|
|
|
->setCode(Choice::NO_SUCH_CHOICE_ERROR) |
|
70
|
|
|
->assertRaised(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testValidMultipleEnum() |
|
74
|
|
|
{ |
|
75
|
|
|
$constraint = new Enum(['enum' => 'type', 'multiple' => true]); |
|
76
|
|
|
|
|
77
|
|
|
$this->validator->validate(['customer', 'prospect'], $constraint); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertNoViolation(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testInvalidMultipleEnum() |
|
83
|
|
|
{ |
|
84
|
|
|
$constraint = new Enum(['enum' => 'type', 'multiple' => true, 'multipleMessage' => 'myMessage']); |
|
85
|
|
|
|
|
86
|
|
|
$this->validator->validate(['customer', 'foo'], $constraint); |
|
87
|
|
|
|
|
88
|
|
|
$this->buildViolation('myMessage') |
|
89
|
|
|
->setParameter('{{ value }}', '"foo"') |
|
90
|
|
|
->setInvalidValue('foo') |
|
91
|
|
|
->setCode(Choice::NO_SUCH_CHOICE_ERROR) |
|
92
|
|
|
->assertRaised(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|