Skip to content

Commit 3b38440

Browse files
committed
Merge branch '2.4' into 2.5
* 2.4: [Validator] Backported constraint validator tests from 2.5 [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/UniqueEntityValidatorTest.php src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php src/Symfony/Component/Security/Core/Tests/Validator/Constraints/UserPasswordValidatorTest.php src/Symfony/Component/Validator/Constraints/AllValidator.php src/Symfony/Component/Validator/Constraints/ChoiceValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Constraints/ExpressionValidator.php src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FalseValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/GroupSequenceTest.php src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LanguageValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NotNullValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NullValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TrueValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php src/Symfony/Component/Validator/Tests/Fixtures/StubGlobalExecutionContext.php
2 parents d70c9aa + e3c01ad commit 3b38440

12 files changed

+97
-50
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
@@ -37,8 +37,8 @@ public function validate($value, Constraint $constraint)
3737

3838
if (!$this->compareValues($value, $constraint->value)) {
3939
$this->context->addViolation($constraint->message, array(
40-
'{{ value }}' => $this->formatValue($value, true),
41-
'{{ compared_value }}' => $this->formatValue($constraint->value, true),
40+
'{{ value }}' => $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
41+
'{{ compared_value }}' => $this->formatValue($constraint->value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
4242
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value)
4343
));
4444
}

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/EqualToValidatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function provideInvalidComparisons()
5959
array(1, '1', 2, '2', 'integer'),
6060
array('22', '"22"', '333', '"333"', 'string'),
6161
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
62-
array(new ComparisonTest_Class(4), 'object', new ComparisonTest_Class(5), 'object', __NAMESPACE__.'\ComparisonTest_Class'), );
62+
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
63+
);
6364
}
6465
}

Tests/Constraints/FileValidatorTest.php

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,36 +95,54 @@ public function testValidUploadedfile()
9595

