Skip to content

Commit ba711a6

Browse files
Merge branch '7.0' into 7.1
* 7.0: [Console][PhpUnitBridge][VarDumper] Fix `NO_COLOR` empty value handling [Translation] Fix CSV escape char in `CsvFileLoader` on PHP >= 7.4 [DoctrineBridge] fix messenger bus dispatch inside an active transaction [HttpFoundation] Add tests for uncovered sections treat uninitialized properties referenced by property paths as null properly set up constraint options [ErrorHandler][VarDumper] Remove PHP 8.4 deprecations move adding detailed JSON error messages to the validate phase [Profiler] Add word wrap in tables in dialog to see all the text [Core] Fix & Enhance security arabic translation. [HttpFoundation] Add tests for `MethodRequestMatcher` and `SchemeRequestMatcher`
2 parents 9f46936 + b3e4d83 commit ba711a6

13 files changed

+209
-14
lines changed

Constraints/AbstractComparisonValidator.php

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
15+
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1516
use Symfony\Component\PropertyAccess\PropertyAccess;
1617
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1718
use Symfony\Component\Validator\Constraint;
@@ -53,6 +54,8 @@ public function validate(mixed $value, Constraint $constraint): void
5354
$comparedValue = $this->getPropertyAccessor()->getValue($object, $path);
5455
} catch (NoSuchPropertyException $e) {
5556
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $path, get_debug_type($constraint)).$e->getMessage(), 0, $e);
57+
} catch (UninitializedPropertyException) {
58+
$comparedValue = null;
5659
}
5760
} else {
5861
$comparedValue = $constraint->value;

Constraints/BicValidator.php

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Intl\Countries;
1515
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
16+
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1617
use Symfony\Component\PropertyAccess\PropertyAccess;
1718
use Symfony\Component\PropertyAccess\PropertyAccessor;
1819
use Symfony\Component\Validator\Constraint;
@@ -127,6 +128,8 @@ public function validate(mixed $value, Constraint $constraint): void
127128
$iban = $this->getPropertyAccessor()->getValue($object, $path);
128129
} catch (NoSuchPropertyException $e) {
129130
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $path, get_debug_type($constraint)).$e->getMessage(), 0, $e);
131+
} catch (UninitializedPropertyException) {
132+
$iban = null;
130133
}
131134
}
132135
if (!$iban) {

Constraints/RangeValidator.php

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
15+
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1516
use Symfony\Component\PropertyAccess\PropertyAccess;
1617
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1718
use Symfony\Component\Validator\Constraint;
@@ -159,6 +160,8 @@ private function getLimit(?string $propertyPath, mixed $default, Constraint $con
159160
return $this->getPropertyAccessor()->getValue($object, $propertyPath);
160161
} catch (NoSuchPropertyException $e) {
161162
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $propertyPath, get_debug_type($constraint)).$e->getMessage(), 0, $e);
163+
} catch (UninitializedPropertyException) {
164+
return null;
162165
}
163166
}
164167

Tests/Constraints/AbstractComparisonValidatorTestCase.php

+26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Constraints\AbstractComparison;
1717
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1818
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
19+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\TypedDummy;
1920

2021
class ComparisonTest_Class
2122
{
@@ -263,6 +264,31 @@ public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsS
263264
}
264265
}
265266

