2010-06-24 09:40:05 +01:00
< ? php
2011-01-15 13:29:43 +00:00
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-01-15 13:29:43 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\Validator\Tests\Mapping ;
2010-06-24 09:40:05 +01:00
2010-11-18 22:10:21 +00:00
use Symfony\Component\Validator\Constraint ;
2010-11-16 22:41:46 +00:00
use Symfony\Component\Validator\Constraints\Valid ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\Validator\Mapping\ClassMetadata ;
2012-03-28 14:43:52 +01:00
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA ;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB ;
use Symfony\Component\Validator\Tests\Fixtures\PropertyConstraint ;
2010-06-24 09:40:05 +01:00
class ClassMetadataTest extends \PHPUnit_Framework_TestCase
{
2012-03-28 14:43:52 +01:00
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity' ;
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent' ;
const PROVIDERCLASS = 'Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity' ;
2010-06-24 10:24:08 +01:00
protected $metadata ;
2010-12-06 14:45:37 +00:00
protected function setUp ()
2010-06-24 10:24:08 +01:00
{
$this -> metadata = new ClassMetadata ( self :: CLASSNAME );
}
2011-06-14 09:44:38 +01:00
protected function tearDown ()
{
$this -> metadata = null ;
}
2010-11-16 22:41:46 +00:00
public function testAddConstraintDoesNotAcceptValid ()
{
$this -> setExpectedException ( 'Symfony\Component\Validator\Exception\ConstraintDefinitionException' );
$this -> metadata -> addConstraint ( new Valid ());
}
2011-01-19 14:30:43 +00:00
public function testAddConstraintRequiresClassConstraints ()
{
$this -> setExpectedException ( 'Symfony\Component\Validator\Exception\ConstraintDefinitionException' );
$this -> metadata -> addConstraint ( new PropertyConstraint ());
}
2010-06-24 10:24:08 +01:00
public function testAddPropertyConstraints ()
{
$this -> metadata -> addPropertyConstraint ( 'firstName' , new ConstraintA ());
$this -> metadata -> addPropertyConstraint ( 'lastName' , new ConstraintB ());
$this -> assertEquals ( array ( 'firstName' , 'lastName' ), $this -> metadata -> getConstrainedProperties ());
}
2014-09-13 00:52:47 +01:00
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 ());
}
2010-06-24 10:24:08 +01:00
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' ,
))),
);
2014-08-19 15:41:47 +01:00
$members = $this -> metadata -> getPropertyMetadata ( 'firstName' );
2010-06-24 10:24:08 +01:00
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 1 , $members );
2010-06-24 10:24:08 +01:00
$this -> assertEquals ( self :: PARENTCLASS , $members [ 0 ] -> getClassName ());
$this -> assertEquals ( $constraints , $members [ 0 ] -> getConstraints ());
}
2011-02-02 10:10:42 +00:00
public function testMemberMetadatas ()
{
$this -> metadata -> addPropertyConstraint ( 'firstName' , new ConstraintA ());
2014-08-19 15:41:47 +01:00
$this -> assertTrue ( $this -> metadata -> hasPropertyMetadata ( 'firstName' ));
2015-08-11 08:58:34 +01:00
$this -> assertFalse ( $this -> metadata -> hasPropertyMetadata ( 'non_existent_field' ));
2011-02-02 10:10:42 +00:00
}
2011-03-01 17:56:35 +00:00
public function testMergeConstraintsKeepsPrivateMembersSeparate ()
2010-06-24 10:24:08 +01:00
{
$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' ,
))),
);
2014-08-19 15:41:47 +01:00
$members = $this -> metadata -> getPropertyMetadata ( 'internal' );
2010-06-24 10:24:08 +01:00
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 2 , $members );
2010-06-24 10:24:08 +01:00
$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 ());
}
2010-06-29 16:20:29 +01:00
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 );
}
2010-11-18 22:10:21 +00:00
public function testGroupSequencesWorkIfContainingDefaultGroup ()
{
$this -> metadata -> setGroupSequence ( array ( 'Foo' , $this -> metadata -> getDefaultGroup ()));
}
public function testGroupSequencesFailIfNotContainingDefaultGroup ()
{
$this -> setExpectedException ( 'Symfony\Component\Validator\Exception\GroupDefinitionException' );
$this -> metadata -> setGroupSequence ( array ( 'Foo' , 'Bar' ));
}
public function testGroupSequencesFailIfContainingDefault ()
{
$this -> setExpectedException ( 'Symfony\Component\Validator\Exception\GroupDefinitionException' );
$this -> metadata -> setGroupSequence ( array ( 'Foo' , $this -> metadata -> getDefaultGroup (), Constraint :: DEFAULT_GROUP ));
}
2010-06-24 09:40:05 +01:00
2012-02-02 14:59:37 +00:00
/**
2013-01-05 17:52:40 +00:00
* @ expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
2012-02-02 14:59:37 +00:00
*/
public function testGroupSequenceFailsIfGroupSequenceProviderIsSet ()
2012-01-30 22:51:45 +00:00
{
$metadata = new ClassMetadata ( self :: PROVIDERCLASS );
$metadata -> setGroupSequenceProvider ( true );
2012-02-02 14:59:37 +00:00
$metadata -> setGroupSequence ( array ( 'GroupSequenceProviderEntity' , 'Foo' ));
2012-01-30 22:51:45 +00:00
}
2012-02-02 14:59:37 +00:00
/**
2013-01-05 17:52:40 +00:00
* @ expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
2012-02-02 14:59:37 +00:00
*/
public function testGroupSequenceProviderFailsIfGroupSequenceIsSet ()
2012-01-30 22:51:45 +00:00
{
$metadata = new ClassMetadata ( self :: PROVIDERCLASS );
$metadata -> setGroupSequence ( array ( 'GroupSequenceProviderEntity' , 'Foo' ));
2012-02-02 14:59:37 +00:00
$metadata -> setGroupSequenceProvider ( true );
2012-01-30 22:51:45 +00:00
}
2012-02-02 14:59:37 +00:00
/**
2013-01-05 17:52:40 +00:00
* @ expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
2012-02-02 14:59:37 +00:00
*/
public function testGroupSequenceProviderFailsIfDomainClassIsInvalid ()
2012-01-27 01:33:45 +00:00
{
2012-01-28 21:10:33 +00:00
$metadata = new ClassMetadata ( 'stdClass' );
2012-02-02 14:59:37 +00:00
$metadata -> setGroupSequenceProvider ( true );
2012-01-30 22:51:45 +00:00
}
2012-01-27 01:33:45 +00:00
2012-01-30 22:51:45 +00:00
public function testGroupSequenceProvider ()
{
$metadata = new ClassMetadata ( self :: PROVIDERCLASS );
$metadata -> setGroupSequenceProvider ( true );
2012-02-02 14:59:37 +00:00
$this -> assertTrue ( $metadata -> isGroupSequenceProvider ());
2012-01-27 01:33:45 +00:00
}
2014-08-08 09:52:31 +01:00
/**
2014-12-21 17:00:50 +00:00
* https :// github . com / symfony / symfony / issues / 11604.
2014-08-08 09:52:31 +01:00
*/
public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadata ()
{
$this -> assertCount ( 0 , $this -> metadata -> getPropertyMetadata ( 'foo' ), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property' );
}
2012-01-27 01:33:45 +00:00
}