Skip to content

Commit b2de577

Browse files
committed
[Validator] Backported constraint validator tests from 2.5
1 parent 52bc840 commit b2de577

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1447
-1533
lines changed

Constraints/AllValidator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public function validate($value, Constraint $constraint)
3838
$group = $this->context->getGroup();
3939

4040
foreach ($value as $key => $element) {
41-
foreach ($constraint->constraints as $constr) {
42-
$this->context->validateValue($element, $constr, '['.$key.']', $group);
43-
}
41+
$this->context->validateValue($element, $constraint->constraints, '['.$key.']', $group);
4442
}
4543
}
4644
}

Constraints/ChoiceValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public function validate($value, Constraint $constraint)
7070
if ($constraint->min !== null && $count < $constraint->min) {
7171
$this->context->addViolation($constraint->minMessage, array(
7272
'{{ limit }}' => $constraint->min
73-
), null, (int) $constraint->min);
73+
), $value, (int) $constraint->min);
7474

7575
return;
7676
}
7777

7878
if ($constraint->max !== null && $count > $constraint->max) {
7979
$this->context->addViolation($constraint->maxMessage, array(
8080
'{{ limit }}' => $constraint->max
81-
), null, (int) $constraint->max);
81+
), $value, (int) $constraint->max);
8282

8383
return;
8484
}

Constraints/CollectionValidator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public function validate($value, Constraint $constraint)
4343
(is_array($value) && array_key_exists($field, $value)) ||
4444
($value instanceof \ArrayAccess && $value->offsetExists($field))
4545
) {
46-
foreach ($fieldConstraint->constraints as $constr) {
47-
$this->context->validateValue($value[$field], $constr, '['.$field.']', $group);
48-
}
46+
$this->context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']', $group);
4947
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
5048
$this->context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
5149
'{{ field }}' => $this->formatValue($field)

Tests/Constraints/AbstractComparisonValidatorTestCase.php

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Intl\Util\IntlTestHelper;
1515
use Symfony\Component\Validator\Constraint;
16-
use Symfony\Component\Validator\Constraints\AbstractComparisonValidator;
1716

