Skip to content

Commit e7a0fbf

Browse files
committed
Merge branch '2.3' into 2.4
* 2.3: [Validator] Backported constraint validator tests from 2.5 Fix toolbar vertical alignment. [HttpFoundation] MongoDbSessionHandler supports auto expiry via configurable expiry_field [FrameworkBundle] add missing attribute to XSD Allow basic auth in url. Improve regex. Add tests. fix typos and syntax in Profiler controller method comments remove volatile tests [Console] fixed style creation when providing an unknown tag option [Validator] Convert objects to string in comparison validators. Reapplies 6cf5e0812e6f20d60acbc0324abf96475e89b6ef [HttpFoundation] Update QUERY_STRING when overrideGlobals Conflicts: src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php src/Symfony/Component/HttpFoundation/Tests/RequestTest.php src/Symfony/Component/Validator/Constraints/AllValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php
2 parents e35fb37 + b2de577 commit e7a0fbf

Some content is hidden

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

55 files changed

+1500
-1603
lines changed

ConstraintValidator.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@
2020
*/
2121
abstract class ConstraintValidator implements ConstraintValidatorInterface
2222
{
23+
/**
24+
* Whether to format {@link \DateTime} objects as RFC-3339 dates
25+
* ("Y-m-d H:i:s").
26+
*
27+
* @var integer
28+
*/
29+
const PRETTY_DATE = 1;
30+
31+
/**
32+
* Whether to cast objects with a "__toString()" method to strings.
33+
*
34+
* @var integer
35+
*/
36+
const OBJECT_TO_STRING = 2;
37+
2338
/**
2439
* @var ExecutionContextInterface
2540
*/
@@ -66,15 +81,15 @@ protected function formatTypeOf($value)
6681
* won't know what an "object", "array" or "resource" is and will be
6782
* confused by the violation message.
6883
*
69-
* @param mixed $value The value to format as string
70-
* @param bool $prettyDateTime Whether to format {@link \DateTime}
71-
* objects as RFC-3339 dates ("Y-m-d H:i:s")
84+
* @param mixed $value The value to format as string
85+
* @param integer $format A bitwise combination of the format
86+
* constants in this class
7287
*
7388
* @return string The string representation of the passed value
7489
*/
75-
protected function formatValue($value, $prettyDateTime = false)
90+
protected function formatValue($value, $format = 0)
7691
{
77-
if ($prettyDateTime && $value instanceof \DateTime) {
92+
if (($format & self::PRETTY_DATE) && $value instanceof \DateTime) {
7893
if (class_exists('IntlDateFormatter')) {
7994
$locale = \Locale::getDefault();
8095
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
@@ -86,6 +101,10 @@ protected function formatValue($value, $prettyDateTime = false)
86101
}
87102

88103
if (is_object($value)) {
104+
if ($format & self::OBJECT_TO_STRING && method_exists($value, '__toString')) {
105+
return $value->__toString();
106+
}
107+
89108
return 'object';
90109
}
91110

@@ -122,18 +141,18 @@ protected function formatValue($value, $prettyDateTime = false)
122141
* Each of the values is converted to a string using
123142
* {@link formatValue()}. The values are then concatenated with commas.
124143
*
125-
* @param array $values A list of values
126-
* @param bool $prettyDateTime Whether to format {@link \DateTime}
127-
* objects as RFC-3339 dates ("Y-m-d H:i:s")
144+
* @param array $values A list of values
145+
* @param integer $format A bitwise combination of the format
146+
* constants in this class
128147
*
129148
* @return string The string representation of the value list
130149
*
131150
* @see formatValue()
132151
*/
133-
protected function formatValues(array $values, $prettyDateTime = false)
152+
protected function formatValues(array $values, $format = 0)
134153
{
135154
foreach ($values as $key => $value) {
136-
$values[$key] = $this->formatValue($value, $prettyDateTime);
155+
$values[$key] = $this->formatValue($value, $format);
137156
}
138157

139158
return implode(', ', $values);

Constraints/AbstractComparisonValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function validate($value, Constraint $constraint)
3232

3333
if (!$this->compareValues($value, $constraint->value)) {
3434
$this->context->addViolation($constraint->message, array(
35-
'{{ value }}' => $this->formatValue($value, true),
36-
'{{ compared_value }}' => $this->formatValue($constraint->value, true),
35+
'{{ value }}' => $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
36+
'{{ compared_value }}' => $this->formatValue($constraint->value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
3737
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value)
3838
));
3939
}

Constraints/AllValidator.php

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

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

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
@@ -53,9 +53,7 @@ public function validate($value, Constraint $constraint)
5353
(is_array($value) && array_key_exists($field, $value)) ||
5454
($value instanceof \ArrayAccess && $value->offsetExists($field))
5555
) {
56-
foreach ($fieldConstraint->constraints as $constr) {
57-
$context->validateValue($value[$field], $constr, '['.$field.']', $group);
58-
}
56+
$context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']', $group);
5957
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
6058
$context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
6159
'{{ field }}' => $this->formatValue($field)

Constraints/UrlValidator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class UrlValidator extends ConstraintValidator
2424
{
2525
const PATTERN = '~^
2626
(%s):// # protocol
27+
(([\pL\pN-]+:)?([\pL\pN-]+)@)? # basic auth
2728
(
2829
([\pL\pN\pS-]+\.)+([\pL]|xn\-\-[\pL\pN-]+)+ # a domain name
2930
| # or

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 <[email protected]>
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
/**

0 commit comments

Comments
 (0)