|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the FreshDoctrineEnumBundle |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Artem Genvald <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Fresh\DoctrineEnumBundle\Tests\Validator; |
|
12
|
|
|
|
|
13
|
|
|
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType; |
|
14
|
|
|
use Fresh\DoctrineEnumBundle\Validator\Constraints\Enum; |
|
15
|
|
|
use Fresh\DoctrineEnumBundle\Validator\Constraints\EnumValidator; |
|
16
|
|
|
use Symfony\Component\Validator\Context\ExecutionContext; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* EnumValidatorTest. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Artem Genvald <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class EnumValidatorTest extends \PHPUnit_Framework_TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var EnumValidator $enumValidator ENUM validator |
|
27
|
|
|
*/ |
|
28
|
|
|
private $enumValidator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ExecutionContext|\PHPUnit_Framework_MockObject_MockObject $context Context |
|
32
|
|
|
*/ |
|
33
|
|
|
private $context; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setUp() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->enumValidator = new EnumValidator(); |
|
41
|
|
|
|
|
42
|
|
|
$this->context = $this->getMockBuilder('Symfony\Component\Validator\Context\ExecutionContext') |
|
43
|
|
|
->disableOriginalConstructor() |
|
44
|
|
|
->getMock(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testExceptionEntityNotSpecified() |
|
51
|
|
|
{ |
|
52
|
|
|
$constraint = new Enum([ |
|
53
|
|
|
'entity' => null, |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
$this->enumValidator->validate(BasketballPositionType::POINT_GUARD, $constraint); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testValidBasketballPositionType() |
|
60
|
|
|
{ |
|
61
|
|
|
$constraint = new Enum([ |
|
62
|
|
|
'entity' => 'Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType', |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
$this->context->expects($this->never()) |
|
66
|
|
|
->method('buildViolation'); |
|
67
|
|
|
|
|
68
|
|
|
$this->enumValidator->initialize($this->context); |
|
69
|
|
|
$this->enumValidator->validate(BasketballPositionType::SMALL_FORWARD, $constraint); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testInvalidBasketballPositionType() |
|
73
|
|
|
{ |
|
74
|
|
|
$constraint = new Enum([ |
|
75
|
|
|
'entity' => 'Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType', |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
$constraintValidationBuilder = $this->getMockBuilder('Symfony\Component\Validator\Violation\ConstraintViolationBuilder') |
|
79
|
|
|
->disableOriginalConstructor() |
|
80
|
|
|
->getMock(); |
|
81
|
|
|
|
|
82
|
|
|
$constraintValidationBuilder->expects($this->once()) |
|
83
|
|
|
->method('setParameter') |
|
84
|
|
|
->with($this->equalTo('{{ value }}'), $this->equalTo('"Pitcher"')) |
|
85
|
|
|
->will($this->returnSelf()); |
|
86
|
|
|
|
|
87
|
|
|
$constraintValidationBuilder->expects($this->once()) |
|
88
|
|
|
->method('setCode') |
|
89
|
|
|
->will($this->returnSelf()); |
|
90
|
|
|
|
|
91
|
|
|
$this->context->expects($this->once()) |
|
92
|
|
|
->method('buildViolation') |
|
93
|
|
|
->with($this->equalTo('The value you selected is not a valid choice.')) |
|
94
|
|
|
->will($this->returnValue($constraintValidationBuilder)); |
|
95
|
|
|
|
|
96
|
|
|
$this->enumValidator->initialize($this->context); |
|
97
|
|
|
$this->enumValidator->validate('Pitcher', $constraint); // It's not a baseball =) |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|