Skip to content

Commit 757b706

Browse files
Merge branch '5.4' into 6.0
* 5.4: Avoid duplicated session listener registration in tests [HttpFoundation] fix SessionHandlerFactory using connections [gha] swap the php versions we use in jobs [DoctrineBridge] fix calling get_class on non-object Update PR template ResponseListener needs only 2 parameters [Lock] create lock table if it does not exist [HttpClient] Fix handling error info in MockResponse [SecurityBundle] Fix invalid reference with `always_authenticate_before_granting` Bump Symfony version to 5.4.1 Update VERSION for 5.4.0 Update CHANGELOG for 5.4.0
2 parents 565a86a + b24e8d6 commit 757b706

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
3838
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
3939
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
40+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
4041
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
4142

4243
/**
@@ -849,6 +850,32 @@ public function testValidateUniquenessWithEmptyIterator($entity, $result)
849850
$this->assertNoViolation();
850851
}
851852

853+
public function testValueMustBeObject()
854+
{
855+
$constraint = new UniqueEntity([
856+
'message' => 'myMessage',
857+
'fields' => ['name'],
858+
'em' => self::EM_NAME,
859+
]);
860+
861+
$this->expectException(UnexpectedValueException::class);
862+
863+
$this->validator->validate('foo', $constraint);
864+
}
865+
866+
public function testValueCanBeNull()
867+
{
868+
$constraint = new UniqueEntity([
869+
'message' => 'myMessage',
870+
'fields' => ['name'],
871+
'em' => self::EM_NAME,
872+
]);
873+
874+
$this->validator->validate(null, $constraint);
875+
876+
$this->assertNoViolation();
877+
}
878+
852879
public function resultWithEmptyIterator(): array
853880
{
854881
$entity = new SingleIntIdEntity(1, 'foo');

Validator/Constraints/UniqueEntityValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Validator\ConstraintValidator;
1919
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
2020
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
21+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
2122

2223
/**
2324
* Unique Entity Validator checks if one or a set of fields contain unique values.
@@ -63,6 +64,10 @@ public function validate(mixed $entity, Constraint $constraint)
6364
return;
6465
}
6566

67+
if (!\is_object($entity)) {
68+
throw new UnexpectedValueException($entity, 'object');
69+
}
70+
6671
if ($constraint->em) {
6772
$em = $this->registry->getManager($constraint->em);
6873

0 commit comments

Comments
 (0)