Skip to content

Commit dc1679c

Browse files
Merge branch '5.1'
* 5.1: [ErrorHandler] fix throwing from __toString() Removed comments and requirements relative to php <5.5 (not supported anymore) Fix caching of parent locales file in translator fix validating lazy properties that evaluate to null
2 parents f165d70 + 8cd9f93 commit dc1679c

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Tests/Fixtures/EntityParent.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class EntityParent implements EntityInterfaceA
1818
protected $firstName;
1919
private $internal;
2020
private $data = 'Data';
21+
private $child;
2122

2223
/**
2324
* @NotNull
@@ -28,4 +29,9 @@ public function getData()
2829
{
2930
return 'Data';
3031
}
32+
33+
public function getChild()
34+
{
35+
return $this->child;
36+
}
3137
}

Tests/Validator/RecursiveValidatorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
use Symfony\Component\Validator\Constraints\NotNull;
2222
use Symfony\Component\Validator\Constraints\Optional;
2323
use Symfony\Component\Validator\Constraints\Required;
24+
use Symfony\Component\Validator\Constraints\Valid;
2425
use Symfony\Component\Validator\ConstraintValidatorFactory;
2526
use Symfony\Component\Validator\Context\ExecutionContextFactory;
2627
use Symfony\Component\Validator\Mapping\ClassMetadata;
2728
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
2829
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildA;
2930
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildB;
3031
use Symfony\Component\Validator\Tests\Fixtures\Entity;
32+
use Symfony\Component\Validator\Tests\Fixtures\EntityParent;
3133
use Symfony\Component\Validator\Tests\Fixtures\EntityWithGroupedConstraintOnMethods;
3234
use Symfony\Component\Validator\Validator\RecursiveValidator;
3335
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -144,6 +146,31 @@ public function testGroupedMethodConstraintValidateInSequence()
144146
$this->assertInstanceOf(IsTrue::class, $violations->get(1)->getConstraint());
145147
}
146148

149+
public function testValidConstraintOnGetterReturningNull()
150+
{
151+
$metadata = new ClassMetadata(EntityParent::class);
152+
$metadata->addGetterConstraint('child', new Valid());
153+
154+
$this->metadataFactory->addMetadata($metadata);
155+
156+
$violations = $this->validator->validate(new EntityParent());
157+
158+
$this->assertCount(0, $violations);
159+
}
160+
161+
public function testNotNullConstraintOnGetterReturningNull()
162+
{
163+
$metadata = new ClassMetadata(EntityParent::class);
164+
$metadata->addGetterConstraint('child', new NotNull());
165+
166+
$this->metadataFactory->addMetadata($metadata);
167+
168+
$violations = $this->validator->validate(new EntityParent());
169+
170+
$this->assertCount(1, $violations);
171+
$this->assertInstanceOf(NotNull::class, $violations->get(0)->getConstraint());
172+
}
173+
147174
public function testAllConstraintValidateAllGroupsForNestedConstraints()
148175
{
149176
$this->metadata->addPropertyConstraint('data', new All(['constraints' => [

Validator/RecursiveContextualValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ private function validateGenericNode($value, ?object $object, ?string $cacheKey,
630630

631631
if ($value instanceof LazyProperty) {
632632
$value = $value->getPropertyValue();
633+
634+
if (null === $value) {
635+
return;
636+
}
633637
}
634638

635639
if (\is_array($value)) {

0 commit comments

Comments
 (0)