Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36101f4

Browse files
committed
[DoctrineBridge] fix calling get_class on non-object
1 parent 382cde2 commit 36101f4

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 25 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,30 @@ public function testValidateUniquenessWithEmptyIterator($entity, $result)
821822
$this->assertNoViolation();
822823
}
823824

825+
/**
826+
* @dataProvider invalidValuesProvider
827+
*/
828+
public function testCannotValidateNonObject($value)
829+
{
830+
$constraint = new UniqueEntity([
831+
'message' => 'myMessage',
832+
'fields' => ['name'],
833+
'em' => self::EM_NAME,
834+
]);
835+
836+
$this->expectException(UnexpectedValueException::class);
837+
838+
$this->validator->validate($value, $constraint);
839+
}
840+
841+
public static function invalidValuesProvider(): array
842+
{
843+
return [
844+
[null],
845+
['foo'],
846+
];
847+
}
848+
824849
public function resultWithEmptyIterator(): array
825850
{
826851
$entity = new SingleIntIdEntity(1, 'foo');

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Lines changed: 3 additions & 2 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.
@@ -57,8 +58,8 @@ public function validate($entity, Constraint $constraint)
5758
throw new ConstraintDefinitionException('At least one field has to be specified.');
5859
}
5960

60-
if (null === $entity) {
61-
return;
61+
if (!\is_object($entity)) {
62+
throw new UnexpectedValueException($entity, 'object');
6263
}
6364

6465
if ($constraint->em) {

0 commit comments

Comments
 (0)