267+
/**
268+
* @dataProvider provideComparisonsToNullValueAtPropertyPath
269+
*/
270+
public function testCompareWithUninitializedPropertyAtPropertyPath($dirtyValue, $dirtyValueAsString, $isValid)
271+
{
272+
$this->setObject(new TypedDummy());
273+
274+
$this->validator->validate($dirtyValue, $this->createConstraint([
275+
'message' => 'Constraint Message',
276+
'propertyPath' => 'value',
277+
]));
278+
279+
if ($isValid) {
280+
$this->assertNoViolation();
281+
} else {
282+
$this->buildViolation('Constraint Message')
283+
->setParameter('{{ value }}', $dirtyValueAsString)
284+
->setParameter('{{ compared_value }}', 'null')
285+
->setParameter('{{ compared_value_type }}', 'null')
286+
->setParameter('{{ compared_value_path }}', 'value')
287+
->setCode($this->getErrorCode())
288+
->assertRaised();
289+
}
290+
}
291+
266292
public static function provideAllInvalidComparisons(): array
267293
{
268294
// The provider runs before setUp(), so we need to manually fix

Tests/Constraints/BicValidatorTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Validator\Mapping\ClassMetadata;
1919
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
2020
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
21+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\BicTypedDummy;
2122

2223
class BicValidatorTest extends ConstraintValidatorTestCase
2324
{
@@ -89,6 +90,15 @@ public function testInvalidComparisonToPropertyPathFromAttribute()
8990
->assertRaised();
9091
}
9192

93+
public function testPropertyPathReferencingUninitializedProperty()
94+
{
95+
$this->setObject(new BicTypedDummy());
96+
97+
$this->validator->validate('UNCRIT2B912', new Bic(['ibanPropertyPath' => 'iban']));
98+
99+
$this->assertNoViolation();
100+
}
101+
92102
public function testValidComparisonToValue()
93103
{
94104
$constraint = new Bic(['iban' => 'FR14 2004 1010 0505 0001 3M02 606']);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Fixtures;
13+
14+
class BicTypedDummy
15+
{
16+
public string $iban;
17+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Fixtures;
13+
14+
class MinMaxTyped
15+
{
16+
public int $min;
17+
public int $max;
18+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Fixtures;
13+
14+
class TypedDummy
15+
{
16+
public mixed $value;
17+
}

Tests/Constraints/GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest extends Greate
2323
{
2424
protected static function createConstraint(?array $options = null): Constraint
2525
{
26-
return new PositiveOrZero();
26+
return new PositiveOrZero($options);
2727
}
2828

2929
public static function provideValidComparisons(): array
@@ -82,6 +82,11 @@ public function testInvalidValuePath()
8282
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
8383
}
8484

85+
public static function provideAllValidComparisons(): array
86+
{
87+
self::markTestSkipped('The "value" option cannot be used in the PositiveOrZero constraint');
88+
}
89+
8590
/**
8691
* @dataProvider provideValidComparisonsToPropertyPath
8792
*/
@@ -90,6 +95,11 @@ public function testValidComparisonToPropertyPath($comparedValue)
9095
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
9196
}
9297

98+
public function testNoViolationOnNullObjectWithPropertyPath()
99+
{
100+
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
101+
}
102+
93103
/**
94104
* @dataProvider throwsOnInvalidStringDatesProvider
95105
*/
@@ -102,4 +112,19 @@ public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
102112
{
103113
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
104114
}
115+
116+
public static function throwsOnInvalidStringDatesProvider(): array
117+
{
118+
self::markTestSkipped('The "value" option cannot be used in the PositiveOrZero constraint');
119+
}
120+
121+
public static function provideAllInvalidComparisons(): array
122+
{
123+
self::markTestSkipped('The "value" option cannot be used in the Negative constraint');
124+
}
125+
126+
public static function provideComparisonsToNullValueAtPropertyPath(): array
127+
{
128+
self::markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
129+
}
105130
}

Tests/Constraints/GreaterThanValidatorWithPositiveConstraintTest.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class GreaterThanValidatorWithPositiveConstraintTest extends GreaterThanValidato
2323
{
2424
protected static function createConstraint(?array $options = null): Constraint
2525
{
26-
return new Positive();
26+
return new Positive($options);
2727
}
2828

2929
public static function provideValidComparisons(): array
@@ -85,6 +85,11 @@ public function testInvalidValuePath()
8585
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
8686
}
8787

88+
public static function provideAllValidComparisons(): array
89+
{
90+
self::markTestSkipped('The "value" option cannot be used in the Positive constraint');
91+
}
92+
8893
/**
8994
* @dataProvider provideValidComparisonsToPropertyPath
9095
*/
@@ -105,4 +110,19 @@ public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
105110
{
106111
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
107112
}
113+
114+
public static function throwsOnInvalidStringDatesProvider(): array
115+
{
116+
self::markTestSkipped('The "value" option cannot be used in the Positive constraint');
117+
}
118+
119+
public static function provideAllInvalidComparisons(): array
120+
{
121+
self::markTestSkipped('The "value" option cannot be used in the Positive constraint');
122+
}
123+
124+
public static function provideComparisonsToNullValueAtPropertyPath(): array
125+
{
126+
self::markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
127+
}
108128
}

Tests/Constraints/LessThanOrEqualValidatorWithNegativeOrZeroConstraintTest.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class LessThanOrEqualValidatorWithNegativeOrZeroConstraintTest extends LessThanO
2323
{
2424
protected static function createConstraint(?array $options = null): Constraint
2525
{
26-
return new NegativeOrZero();
26+
return new NegativeOrZero($options);
2727
}
2828

2929
public static function provideValidComparisons(): array
@@ -85,6 +85,11 @@ public function testInvalidValuePath()
8585
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
8686
}
8787

88+
public static function provideAllValidComparisons(): array
89+
{
90+
self::markTestSkipped('The "value" option cannot be used in the NegativeOrZero constraint');
91+
}
92+
8893
/**
8994
* @dataProvider provideValidComparisonsToPropertyPath
9095
*/
@@ -93,12 +98,9 @@ public function testValidComparisonToPropertyPath($comparedValue)
9398
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
9499
}
95100

