Skip to content

Commit

Permalink
Merge branch '5.4' into 6.4
Browse files Browse the repository at this point in the history
* 5.4:
  [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
  [Core] Fix & Enhance security arabic translation.
  • Loading branch information
nicolas-grekas committed Jul 26, 2024
2 parents f585ed9 + 5ad62a0 commit bcf939a
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Constraints/AbstractComparisonValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Validator\Constraint;
Expand Down Expand Up @@ -56,6 +57,8 @@ public function validate(mixed $value, Constraint $constraint)
$comparedValue = $this->getPropertyAccessor()->getValue($object, $path);
} catch (NoSuchPropertyException $e) {
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $path, get_debug_type($constraint)).$e->getMessage(), 0, $e);
} catch (UninitializedPropertyException) {
$comparedValue = null;
}
} else {
$comparedValue = $constraint->value;
Expand Down
3 changes: 3 additions & 0 deletions Constraints/BicValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Intl\Countries;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Validator\Constraint;
Expand Down Expand Up @@ -130,6 +131,8 @@ public function validate(mixed $value, Constraint $constraint)
$iban = $this->getPropertyAccessor()->getValue($object, $path);
} catch (NoSuchPropertyException $e) {
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $path, get_debug_type($constraint)).$e->getMessage(), 0, $e);
} catch (UninitializedPropertyException) {
$iban = null;
}
}
if (!$iban) {
Expand Down
3 changes: 3 additions & 0 deletions Constraints/RangeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Validator\Constraint;
Expand Down Expand Up @@ -162,6 +163,8 @@ private function getLimit(?string $propertyPath, mixed $default, Constraint $con
return $this->getPropertyAccessor()->getValue($object, $propertyPath);
} catch (NoSuchPropertyException $e) {
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $propertyPath, get_debug_type($constraint)).$e->getMessage(), 0, $e);
} catch (UninitializedPropertyException) {
return null;
}
}

Expand Down
26 changes: 26 additions & 0 deletions Tests/Constraints/AbstractComparisonValidatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Validator\Constraints\AbstractComparison;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\TypedDummy;

class ComparisonTest_Class
{
Expand Down Expand Up @@ -265,6 +266,31 @@ public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsS
}
}

/**
* @dataProvider provideComparisonsToNullValueAtPropertyPath
*/
public function testCompareWithUninitializedPropertyAtPropertyPath($dirtyValue, $dirtyValueAsString, $isValid)
{
$this->setObject(new TypedDummy());

$this->validator->validate($dirtyValue, $this->createConstraint([
'message' => 'Constraint Message',
'propertyPath' => 'value',
]));

if ($isValid) {
$this->assertNoViolation();
} else {
$this->buildViolation('Constraint Message')
->setParameter('{{ value }}', $dirtyValueAsString)
->setParameter('{{ compared_value }}', 'null')
->setParameter('{{ compared_value_type }}', 'null')
->setParameter('{{ compared_value_path }}', 'value')
->setCode($this->getErrorCode())
->assertRaised();
}
}

