[Validator] Add ClassMetadata plural methods for convinience

This commit is contained in:
Jakub Zalas 2014-09-13 00:52:47 +01:00 committed by Bernhard Schussek
parent 229355618b
commit 0fd6769d2a
2 changed files with 80 additions and 1 deletions

View File

@ -184,7 +184,7 @@ class ClassMetadata extends ElementMetadata implements LegacyMetadataInterface,
'members', 'members',
'name', 'name',
'properties', 'properties',
'defaultGroup' 'defaultGroup',
)); ));
} }
@ -278,6 +278,21 @@ class ClassMetadata extends ElementMetadata implements LegacyMetadataInterface,
return $this; return $this;
} }
/**
* @param string $property
* @param Constraint[] $constraints
*
* @return ClassMetadata
*/
public function addPropertyConstraints($property, array $constraints)
{
foreach ($constraints as $constraint) {
$this->addPropertyConstraint($property, $constraint);
}
return $this;
}
/** /**
* Adds a constraint to the getter of the given property. * Adds a constraint to the getter of the given property.
* *
@ -304,6 +319,21 @@ class ClassMetadata extends ElementMetadata implements LegacyMetadataInterface,
return $this; return $this;
} }
/**
* @param string $property
* @param Constraint[] $constraints
*
* @return ClassMetadata
*/
public function addGetterConstraints($property, array $constraints)
{
foreach ($constraints as $constraint) {
$this->addGetterConstraint($property, $constraint);
}
return $this;
}
/** /**
* Merges the constraints of the given metadata into this object. * Merges the constraints of the given metadata into this object.
* *

View File

@ -58,6 +58,55 @@ class ClassMetadataTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties()); $this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
} }
public function testAddMultiplePropertyConstraints()
{
$this->metadata->addPropertyConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
$constraints = array(
new ConstraintA(array('groups' => array('Default', 'Entity'))),
new ConstraintB(array('groups' => array('Default', 'Entity'))),
);
$properties = $this->metadata->getPropertyMetadata('lastName');
$this->assertCount(1, $properties);
$this->assertEquals('lastName', $properties[0]->getName());
$this->assertEquals($constraints, $properties[0]->getConstraints());
}
public function testAddGetterConstraints()
{
$this->metadata->addGetterConstraint('lastName', new ConstraintA());
$this->metadata->addGetterConstraint('lastName', new ConstraintB());
$constraints = array(
new ConstraintA(array('groups' => array('Default', 'Entity'))),
new ConstraintB(array('groups' => array('Default', 'Entity'))),
);
$properties = $this->metadata->getPropertyMetadata('lastName');
$this->assertCount(1, $properties);
$this->assertEquals('getLastName', $properties[0]->getName());
$this->assertEquals($constraints, $properties[0]->getConstraints());
}
public function testAddMultipleGetterConstraints()
{
$this->metadata->addGetterConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
$constraints = array(
new ConstraintA(array('groups' => array('Default', 'Entity'))),
new ConstraintB(array('groups' => array('Default', 'Entity'))),
);
$properties = $this->metadata->getPropertyMetadata('lastName');
$this->assertCount(1, $properties);
$this->assertEquals('getLastName', $properties[0]->getName());
$this->assertEquals($constraints, $properties[0]->getConstraints());
}
public function testMergeConstraintsMergesClassConstraints() public function testMergeConstraintsMergesClassConstraints()
{ {
$parent = new ClassMetadata(self::PARENTCLASS); $parent = new ClassMetadata(self::PARENTCLASS);