96-
/**
97-
* @dataProvider throwsOnInvalidStringDatesProvider
98-
*/
99-
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
101+
public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
100102
{
101-
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
103+
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
102104
}
103105

104106
/**
@@ -109,8 +111,21 @@ public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsS
109111
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
110112
}
111113

112-
public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
114+
/**
115+
* @dataProvider provideComparisonsToNullValueAtPropertyPath
116+
*/
117+
public function testCompareWithUninitializedPropertyAtPropertyPath($dirtyValue, $dirtyValueAsString, $isValid)
113118
{
114119
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
115120
}
121+
122+
public static function throwsOnInvalidStringDatesProvider(): array
123+
{
124+
self::markTestSkipped('The "value" option cannot be used in the NegativeOrZero constraint');
125+
}
126+
127+
public static function provideAllInvalidComparisons(): array
128+
{
129+
self::markTestSkipped('The "value" option cannot be used in the NegativeOrZero constraint');
130+
}
116131
}

Tests/Constraints/LessThanValidatorWithNegativeConstraintTest.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class LessThanValidatorWithNegativeConstraintTest extends LessThanValidatorTest
2323
{
2424
protected static function createConstraint(?array $options = null): Constraint
2525
{
26-
return new Negative();
26+
return new Negative($options);
2727
}
2828

2929
public static function provideValidComparisons(): array
@@ -85,6 +85,11 @@ public function testInvalidValuePath()
8585
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
8686
}
8787

88+
public static function provideAllValidComparisons(): array
89+
{
90+
self::markTestSkipped('The "value" option cannot be used in the Negative constraint');
91+
}
92+
8893
/**
8994
* @dataProvider provideValidComparisonsToPropertyPath
9095
*/
@@ -93,18 +98,23 @@ public function testValidComparisonToPropertyPath($comparedValue)
9398
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
9499
}
95100

101+
public static function throwsOnInvalidStringDatesProvider(): array
102+
{
103+
self::markTestSkipped('The "value" option cannot be used in the Negative constraint');
104+
}
105+
96106
/**
97-
* @dataProvider throwsOnInvalidStringDatesProvider
107+
* @dataProvider provideComparisonsToNullValueAtPropertyPath
98108
*/
99-
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
109+
public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsString, $isValid)
100110
{
101-
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
111+
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
102112
}
103113

104114
/**
105115
* @dataProvider provideComparisonsToNullValueAtPropertyPath
106116
*/
107-
public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsString, $isValid)
117+
public function testCompareWithUninitializedPropertyAtPropertyPath($dirtyValue, $dirtyValueAsString, $isValid)
108118
{
109119
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
110120
}
@@ -113,4 +123,9 @@ public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
113123
{
114124
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
115125
}
126+
127+
public static function provideAllInvalidComparisons(): array
128+
{
129+
self::markTestSkipped('The "value" option cannot be used in the Negative constraint');
130+
}
116131
}

0 commit comments

Comments
 (0)