public static function provideAllInvalidComparisons(): array
{
// The provider runs before setUp(), so we need to manually fix
Expand Down
10 changes: 10 additions & 0 deletions Tests/Constraints/BicValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\BicTypedDummy;

class BicValidatorTest extends ConstraintValidatorTestCase
{
Expand Down Expand Up @@ -89,6 +90,15 @@ public function testInvalidComparisonToPropertyPathFromAttribute()
->assertRaised();
}

public function testPropertyPathReferencingUninitializedProperty()
{
$this->setObject(new BicTypedDummy());

$this->validator->validate('UNCRIT2B912', new Bic(['ibanPropertyPath' => 'iban']));

$this->assertNoViolation();
}

public function testValidComparisonToValue()
{
$constraint = new Bic(['iban' => 'FR14 2004 1010 0505 0001 3M02 606']);
Expand Down
17 changes: 17 additions & 0 deletions Tests/Constraints/Fixtures/BicTypedDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Constraints\Fixtures;

class BicTypedDummy
{
public string $iban;
}
18 changes: 18 additions & 0 deletions Tests/Constraints/Fixtures/MinMaxTyped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Constraints\Fixtures;

class MinMaxTyped
{
public int $min;
public int $max;
}
17 changes: 17 additions & 0 deletions Tests/Constraints/Fixtures/TypedDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Constraints\Fixtures;

class TypedDummy
{
public mixed $value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest extends Greate
{
protected static function createConstraint(?array $options = null): Constraint
{
return new PositiveOrZero();
return new PositiveOrZero($options);
}

public static function provideValidComparisons(): array
Expand Down Expand Up @@ -86,6 +86,11 @@ public function testInvalidValuePath()
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
}

public static function provideAllValidComparisons(): array
{
self::markTestSkipped('The "value" option cannot be used in the PositiveOrZero constraint');
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
*/
Expand All @@ -94,6 +99,11 @@ public function testValidComparisonToPropertyPath($comparedValue)
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
}

public function testNoViolationOnNullObjectWithPropertyPath()
{
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
}

/**
* @dataProvider throwsOnInvalidStringDatesProvider
*/
Expand All @@ -106,4 +116,19 @@ public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
{
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
}

public static function throwsOnInvalidStringDatesProvider(): array
{
self::markTestSkipped('The "value" option cannot be used in the PositiveOrZero constraint');
}

public static function provideAllInvalidComparisons(): array
{
self::markTestSkipped('The "value" option cannot be used in the Negative constraint');
}

public static function provideComparisonsToNullValueAtPropertyPath(): array
{
self::markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class GreaterThanValidatorWithPositiveConstraintTest extends GreaterThanValidato
{
protected static function createConstraint(?array $options = null): Constraint
{
return new Positive();
return new Positive($options);
}

public static function provideValidComparisons(): array
Expand Down Expand Up @@ -89,6 +89,11 @@ public function testInvalidValuePath()
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
}

public static function provideAllValidComparisons(): array
{
self::markTestSkipped('The "value" option cannot be used in the Positive constraint');
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
*/
Expand All @@ -109,4 +114,19 @@ public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
{
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
}

public static function throwsOnInvalidStringDatesProvider(): array
{
self::markTestSkipped('The "value" option cannot be used in the Positive constraint');
}

public static function provideAllInvalidComparisons(): array
{
self::markTestSkipped('The "value" option cannot be used in the Positive constraint');
}

public static function provideComparisonsToNullValueAtPropertyPath(): array
{
self::markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LessThanOrEqualValidatorWithNegativeOrZeroConstraintTest extends LessThanO
{
protected static function createConstraint(?array $options = null): Constraint
{
return new NegativeOrZero();
return new NegativeOrZero($options);
}

public static function provideValidComparisons(): array
Expand Down Expand Up @@ -89,6 +89,11 @@ public function testInvalidValuePath()
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
}

public static function provideAllValidComparisons(): array
{
self::markTestSkipped('The "value" option cannot be used in the NegativeOrZero constraint');
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
*/
Expand All @@ -97,12 +102,9 @@ public function testValidComparisonToPropertyPath($comparedValue)
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
}

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

/**
Expand All @@ -113,8 +115,21 @@ public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsS
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
}

public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
/**
* @dataProvider provideComparisonsToNullValueAtPropertyPath
*/
public function testCompareWithUninitializedPropertyAtPropertyPath($dirtyValue, $dirtyValueAsString, $isValid)
{
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
}

public static function throwsOnInvalidStringDatesProvider(): array
{
self::markTestSkipped('The "value" option cannot be used in the NegativeOrZero constraint');
}

public static function provideAllInvalidComparisons(): array
{
self::markTestSkipped('The "value" option cannot be used in the NegativeOrZero constraint');
}
}
25 changes: 20 additions & 5 deletions Tests/Constraints/LessThanValidatorWithNegativeConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LessThanValidatorWithNegativeConstraintTest extends LessThanValidatorTest
{
protected static function createConstraint(?array $options = null): Constraint
{
return new Negative();
return new Negative($options);
}

public static function provideValidComparisons(): array
Expand Down Expand Up @@ -89,6 +89,11 @@ public function testInvalidValuePath()
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
}

public static function provideAllValidComparisons(): array
{
self::markTestSkipped('The "value" option cannot be used in the Negative constraint');
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
*/
Expand All @@ -97,18 +102,23 @@ public function testValidComparisonToPropertyPath($comparedValue)
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
}

public static function throwsOnInvalidStringDatesProvider(): array
{
self::markTestSkipped('The "value" option cannot be used in the Negative constraint');
}

/**
* @dataProvider throwsOnInvalidStringDatesProvider
* @dataProvider provideComparisonsToNullValueAtPropertyPath
*/
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsString, $isValid)
{
$this->markTestSkipped('The compared value cannot be an invalid string date because it is hardcoded to 0.');
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
}

/**
* @dataProvider provideComparisonsToNullValueAtPropertyPath
*/
public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsString, $isValid)
public function testCompareWithUninitializedPropertyAtPropertyPath($dirtyValue, $dirtyValueAsString, $isValid)
{
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
}
Expand All @@ -117,4 +127,9 @@ public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
{
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
}

public static function provideAllInvalidComparisons(): array
{
self::markTestSkipped('The "value" option cannot be used in the Negative constraint');
}
}
Loading

0 comments on commit bcf939a

Please sign in to comment.