9696
public function provideMaxSizeExceededTests()
9797
{
98-
return array(
99-
array(11, 10, '11', '10', 'bytes'),
100-
101-
array(ceil(1.005*1000), ceil(1.005*1000) - 1, '1005', '1004', 'bytes'),
102-
array(ceil(1.005*1000*1000), ceil(1.005*1000*1000) - 1, '1005000', '1004999', 'bytes'),
98+
// We have various interesting limit - size combinations to test.
99+
// Assume a limit of 1000 bytes (1 kB). Then the following table
100+
// lists the violation messages for different file sizes:
103101

104-
// round(size) == 1.01kB, limit == 1kB
105-
array(ceil(1.005*1000), 1000, '1.01', '1', 'kB'),
106-
array(ceil(1.005*1000), '1k', '1.01', '1', 'kB'),
102+
// -----------+--------------------------------------------------------
103+
// Size | Violation Message
104+
// -----------+--------------------------------------------------------
105+
// 1000 bytes | No violation
106+
// 1001 bytes | "Size of 1001 bytes exceeded limit of 1000 bytes"
107+
// 1004 bytes | "Size of 1004 bytes exceeded limit of 1000 bytes"
108+
// | NOT: "Size of 1 kB exceeded limit of 1 kB"
109+
// 1005 bytes | "Size of 1.01 kB exceeded limit of 1 kB"
110+
// -----------+--------------------------------------------------------
107111

108-
// round(size) == 1kB, limit == 1kB -> use bytes
109-
array(ceil(1.004*1000), 1000, '1004', '1000', 'bytes'),
110-
array(ceil(1.004*1000), '1k', '1004', '1000', 'bytes'),
112+
// As you see, we have two interesting borders:
111113

112-
array(1000 + 1, 1000, '1001', '1000', 'bytes'),
113-
array(1000 + 1, '1k', '1001', '1000', 'bytes'),
114+
// 1000/1001 - The border as of which a violation occurs
115+
// 1004/1005 - The border as of which the message can be rounded to kB
114116

115-
// round(size) == 1.01MB, limit == 1MB
116-
array(ceil(1.005*1000*1000), 1000*1000, '1.01', '1', 'MB'),
117-
array(ceil(1.005*1000*1000), '1000k', '1.01', '1', 'MB'),
118-
array(ceil(1.005*1000*1000), '1M', '1.01', '1', 'MB'),
117+
// Analogous for kB/MB.
119118

120-
// round(size) == 1MB, limit == 1MB -> use kB
121-
array(ceil(1.004*1000*1000), 1000*1000, '1004', '1000', 'kB'),
122-
array(ceil(1.004*1000*1000), '1000k', '1004', '1000', 'kB'),
123-
array(ceil(1.004*1000*1000), '1M', '1004', '1000', 'kB'),
119+
// Prior to Symfony 2.5, violation messages are always displayed in the
120+
// same unit used to specify the limit.
124121

125-
array(1000*1000 + 1, 1000*1000, '1000001', '1000000', 'bytes'),
126-
array(1000*1000 + 1, '1000k', '1000001', '1000000', 'bytes'),
127-
array(1000*1000 + 1, '1M', '1000001', '1000000', 'bytes'),
122+
// As of Symfony 2.5, the above logic is implemented.
123+
return array(
124+
// limit in bytes
125+
array(1001, 1000, '1001', '1000', 'bytes'),
126+
array(1004, 1000, '1004', '1000', 'bytes'),
127+
array(1005, 1000, '1.01', '1', 'kB'),
128+
129+
array(1000001, 1000000, '1000001', '1000000', 'bytes'),
130+
array(1004999, 1000000, '1005', '1000', 'kB'),
131+
array(1005000, 1000000, '1.01', '1', 'MB'),
132+
133+
// limit in kB
134+
array(1001, '1k', '1001', '1000', 'bytes'),
135+
array(1004, '1k', '1004', '1000', 'bytes'),
136+
array(1005, '1k', '1.01', '1', 'kB'),
137+
138+
array(1000001, '1000k', '1000001', '1000000', 'bytes'),
139+
array(1004999, '1000k', '1005', '1000', 'kB'),
140+
array(1005000, '1000k', '1.01', '1', 'MB'),
141+
142+
// limit in MB
143+
array(1000001, '1M', '1000001', '1000000', 'bytes'),
144+
array(1004999, '1M', '1005', '1000', 'kB'),
145+
array(1005000, '1M', '1.01', '1', 'MB'),
128146
);
129147
}
130148

@@ -155,14 +173,16 @@ public function testMaxSizeExceeded($bytesWritten, $limit, $sizeAsString, $limit
155173
public function provideMaxSizeNotExceededTests()
156174
{
157175
return array(
158-
array(10, 10),
159-
array(9, 10),
176+
// limit in bytes
177+
array(1000, 1000),
178+
array(1000000, 1000000),
160179

180+
// limit in kB
161181
array(1000, '1k'),
162-
array(1000 - 1, '1k'),
182+
array(1000000, '1000k'),
163183

164-
array(1000*1000, '1M'),
165-
array(1000*1000 - 1, '1M'),
184+
// limit in MB
185+
array(1000000, '1M'),
166186
);
167187
}
168188

Tests/Constraints/GreaterThanValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public function provideInvalidComparisons()
5959
array(2, '2', 2, '2', 'integer'),
6060
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2005/01/01'), 'Jan 1, 2005, 12:00 AM', 'DateTime'),
6161
array(new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
62-
array(new ComparisonTest_Class(5), 'object', new ComparisonTest_Class(5), 'object', __NAMESPACE__.'\ComparisonTest_Class'),
63-
array(new ComparisonTest_Class(5), 'object', new ComparisonTest_Class(5), 'object', __NAMESPACE__.'\ComparisonTest_Class'),
62+
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
63+
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
6464
array('22', '"22"', '333', '"333"', 'string'),
6565
array('22', '"22"', '22', '"22"', 'string')
6666
);

Tests/Constraints/IdenticalToValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function provideInvalidComparisons()
6363
array('22', '"22"', '333', '"333"', 'string'),
6464
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', 'DateTime'),
6565
array(new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('1999-01-01'), 'Jan 1, 1999, 12:00 AM', 'DateTime'),
66-
array(new ComparisonTest_Class(4), 'object', new ComparisonTest_Class(5), 'object', __NAMESPACE__.'\ComparisonTest_Class'),
66+
array(new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
6767
);
6868
}
6969
}

