Skip to content

[Validator] Fix issue 12302 - cache constraints #20740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ public function getMetadataFor($value)
return $this->loadedClasses[$class];
}

if (null !== $this->cache && false !== ($this->loadedClasses[$class] = $this->cache->read($class))) {
return $this->loadedClasses[$class];
if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) {
// Include constraints from the parent class
$this->mergeConstraints($metadata);

return $this->loadedClasses[$class] = $metadata;
}

if (!class_exists($class) && !interface_exists($class)) {
Expand All @@ -109,6 +112,22 @@ public function getMetadataFor($value)

$metadata = new ClassMetadata($class);

if (null !== $this->loader) {
$this->loader->loadClassMetadata($metadata);
}

if (null !== $this->cache) {
$this->cache->write($metadata);
}

// Include constraints from the parent class
$this->mergeConstraints($metadata);

return $this->loadedClasses[$class] = $metadata;
}

protected function mergeConstraints(ClassMetadata $metadata)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be made private?

{
// Include constraints from the parent class
if ($parent = $metadata->getReflectionClass()->getParentClass()) {
$metadata->mergeConstraints($this->getMetadataFor($parent->name));
Expand Down Expand Up @@ -139,16 +158,6 @@ public function getMetadataFor($value)
}
$metadata->mergeConstraints($this->getMetadataFor($interface->name));
}

if (null !== $this->loader) {
$this->loader->loadClassMetadata($metadata);
}

if (null !== $this->cache) {
$this->cache->write($metadata);
}

return $this->loadedClasses[$class] = $metadata;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Tests\Mapping\Factory;

use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
Expand All @@ -30,8 +31,8 @@ public function testLoadClassMetadataWithInterface()
$metadata = $factory->getMetadataFor(self::PARENT_CLASS);

$constraints = array(
new ConstraintA(array('groups' => array('Default', 'EntityInterfaceA', 'EntityParent'))),
new ConstraintA(array('groups' => array('Default', 'EntityParent'))),
new ConstraintA(array('groups' => array('Default', 'EntityInterfaceA', 'EntityParent'))),
);

$this->assertEquals($constraints, $metadata->getConstraints());
Expand All @@ -43,10 +44,9 @@ public function testMergeParentConstraints()
$metadata = $factory->getMetadataFor(self::CLASS_NAME);

$constraints = array(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line

new ConstraintA(array('groups' => array(
'Default',
'EntityInterfaceA',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
Expand All @@ -56,8 +56,8 @@ public function testMergeParentConstraints()
))),
new ConstraintA(array('groups' => array(
'Default',
'EntityParentInterface',
'EntityInterfaceB',
'EntityInterfaceA',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
Expand All @@ -67,8 +67,11 @@ public function testMergeParentConstraints()
))),
new ConstraintA(array('groups' => array(
'Default',
'EntityParentInterface',
'EntityInterfaceB',
'Entity',
))),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line

);

$this->assertEquals($constraints, $metadata->getConstraints());
Expand All @@ -80,8 +83,8 @@ public function testWriteMetadataToCache()
$factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);

$parentClassConstraints = array(
new ConstraintA(array('groups' => array('Default', 'EntityInterfaceA', 'EntityParent'))),
new ConstraintA(array('groups' => array('Default', 'EntityParent'))),
new ConstraintA(array('groups' => array('Default', 'EntityInterfaceA', 'EntityParent'))),
);
$interfaceAConstraints = array(
new ConstraintA(array('groups' => array('Default', 'EntityInterfaceA'))),
Expand Down Expand Up @@ -127,12 +130,43 @@ public function testReadMetadataFromCache()

$cache->expects($this->never())
->method('has');
$cache->expects($this->once())
$cache->expects($this->exactly(2))
->method('read')
->will($this->returnValue($metadata));
->withConsecutive(
array(self::PARENT_CLASS),
array(self::INTERFACE_A_CLASS)
)
->willReturnCallback(function ($name) use ($metadata) {
if (self::PARENT_CLASS == $name) {
return $metadata;
}

return new ClassMetadata(self::INTERFACE_A_CLASS);
});

$this->assertEquals($metadata, $factory->getMetadataFor(self::PARENT_CLASS));
}

public function testMetadataCacheWithRuntimeConstraint()
{
$cache = $this->getMock('Symfony\Component\Validator\Mapping\Cache\CacheInterface');
$factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);

$cache
->expects($this->any())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 spaces indendation (same below)

->method('write')
->will($this->returnCallback(function ($metadata) { serialize($metadata);}))
;

$cache->expects($this->any())
->method('read')
->will($this->returnValue(false));

$metadata = $factory->getMetadataFor(self::PARENT_CLASS);
$metadata->addConstraint(new Callback(function () {}));

$metadata = $factory->getMetadataFor(self::CLASS_NAME);
}
}

class TestLoader implements LoaderInterface
Expand Down