This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Validator/Mapping/ClassMetadataTest.php
Bernhard Schussek 6a148465da [Validator][Form] Removed support for match-all group "*"
The constraint "Valid" does not accept any options or groups anymore. As per
JSR303 1.0 final, section 3.5.1 "Object graph validation" (page 39),
properties  annotated with valid should be cascaded independent of the current
group (i.e. always). Thus the group "*" is not necessary anymore and was
removed from the "Valid" constraint in the Form validation.xml.
2010-11-17 08:02:06 +01:00

144 lines
4.8 KiB
PHP

<?php
namespace Symfony\Tests\Component\Validator\Mapping;
require_once __DIR__.'/../Fixtures/Entity.php';
require_once __DIR__.'/../Fixtures/ConstraintA.php';
require_once __DIR__.'/../Fixtures/ConstraintB.php';
use Symfony\Tests\Component\Validator\Fixtures\Entity;
use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\PropertyMetadata;
class ClassMetadataTest extends \PHPUnit_Framework_TestCase
{
const CLASSNAME = 'Symfony\Tests\Component\Validator\Fixtures\Entity';
const PARENTCLASS = 'Symfony\Tests\Component\Validator\Fixtures\EntityParent';
protected $metadata;
public function setUp()
{
$this->metadata = new ClassMetadata(self::CLASSNAME);
}
public function testAddConstraintDoesNotAcceptValid()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
$this->metadata->addConstraint(new Valid());
}
public function testAddPropertyConstraints()
{
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
$this->metadata->addPropertyConstraint('lastName', new ConstraintB());
$this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
}
public function testMergeConstraintsMergesClassConstraints()
{
$parent = new ClassMetadata(self::PARENTCLASS);
$parent->addConstraint(new ConstraintA());
$this->metadata->mergeConstraints($parent);
$this->metadata->addConstraint(new ConstraintA());
$constraints = array(
new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
'Default',
'Entity',
))),
);
$this->assertEquals($constraints, $this->metadata->getConstraints());
}
public function testMergeConstraintsMergesMemberConstraints()
{
$parent = new ClassMetadata(self::PARENTCLASS);
$parent->addPropertyConstraint('firstName', new ConstraintA());
$this->metadata->mergeConstraints($parent);
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
$constraints = array(
new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
'Default',
'Entity',
))),
);
$members = $this->metadata->getMemberMetadatas('firstName');
$this->assertEquals(1, count($members));
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
$this->assertEquals($constraints, $members[0]->getConstraints());
}
public function testMergeConstraintsKeepsPrivateMembersSeperate()
{
$parent = new ClassMetadata(self::PARENTCLASS);
$parent->addPropertyConstraint('internal', new ConstraintA());
$this->metadata->mergeConstraints($parent);
$this->metadata->addPropertyConstraint('internal', new ConstraintA());
$parentConstraints = array(
new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
))),
);
$constraints = array(
new ConstraintA(array('groups' => array(
'Default',
'Entity',
))),
);
$members = $this->metadata->getMemberMetadatas('internal');
$this->assertEquals(2, count($members));
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
$this->assertEquals($parentConstraints, $members[0]->getConstraints());
$this->assertEquals(self::CLASSNAME, $members[1]->getClassName());
$this->assertEquals($constraints, $members[1]->getConstraints());
}
public function testGetReflectionClass()
{
$reflClass = new \ReflectionClass(self::CLASSNAME);
$this->assertEquals($reflClass, $this->metadata->getReflectionClass());
}
public function testSerialize()
{
$this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
$this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
$this->metadata->addGetterConstraint('lastName', new ConstraintB());
$metadata = unserialize(serialize($this->metadata));
$this->assertEquals($this->metadata, $metadata);
}
}