Skip to content

Commit e3c01ad

Browse files
committed
[Validator] Backported constraint validator tests from 2.5
1 parent e7a0fbf commit e3c01ad

File tree

2 files changed

+92
-105
lines changed

2 files changed

+92
-105
lines changed

Constraints/ExpressionValidator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function validate($value, Constraint $constraint)
5151
$variables = array();
5252

5353
if (null === $this->context->getPropertyName()) {
54+
$variables['value'] = $value;
5455
$variables['this'] = $value;
5556
} else {
5657
// Extract the object that the property belongs to from the object

Tests/Constraints/ExpressionValidatorTest.php

Lines changed: 91 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -14,184 +14,170 @@
1414
use Symfony\Component\PropertyAccess\PropertyAccess;
1515
use Symfony\Component\Validator\Constraints\Expression;
1616
use Symfony\Component\Validator\Constraints\ExpressionValidator;
17+
use Symfony\Component\Validator\Tests\Fixtures\Entity;
1718

18-
class ExpressionValidatorTest extends \PHPUnit_Framework_TestCase
19+
class ExpressionValidatorTest extends AbstractConstraintValidatorTest
1920
{
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()
3522
{
36-
$this->context = null;
37-
$this->validator = null;
23+
return new ExpressionValidator(PropertyAccess::createPropertyAccessor());
3824
}
3925

4026
public function testNullIsValid()
4127
{
42-
$this->context->expects($this->never())
43-
->method('addViolation');
44-
4528
$this->validator->validate(null, new Expression('value == 1'));
29+
30+
$this->assertNoViolation();
4631
}
4732

4833
public function testEmptyStringIsValid()
4934
{
50-
$this->context->expects($this->never())
51-
->method('addViolation');
52-
5335
$this->validator->validate('', new Expression('value == 1'));
36+
37+
$this->assertNoViolation();
5438
}
5539

5640
public function testSucceedingExpressionAtObjectLevel()
5741
{
58-
$constraint = new Expression('this.property == 1');
59-
60-
$object = (object) array('property' => '1');
42+
$constraint = new Expression('this.data == 1');
6143

62-
$this->context->expects($this->any())
63-
->method('getPropertyName')
64-
->will($this->returnValue(null));
44+
$object = new Entity();
45+
$object->data = '1';
6546

66-
$this->context->expects($this->never())
67-
->method('addViolation');
47+
$this->setObject($object);
6848

6949
$this->validator->validate($object, $constraint);
50+
51+
$this->assertNoViolation();
7052
}
7153

7254
public function testFailingExpressionAtObjectLevel()
7355
{
7456
$constraint = new Expression(array(
75-
'expression' => 'this.property == 1',
57+
'expression' => 'this.data == 1',
7658
'message' => 'myMessage',
7759
));
7860

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';
8463

85-
$this->context->expects($this->once())
86-
->method('addViolation')
87-
->with('myMessage');
64+
$this->setObject($object);
8865

8966
$this->validator->validate($object, $constraint);
67+
68+
$this->assertViolation('myMessage');
9069
}
9170

9271
public function testSucceedingExpressionAtPropertyLevel()
9372
{
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');
10174

102-
$this->context->expects($this->any())
103-
->method('getPropertyPath')
104-
->will($this->returnValue('property'));
75+
$object = new Entity();
76+
$object->data = '1';
10577

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');
11281

11382
$this->validator->validate('1', $constraint);
83+
84+
$this->assertNoViolation();
11485
}
11586

11687
public function testFailingExpressionAtPropertyLevel()
11788
{
11889
$constraint = new Expression(array(
119-
'expression' => 'value == this.expected',
90+
'expression' => 'value == this.data',
12091
'message' => 'myMessage',
12192
));
12293

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';
13696

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');
140100

141101
$this->validator->validate('2', $constraint);
102+
103+
$this->assertViolation('myMessage', array(), 'data');
142104
}
143105

144106
public function testSucceedingExpressionAtNestedPropertyLevel()
145107
{
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');
154109

155-
$this->context->expects($this->any())
156-
->method('getPropertyPath')
157-
->will($this->returnValue('nested.property'));
110+
$object = new Entity();
111+
$object->data = '1';
158112

159-
$this->context->expects($this->any())
160-
->method('getRoot')
161-
->will($this->returnValue($root));
113+
$root = new Entity();
114+
$root->reference = $object;
162115

163-
$this->context->expects($this->never())
164-
->method('addViolation');
116+
$this->setRoot($root);
117+
$this->setPropertyPath('reference.data');
118+
$this->setProperty($object, 'data');
165119

166120
$this->validator->validate('1', $constraint);
121+
122+
$this->assertNoViolation();
167123
}
168124

169125
public function testFailingExpressionAtNestedPropertyLevel()
170126
{
171127
$constraint = new Expression(array(
172-
'expression' => 'value == this.expected',
128+
'expression' => 'value == this.data',
173129
'message' => 'myMessage',
174130
));
175131

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"');
178154

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');
182158

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+
}
186163

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+
));
190174

191-
$this->context->expects($this->once())
192-
->method('addViolation')
193-
->with('myMessage');
175+
$this->setRoot('2');
176+
$this->setPropertyPath('');
177+
$this->setProperty(null, 'property');
194178

195179
$this->validator->validate('2', $constraint);
180+
181+
$this->assertViolation('myMessage', array(), '');
196182
}
197183
}

0 commit comments

Comments
 (0)