8000 bug #20745 [Validator] add class name to the cache key (Simperfit) · symfony/symfony@bf9f5f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf9f5f0

Browse files
bug #20745 [Validator] add class name to the cache key (Simperfit)
This PR was squashed before being merged into the 3.1 branch (closes #20745). Discussion ---------- [Validator] add class name to the cache key | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20685 | License | MIT | Doc PR | Adding the class name to the cache key to avoid collision Commits ------- 1681fc9 [Validator] add class name to the cache key
2 parents 971e6e3 + 1681fc9 commit bf9f5f0

File tree

6 files changed

+148
-3
lines changed

6 files changed

+148
-3
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints\Fixtures;
13+
14+
use Symfony\Component\Validator\Constraints as Assert;
15+
16+
class ChildA
17+
{
18+
/**
19+
* @Assert\Valid
20+
* @Assert\NotNull
21+
* @Assert\NotBlank
22+
*/
23+
public $name;
24+
/**
25+
* @var ChildB
26+
* @Assert\Valid
27+
* @Assert\NotNull
28+
*/
29+
public $childB;
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints\Fixtures;
13+
14+
use Symfony\Component\Validator\Constraints as Assert;
15+
16+
class ChildB
17+
{
18+
/**
19+
* @Assert\Valid
20+
* @Assert\NotBlank
21+
*/
22+
public $name;
23+
/**
24+
* @var ChildA
25+
* @Assert\Valid
26+
* @Assert\NotBlank
27+
*/
28+
public $childA;
29+
}

src/Symfony/Component/Validator/Tests/Fixtures/Entity.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class Entity extends EntityParent implements EntityInterfaceB
3333
* @Assert\Choice(choices={"A", "B"}, message="Must be one of %choices%")
3434
*/
3535
public $firstName;
36+
/**
37+
* @Assert\Valid
38+
*/
39+
public $childA;
40+
/**
41+
* @Assert\Valid
42+
*/
43+
public $childB;
3644
protected $lastName;
3745
public $reference;
3846
public $reference2;
@@ -97,4 +105,36 @@ public function validateMe(ExecutionContextInterface $context)
97105
public static function validateMeStatic($object, ExecutionContextInterface $context)
98106
{
99107
}
108+
109+
/**
110+
* @return mixed
111+
*/
112+
public function getChildA()
113+
{
114+
return $this->childA;
115+
}
116+
117+
/**
118+
* @param mixed $childA
119+
*/
120+
public function setChildA($childA)
121+
{
122+
$this->childA = $childA;
123+
}
124+
125+
/**
126+
* @return mixed
127+
*/
128+
public function getChildB()
129+
{
130+
return $this->childB;
131+
}
132+
133+
/**
134+
* @param mixed $childB
135+
*/
136+
public function setChildB($childB)
137+
{
138+
$this->childB = $childB;
139+
}
100140
}

src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Validator\Constraints\NotNull;
2020
use Symfony\Component\Validator\Constraints\Range;
2121
use Symfony\Component\Validator\Constraints\IsTrue;
22+
use Symfony\Component\Validator\Constraints\Valid;
2223
use Symfony\Component\Validator\Mapping\ClassMetadata;
2324
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
2425
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@@ -67,6 +68,8 @@ public function testLoadClassMetadata()
6768
'message' => 'Must be one of %choices%',
6869
'choices' => array('A', 'B'),
6970
)));
71+
$expected->addPropertyConstraint('childA', new Valid());
72+
$expected->addPropertyConstraint('childB', new Valid());
7073
$expected->addGetterConstraint('lastName', new NotNull());
7174
$expected->addGetterConstraint('valid', new IsTrue());
7275
$expected->addGetterConstraint('permissions', new IsTrue());
@@ -137,6 +140,8 @@ public function testLoadClassMetadataAndMerge()
137140
'message' => 'Must be one of %choices%',
138141
'choices' => array('A', 'B'),
139142
)));
143+
$expected->addPropertyConstraint('childA', new Valid());
144+
$expected->addPropertyConstraint('childB', new Valid());
140145
$expected->addGetterConstraint('lastName', new NotNull());
141146
$expected->addGetterConstraint('valid', new IsTrue());
142147
$expected->addGetterConstraint('permissions', new IsTrue());

src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\Validator\ConstraintValidatorFactory;
1616
use Symfony\Component\Validator\Context\ExecutionContextFactory;
1717
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
18+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildA;
19+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildB;
1820
use Symfony\Component\Validator\Tests\Fixtures\Entity;
1921
use Symfony\Component\Validator\Validator\RecursiveValidator;
2022

@@ -34,6 +36,45 @@ protected function createValidator(MetadataFactoryInterface $metadataFactory, ar
3436
public function testEmptyGroupsArrayDoesNotTriggerDeprecation()
3537
{
3638
$entity = new Entity();
39+
$childA = new ChildA();
40+
$childB = new ChildB();
41+
$childA->name = false;
42+
$childB->name = 'fake';
43+
$entity->childA = array($childA);
44+
$entity->childB = array($childB);
45+
$validatorContext = $this->getMock('Symfony\Component\Validator\Validator\ContextualValidatorInterface');
46+
$validatorContext
47+
->expects($this->once())
48+
->method('validate')
49+
->with($entity, null, array())
50+
->willReturnSelf();
51+
52+
$validator = $this
53+
->getMockBuilder('Symfony\Component\Validator\Validator\RecursiveValidator')
54+
->disableOriginalConstructor()
55+
->setMethods(array('startContext'))
56+
->getMock();
57+
$validator
58+
->expects($this->once())
59+
->method('startContext')
60+
->willReturn($validatorContext);
61+
62+
$validator->validate($entity, null, array());
63+
}
64+
65+
public function testRelationBetweenChildAAndChildB()
66+
{
67+
$entity = new Entity();
68+
$childA = new ChildA();
69+
$childB = new ChildB();
70+
71+
$childA->childB = $childB;
72+
$childB->childA = $childA;
73+
74+
$childA->name = false;
75+
$childB->name = 'fake';
76+
$entity->childA = array($childA);
77+
$entity->childB = array($childB);
3778

3879
$validatorContext = $this->getMock('Symfony\Component\Validator\Validator\ContextualValidatorInterface');
3980
$validatorContext

src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function validateProperty($object, $propertyName, $groups = null)
224224
$this->validateGenericNode(
225225
$propertyValue,
226226
$object,
227-
$cacheKey.':'.$propertyName,
227+
$cacheKey.':'.get_class($object).':'.$propertyName,
228228
$propertyMetadata,
229229
$propertyPath,
230230
$groups,
@@ -280,7 +280,7 @@ public function validatePropertyValue($objectOrClass, $propertyName, $value, $gr
280280
$this->validateGenericNode(
281281
$value,
282282
$object,
283-
$cacheKey.':'.$propertyName,
283+
$cacheKey.':'.get_class($object).':'.$propertyName,
284284
$propertyMetadata,
285285
$propertyPath,
286286
$groups,
@@ -589,7 +589,7 @@ private function validateClassNode($object, $cacheKey, ClassMetadataInterface $m
589589
$this->validateGenericNode(
590590
$propertyValue,
591591
$object,
592-
$cacheKey.':'.$propertyName,
592+
$cacheKey.':'.get_class($object).':'.$propertyName,
593593
$propertyMetadata,
594594
PropertyPath::append($propertyPath, $propertyName),
595595
$groups,

0 commit comments

Comments
 (0)
0