|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\EnumBundle\Tests\Validator\Constraints; |
|
4
|
|
|
|
|
5
|
|
|
use Prophecy\PhpUnit\ProphecyTrait; |
|
6
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
7
|
|
|
use Symfony\Component\Validator\Constraints\Choice; |
|
8
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
|
9
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
10
|
|
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
|
11
|
|
|
use Yokai\EnumBundle\EnumRegistry; |
|
12
|
|
|
use Yokai\EnumBundle\Tests\Fixtures\GenderEnum; |
|
13
|
|
|
use Yokai\EnumBundle\Tests\Fixtures\TypeEnum; |
|
14
|
|
|
use Yokai\EnumBundle\Validator\Constraints\Enum; |
|
15
|
|
|
use Yokai\EnumBundle\Validator\Constraints\EnumValidator; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Yann Eugoné <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class EnumValidatorTest extends ConstraintValidatorTestCase |
|
21
|
|
|
{ |
|
22
|
|
|
use ProphecyTrait; |
|
23
|
|
|
|
|
24
|
|
|
protected function createValidator(): EnumValidator |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var EnumRegistry|ObjectProphecy $registry */ |
|
27
|
|
|
$registry = $this->prophesize(EnumRegistry::class); |
|
28
|
|
|
$registry->has('state')->willReturn(false); |
|
|
|
|
|
|
29
|
|
|
$registry->has(GenderEnum::class)->willReturn(true); |
|
30
|
|
|
$registry->has('type')->willReturn(true); |
|
31
|
|
|
$registry->get('type')->willReturn(new TypeEnum); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
return new EnumValidator($registry->reveal()); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testAcceptOnlyEnum(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->expectException(UnexpectedTypeException::class); |
|
39
|
|
|
$this->validator->validate(null, new Choice); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testEnumIsRequired(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->expectException(ConstraintDefinitionException::class); |
|
45
|
|
|
$this->validator->validate('foo', new Enum); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testValidEnumIsRequired(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->expectException(ConstraintDefinitionException::class); |
|
51
|
|
|
$this->validator->validate('foo', new Enum('state')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testNullIsValid(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->validator->validate(null, new Enum('type')); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertNoViolation(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testValidSingleEnum(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$this->validator->validate('customer', new Enum('type')); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertNoViolation(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testInvalidSingleEnum(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$constraint = new Enum(['enum' => 'type', 'message' => 'myMessage']); |
|
71
|
|
|
|
|
72
|
|
|
$this->validator->validate('foo', $constraint); |
|
73
|
|
|
|
|
74
|
|
|
$this->buildViolation('myMessage') |
|
75
|
|
|
->setParameter('{{ value }}', '"foo"') |
|
76
|
|
|
->setCode(Choice::NO_SUCH_CHOICE_ERROR) |
|
77
|
|
|
->setParameter('{{ choices }}', '"customer", "prospect"') |
|
78
|
|
|
->assertRaised(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testValidMultipleEnum(): void |
|
82
|
|
|
{ |
|
83
|
|
|
$constraint = new Enum(['enum' => 'type', 'multiple' => true]); |
|
84
|
|
|
|
|
85
|
|
|
$this->validator->validate(['customer', 'prospect'], $constraint); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertNoViolation(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testInvalidMultipleEnum(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$constraint = new Enum(['enum' => 'type', 'multiple' => true, 'multipleMessage' => 'myMessage']); |
|
93
|
|
|
|
|
94
|
|
|
$this->validator->validate(['customer', 'foo'], $constraint); |
|
95
|
|
|
|
|
96
|
|
|
$this->buildViolation('myMessage') |
|
97
|
|
|
->setParameter('{{ value }}', '"foo"') |
|
98
|
|
|
->setInvalidValue('foo') |
|
99
|
|
|
->setCode(Choice::NO_SUCH_CHOICE_ERROR) |
|
100
|
|
|
->setParameter('{{ choices }}', '"customer", "prospect"') |
|
101
|
|
|
->assertRaised(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: