Skip to content

Commit 990afdc

Browse files
bug #44375 [DoctrineBridge] fix calling get_class on non-object (kbond)
This PR was merged into the 4.4 branch. Discussion ---------- [DoctrineBridge] fix calling get_class on non-object | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | n/a Discovered this bug while reviewing symfony/symfony#38662. Commits ------- 70310ca9f3 [DoctrineBridge] fix calling get_class on non-object
2 parents 4849128 + 4ade3bf commit 990afdc

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
/**
@@ -821,6 +822,32 @@ public function testValidateUniquenessWithEmptyIterator($entity, $result)
821822
$this->assertNoViolation();
822823
}
823824

825+
public function testValueMustBeObject()
826+
{
827+
$constraint = new UniqueEntity([
828+
'message' => 'myMessage',
829+
'fields' => ['name'],
830+
'em' => self::EM_NAME,
831+
]);
832+
833+
$this->expectException(UnexpectedValueException::class);
834+
835+
$this->validator->validate('foo', $constraint);
836+
}
837+
838+
public function testValueCanBeNull()
839+
{
840+
$constraint = new UniqueEntity([
841+
'message' => 'myMessage',
842+
'fields' => ['name'],
843+
'em' => self::EM_NAME,
844+
]);
845+
846+
$this->validator->validate(null, $constraint);
847+
848+
$this->assertNoViolation();
849+
}
850+
824851
public function resultWithEmptyIterator(): array
825852
{
826853
$entity = new SingleIntIdEntity(1, 'foo');

Validator/Constraints/UniqueEntityValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\ConstraintValidator;
1717
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1818
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1920

2021
/**
2122
* Unique Entity Validator checks if one or a set of fields contain unique values.
@@ -61,6 +62,10 @@ public function validate($entity, Constraint $constraint)
6162
return;
6263
}
6364

65+
if (!\is_object($entity)) {
66+
throw new UnexpectedValueException($entity, 'object');
67+
}
68+
6469
if ($constraint->em) {
6570
$em = $this->registry->getManager($constraint->em);
6671

0 commit comments

Comments
 (0)