|
14 | 14 | use Symfony\Component\PropertyAccess\PropertyAccess;
|
15 | 15 | use Symfony\Component\Validator\Constraints\Expression;
|
16 | 16 | use Symfony\Component\Validator\Constraints\ExpressionValidator;
|
| 17 | +use Symfony\Component\Validator\Tests\Fixtures\Entity; |
17 | 18 |
|
18 |
| -class ExpressionValidatorTest extends \PHPUnit_Framework_TestCase |
| 19 | +class ExpressionValidatorTest extends AbstractConstraintValidatorTest |
19 | 20 | {
|
20 |
| - protected $context; |
21 |
| - protected $validator; |
22 |
| - |
23 |
| - protected function setUp() |
24 |
| - { |
25 |
| - $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); |
26 |
| - $this->validator = new ExpressionValidator(PropertyAccess::createPropertyAccessor()); |
27 |
| - $this->validator->initialize($this->context); |
28 |
| - |
29 |
| - $this->context->expects($this->any()) |
30 |
| - ->method('getClassName') |
31 |
| - ->will($this->returnValue(__CLASS__)); |
32 |
| - } |
33 |
| - |
34 |
| - protected function tearDown() |
| 21 | + protected function createValidator() |
35 | 22 | {
|
36 |
| - $this->context = null; |
37 |
| - $this->validator = null; |
| 23 | + return new ExpressionValidator(PropertyAccess::createPropertyAccessor()); |
38 | 24 | }
|
39 | 25 |
|
40 | 26 | public function testNullIsValid()
|
41 | 27 | {
|
42 |
| - $this->context->expects($this->never()) |
43 |
| - ->method('addViolation'); |
44 |
| - |
45 | 28 | $this->validator->validate(null, new Expression('value == 1'));
|
| 29 | + |
| 30 | + $this->assertNoViolation(); |
46 | 31 | }
|
47 | 32 |
|
48 | 33 | public function testEmptyStringIsValid()
|
49 | 34 | {
|
50 |
| - $this->context->expects($this->never()) |
51 |
| - ->method('addViolation'); |
52 |
| - |
53 | 35 | $this->validator->validate('', new Expression('value == 1'));
|
| 36 | + |
| 37 | + $this->assertNoViolation(); |
54 | 38 | }
|
55 | 39 |
|
56 | 40 | public function testSucceedingExpressionAtObjectLevel()
|
57 | 41 | {
|
58 |
| - $constraint = new Expression('this.property == 1'); |
59 |
| - |
60 |
| - $object = (object) array('property' => '1'); |
| 42 | + $constraint = new Expression('this.data == 1'); |
61 | 43 |
|
62 |
| - $this->context->expects($this->any()) |
63 |
| - ->method('getPropertyName') |
64 |
| - ->will($this->returnValue(null)); |
| 44 | + $object = new Entity(); |
| 45 | + $object->data = '1'; |
65 | 46 |
|
66 |
| - $this->context->expects($this->never()) |
67 |
| - ->method('addViolation'); |
| 47 | + $this->setObject($object); |
68 | 48 |
|
69 | 49 | $this->validator->validate($object, $constraint);
|
| 50 | + |
| 51 | + $this->assertNoViolation(); |
70 | 52 | }
|
71 | 53 |
|
72 | 54 | public function testFailingExpressionAtObjectLevel()
|
73 | 55 | {
|
74 | 56 | $constraint = new Expression(array(
|
75 |
| - 'expression' => 'this.property == 1', |
| 57 | + 'expression' => 'this.data == 1', |
76 | 58 | 'message' => 'myMessage',
|
77 | 59 | ));
|
78 | 60 |
|
79 |
| - $object = (object) array('property' => '2'); |
80 |
| - |
81 |
| - $this->context->expects($this->any()) |
82 |
| - ->method('getPropertyName') |
83 |
| - ->will($this->returnValue(null)); |
| 61 | + $object = new Entity(); |
| 62 | + $object->data = '2'; |
84 | 63 |
|
85 |
| - $this->context->expects($this->once()) |
86 |
| - ->method('addViolation') |
87 |
| - ->with('myMessage'); |
| 64 | + $this->setObject($object); |
88 | 65 |
|
89 | 66 | $this->validator->validate($object, $constraint);
|
| 67 | + |
| 68 | + $this->assertViolation('myMessage'); |
90 | 69 | }
|
91 | 70 |
|
92 | 71 | public function testSucceedingExpressionAtPropertyLevel()
|
93 | 72 | {
|
94 |
| - $constraint = new Expression('value == this.expected'); |
95 |
| - |
96 |
| - $object = (object) array('expected' => '1'); |
97 |
| - |
98 |
| - $this->context->expects($this->any()) |
99 |
| - ->method('getPropertyName') |
100 |
| - ->will($this->returnValue('property')); |
| 73 | + $constraint = new Expression('value == this.data'); |
101 | 74 |
|
102 |
| - $this->context->expects($this->any()) |
103 |
| - ->method('getPropertyPath') |
104 |
| - ->will($this->returnValue('property')); |
| 75 | + $object = new Entity(); |
| 76 | + $object->data = '1'; |
105 | 77 |
|
106 |
| - $this->context->expects($this->any()) |
107 |
| - ->method('getRoot') |
108 |
| - ->will($this->returnValue($object)); |
109 |
| - |
110 |
| - $this->context->expects($this->never()) |
111 |
| - ->method('addViolation'); |
| 78 | + $this->setRoot($object); |
| 79 | + $this->setPropertyPath('data'); |
| 80 | + $this->setProperty($object, 'data'); |
112 | 81 |
|
113 | 82 | $this->validator->validate('1', $constraint);
|
| 83 | + |
| 84 | + $this->assertNoViolation(); |
114 | 85 | }
|
115 | 86 |
|
116 | 87 | public function testFailingExpressionAtPropertyLevel()
|
117 | 88 | {
|
118 | 89 | $constraint = new Expression(array(
|
119 |
| - 'expression' => 'value == this.expected', |
| 90 | + 'expression' => 'value == this.data', |
120 | 91 | 'message' => 'myMessage',
|
121 | 92 | ));
|
122 | 93 |
|
123 |
| - $object = (object) array('expected' => '1'); |
124 |
| - |
125 |
| - $this->context->expects($this->any()) |
126 |
| - ->method('getPropertyName') |
127 |
| - ->will($this->returnValue('property')); |
128 |
| - |
129 |
| - $this->context->expects($this->any()) |
130 |
| - ->method('getPropertyPath') |
131 |
| - ->will($this->returnValue('property')); |
132 |
| - |
133 |
| - $this->context->expects($this->any()) |
134 |
| - ->method('getRoot') |
135 |
| - ->will($this->returnValue($object)); |
| 94 | + $object = new Entity(); |
| 95 | + $object->data = '1'; |
136 | 96 |
|
137 |
| - $this->context->expects($this->once()) |
138 |
| - ->method('addViolation') |
139 |
| - ->with('myMessage'); |
| 97 | + $this->setRoot($object); |
| 98 | + $this->setPropertyPath('data'); |
| 99 | + $this->setProperty($object, 'data'); |
140 | 100 |
|
141 | 101 | $this->validator->validate('2', $constraint);
|
| 102 | + |
| 103 | + $this->assertViolation('myMessage', array(), 'data'); |
142 | 104 | }
|
143 | 105 |
|
144 | 106 | public function testSucceedingExpressionAtNestedPropertyLevel()
|
145 | 107 | {
|
146 |
| - $constraint = new Expression('value == this.expected'); |
147 |
| - |
148 |
| - $object = (object) array('expected' => '1'); |
149 |
| - $root = (object) array('nested' => $object); |
150 |
| - |
151 |
| - $this->context->expects($this->any()) |
152 |
| - ->method('getPropertyName') |
153 |
| - ->will($this->returnValue('property')); |
| 108 | + $constraint = new Expression('value == this.data'); |
154 | 109 |
|
155 |
| - $this->context->expects($this->any()) |
156 |
| - ->method('getPropertyPath') |
157 |
| - ->will($this->returnValue('nested.property')); |
| 110 | + $object = new Entity(); |
| 111 | + $object->data = '1'; |
158 | 112 |
|
159 |
| - $this->context->expects($this->any()) |
160 |
| - ->method('getRoot') |
161 |
| - ->will($this->returnValue($root)); |
| 113 | + $root = new Entity(); |
| 114 | + $root->reference = $object; |
162 | 115 |
|
163 |
| - $this->context->expects($this->never()) |
164 |
| - ->method('addViolation'); |
| 116 | + $this->setRoot($root); |
| 117 | + $this->setPropertyPath('reference.data'); |
| 118 | + $this->setProperty($object, 'data'); |
165 | 119 |
|
166 | 120 | $this->validator->validate('1', $constraint);
|
| 121 | + |
| 122 | + $this->assertNoViolation(); |
167 | 123 | }
|
168 | 124 |
|
169 | 125 | public function testFailingExpressionAtNestedPropertyLevel()
|
170 | 126 | {
|
171 | 127 | $constraint = new Expression(array(
|
172 |
| - 'expression' => 'value == this.expected', |
| 128 | + 'expression' => 'value == this.data', |
173 | 129 | 'message' => 'myMessage',
|
174 | 130 | ));
|
175 | 131 |
|
176 |
| - $object = (object) array('expected' => '1'); |
177 |
| - $root = (object) array('nested' => $object); |
| 132 | + $object = new Entity(); |
| 133 | + $object->data = '1'; |
| 134 | + |
| 135 | + $root = new Entity(); |
| 136 | + $root->reference = $object; |
| 137 | + |
| 138 | + $this->setRoot($root); |
| 139 | + $this->setPropertyPath('reference.data'); |
| 140 | + $this->setProperty($object, 'data'); |
| 141 | + |
| 142 | + $this->validator->validate('2', $constraint); |
| 143 | + |
| 144 | + $this->assertViolation('myMessage', array(), 'reference.data'); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * When validatePropertyValue() is called with a class name |
| 149 | + * https://github.com/symfony/symfony/pull/11498 |
| 150 | + */ |
| 151 | + public function testSucceedingExpressionAtPropertyLevelWithoutRoot() |
| 152 | + { |
| 153 | + $constraint = new Expression('value == "1"'); |
178 | 154 |
|
179 |
| - $this->context->expects($this->any()) |
180 |
| - ->method('getPropertyName') |
181 |
| - ->will($this->returnValue('property')); |
| 155 | + $this->setRoot('1'); |
| 156 | + $this->setPropertyPath(''); |
| 157 | + $this->setProperty(null, 'property'); |
182 | 158 |
|
183 |
| - $this->context->expects($this->any()) |
184 |
| - ->method('getPropertyPath') |
185 |
| - ->will($this->returnValue('nested.property')); |
| 159 | + $this->validator->validate('1', $constraint); |
| 160 | + |
| 161 | + $this->assertNoViolation(); |
| 162 | + } |
186 | 163 |
|
187 |
| - $this->context->expects($this->any()) |
188 |
| - ->method('getRoot') |
189 |
| - ->will($this->returnValue($root)); |
| 164 | + /** |
| 165 | + * When validatePropertyValue() is called with a class name |
| 166 | + * https://github.com/symfony/symfony/pull/11498 |
| 167 | + */ |
| 168 | + public function testFailingExpressionAtPropertyLevelWithoutRoot() |
| 169 | + { |
| 170 | + $constraint = new Expression(array( |
| 171 | + 'expression' => 'value == "1"', |
| 172 | + 'message' => 'myMessage', |
| 173 | + )); |
190 | 174 |
|
191 |
| - $this->context->expects($this->once()) |
192 |
| - ->method('addViolation') |
193 |
| - ->with('myMessage'); |
| 175 | + $this->setRoot('2'); |
| 176 | + $this->setPropertyPath(''); |
| 177 | + $this->setProperty(null, 'property'); |
194 | 178 |
|
195 | 179 | $this->validator->validate('2', $constraint);
|
| 180 | + |
| 181 | + $this->assertViolation('myMessage', array(), ''); |
196 | 182 | }
|
197 | 183 | }
|
0 commit comments