Tests/Constraints/LessThanOrEqualValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function provideInvalidComparisons()
6161
return array(
6262
array(2, '2', 1, '1', 'integer'),
6363
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
64-
array(new ComparisonTest_Class(5), 'object', new ComparisonTest_Class(4), 'object', __NAMESPACE__.'\ComparisonTest_Class'),
64+
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(4), '4', __NAMESPACE__.'\ComparisonTest_Class'),
6565
array('c', '"c"', 'b', '"b"', 'string')
6666
);
6767
}

Tests/Constraints/LessThanValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public function provideInvalidComparisons()
5959
array(2, '2', 2, '2', 'integer'),
6060
array(new \DateTime('2010-01-01'), 'Jan 1, 2010, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
6161
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
62-
array(new ComparisonTest_Class(5), 'object', new ComparisonTest_Class(5), 'object', __NAMESPACE__.'\ComparisonTest_Class'),
63-
array(new ComparisonTest_Class(6), 'object', new ComparisonTest_Class(5), 'object', __NAMESPACE__.'\ComparisonTest_Class'),
62+
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
63+
array(new ComparisonTest_Class(6), '6', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
6464
array('333', '"333"', '22', '"22"', 'string'),
6565
);
6666
}

Tests/Constraints/NotEqualToValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function provideInvalidComparisons()
5959
array('2', '"2"', 2, '2', 'integer'),
6060
array('a', '"a"', 'a', '"a"', 'string'),
6161
array(new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', new \DateTime('2000-01-01'), 'Jan 1, 2000, 12:00 AM', 'DateTime'),
62-
array(new ComparisonTest_Class(5), 'object', new ComparisonTest_Class(5), 'object', __NAMESPACE__.'\ComparisonTest_Class'),
62+
array(new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'),
6363
);
6464
}
6565
}

Tests/Constraints/NotIdenticalToValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function provideInvalidComparisons()
6262
array(3, '3', 3, '3', 'integer'),
6363
array('a', '"a"', 'a', '"a"', 'string'),
6464
array($date, 'Jan 1, 2000, 12:00 AM', $date, 'Jan 1, 2000, 12:00 AM', 'DateTime'),
65-
array($object, 'object', $object, 'object', __NAMESPACE__.'\ComparisonTest_Class'),
65+
array($object, '2', $object, '2', __NAMESPACE__.'\ComparisonTest_Class'),
6666
);
6767
}
6868
}

Tests/Constraints/UrlValidatorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public function getValidUrls()
108108
array('http://xn--espaa-rta.xn--ca-ol-fsay5a/'),
109109
array('http://xn--d1abbgf6aiiy.xn--p1ai/'),
110110
array('http://☎.com/'),
111+
array('http://username:password@symfony.com'),
112+
array('http://user-name@symfony.com'),
111113
);
112114
}
113115

@@ -145,6 +147,10 @@ public function getInvalidUrls()
145147
array('ftp://[::1]/'),
146148
array('http://[::1'),
147149
array('http://hello.☎/'),
150+
array('http://:password@symfony.com'),
151+
array('http://:password@@symfony.com'),
152+
array('http://username:passwordsymfony.com'),
153+
array('http://usern@me:password@symfony.com'),
148154
);
149155
}
150156

0 commit comments

Comments
 (0)