1817
class ComparisonTest_Class
1918
{
@@ -33,32 +32,15 @@ public function __toString()
3332
/**
3433
* @author Daniel Holmes <daniel@danielholmes.org>
3534
*/
36-
abstract class AbstractComparisonValidatorTestCase extends \PHPUnit_Framework_TestCase
35+
abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintValidatorTest
3736
{
38-
private $validator;
39-
private $context;
40-
41-
protected function setUp()
42-
{
43-
$this->validator = $this->createValidator();
44-
$this->context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')
45-
->disableOriginalConstructor()
46-
->getMock();
47-
$this->validator->initialize($this->context);
48-
49-
\Locale::setDefault('en');
50-
}
51-
5237
/**
53-
* @return AbstractComparisonValidator
38+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
5439
*/
55-
abstract protected function createValidator();
56-
5740
public function testThrowsConstraintExceptionIfNoValueOrProperty()
5841
{
59-
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
60-
6142
$comparison = $this->createConstraint(array());
43+
6244
$this->validator->validate('some value', $comparison);
6345
}
6446

@@ -69,16 +51,11 @@ public function testThrowsConstraintExceptionIfNoValueOrProperty()
6951
*/
7052
public function testValidComparisonToValue($dirtyValue, $comparisonValue)
7153
{
72-
$this->context->expects($this->never())
73-
->method('addViolation');
74-
7554
$constraint = $this->createConstraint(array('value' => $comparisonValue));
7655

77-
$this->context->expects($this->any())
78-
->method('getPropertyPath')
79-
->will($this->returnValue('property1'));
80-
8156
$this->validator->validate($dirtyValue, $constraint);
57+
58+
$this->assertNoViolation();
8259
}
8360

8461
/**
@@ -105,19 +82,13 @@ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $
10582
$constraint = $this->createConstraint(array('value' => $comparedValue));
10683
$constraint->message = 'Constraint Message';
10784

108-
$this->context->expects($this->any())
109-
->method('getPropertyPath')
110-
->will($this->returnValue('property1'));
111-
112-
$this->context->expects($this->once())
113-
->method('addViolation')
114-
->with('Constraint Message', array(
115-
'{{ value }}' => $dirtyValueAsString,
116-
'{{ compared_value }}' => $comparedValueString,
117-
'{{ compared_value_type }}' => $comparedValueType
118-
));
119-
12085
$this->validator->validate($dirtyValue, $constraint);
86+
87+
$this->assertViolation('Constraint Message', array(
88+
'{{ value }}' => $dirtyValueAsString,
89+
'{{ compared_value }}' => $comparedValueString,
90+
'{{ compared_value_type }}' => $comparedValueType
91+
));
12192
}
12293

12394
/**
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use Symfony\Component\Validator\ConstraintValidatorInterface;
15+
use Symfony\Component\Validator\ConstraintViolation;
16+
use Symfony\Component\Validator\Context\ExecutionContext;
17+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
18+
use Symfony\Component\Validator\Mapping\ClassMetadata;
19+
use Symfony\Component\Validator\Mapping\PropertyMetadata;
20+
use Symfony\Component\Validator\Tests\Fixtures\StubGlobalExecutionContext;
21+
22+
/**
23+
* @since 2.5.3
24+
* @author Bernhard Schussek <bschussek@gmail.com>
25+
*/
26+
abstract class AbstractConstraintValidatorTest extends \PHPUnit_Framework_TestCase
27+
{
28+
/**
29+
* @var ExecutionContextInterface
30+
*/
31+
protected $context;
32+
33+
/**
34+
* @var ConstraintValidatorInterface
35+
*/
36+
protected $validator;
37+
38+
protected $group;
39+
40+
protected $metadata;
41+
42+
protected $object;
43+
44+
protected $value;
45+
46+
protected $root;
47+
48+
protected $propertyPath;
49+
50+
protected function setUp()
51+
{
52+
$this->group = 'MyGroup';
53+
$this->metadata = null;
54+
$this->object = null;
55+
$this->value = 'InvalidValue';
56+
$this->root = 'root';
57+
$this->propertyPath = 'property.path';
58+
$this->context = $this->createContext();
59+
$this->validator = $this->createValidator();
60+
$this->validator->initialize($this->context);
61+
62+
\Locale::setDefault('en');
63+
}
64+
65+
protected function createContext()
66+
{
67+
$translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
68+
69+
return $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')
70+
->setConstructorArgs(array(
71+
new StubGlobalExecutionContext($this->root),
72+
$translator,
73+
null,
74+
$this->metadata,
75+
$this->value,
76+
$this->group,
77+
$this->propertyPath
78+
))
79+
->setMethods(array('validate', 'validateValue'))
80+
->getMock();
81+
}
82+
83+
protected function createViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null)
84+
{
85+
return new ConstraintViolation(
86+
null,
87+
$message,
88+
$parameters,
89+
$this->root,
90+
$propertyPath,
91+
$invalidValue,
92+
$plural,
93+
$code
94+
);
95+
}
96+
97+
protected function setGroup($group)
98+
{
99+
$this->group = $group;
100+
$this->context = $this->createContext();
101+
$this->validator->initialize($this->context);
102+
}
103+
104+
protected function setObject($object)
105+
{
106+
$this->object = $object;
107+
$this->metadata = is_object($object)
108+
? new ClassMetadata(get_class($object))
109+
: null;
110+
$this->context = $this->createContext();
111+
$this->validator->initialize($this->context);
112+
}
113+
114+
protected function setProperty($object, $property)
115+
{
116+
$this->object = $object;
117+
$this->metadata = is_object($object)
118+
? new PropertyMetadata(get_class($object), $property)
119+
: null;
120+
$this->context = $this->createContext();
121+
$this->validator->initialize($this->context);
122+
}
123+
124+
protected function setValue($value)
125+
{
126+
$this->value = $value;
127+
$this->context = $this->createContext();
128+
$this->validator->initialize($this->context);
129+
}
130+
131+
protected function setRoot($root)
132+
{
133+
$this->root = $root;
134+
$this->context = $this->createContext();
135+
$this->validator->initialize($this->context);
136+
}
137+
138+
protected function setPropertyPath($propertyPath)
139+
{
140+
$this->propertyPath = $propertyPath;
141+
$this->context = $this->createContext();
142+
$this->validator->initialize($this->context);
143+
}
144+
145+
protected function expectNoValidate()
146+
{
147+
$this->context->expects($this->never())
148+
->method('validate');
149+
$this->context->expects($this->never())
150+
->method('validateValue');
151+
}
152+
153+
protected function expectValidateAt($i, $propertyPath, $value, $group)
154+
{
155+
$this->context->expects($this->at($i))
156+
->method('validate')
157+
->with($value, $propertyPath, $group);
158+
}
159+
160+
protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group)
161+
{
162+
$this->context->expects($this->at($i))
163+
->method('validateValue')
164+
->with($value, $constraints, $propertyPath, $group);
165+
}
166+
167+
protected function assertNoViolation()
168+
{
169+
$this->assertCount(0, $this->context->getViolations());
170+
}
171+
172+
protected function assertViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null)
173+
{
174+
$violations = $this->context->getViolations();
175+
176+
$this->assertCount(1, $violations);
177+
$this->assertEquals($this->createViolation($message, $parameters, $propertyPath, $invalidValue, $plural, $code), $violations[0]);
178+
}
179+
180+
protected function assertViolations(array $expected)
181+
{
182+
$violations = $this->context->getViolations();
183+
184+
$this->assertCount(count($expected), $violations);
185+
186+
$i = 0;
187+
188+
foreach ($expected as $violation) {
189+
$this->assertEquals($violation, $violations[$i++]);
190+
}
191+
}
192+
193+
abstract protected function createValidator();
194+
}

0 commit comments

Comments
 (0)