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
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase ;
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
2017-02-08 07:24:27 +00:00
class ClassMetadataTest extends TestCase
2010-06-24 09:40:05 +01:00
{
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' ;
2017-08-17 22:27:11 +01:00
const PROVIDERCHILDCLASS = 'Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderChildEntity' ;
2010-06-24 10:24:08 +01:00
protected $metadata ;
2019-08-02 18:02:27 +01:00
protected function setUp ()
2010-06-24 10:24:08 +01:00
{
$this -> metadata = new ClassMetadata ( self :: CLASSNAME );
}
2019-08-02 18:02:27 +01:00
protected function tearDown ()
2011-06-14 09:44:38 +01:00
{
$this -> metadata = null ;
}
2010-11-16 22:41:46 +00:00
public function testAddConstraintDoesNotAcceptValid ()
{
2019-08-01 15:27:42 +01:00
$this -> expectException ( 'Symfony\Component\Validator\Exception\ConstraintDefinitionException' );
2010-11-16 22:41:46 +00:00
$this -> metadata -> addConstraint ( new Valid ());
}
2011-01-19 14:30:43 +00:00
public function testAddConstraintRequiresClassConstraints ()
{
2019-08-01 15:27:42 +01:00
$this -> expectException ( 'Symfony\Component\Validator\Exception\ConstraintDefinitionException' );
2011-01-19 14:30:43 +00:00
$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 ());
2019-01-16 09:39:14 +00:00
$this -> assertEquals ([ 'firstName' , 'lastName' ], $this -> metadata -> getConstrainedProperties ());
2010-06-24 10:24:08 +01:00
}
2014-09-13 00:52:47 +01:00
public function testAddMultiplePropertyConstraints ()
{
2019-01-16 09:39:14 +00:00
$this -> metadata -> addPropertyConstraints ( 'lastName' , [ new ConstraintA (), new ConstraintB ()]);
2014-09-13 00:52:47 +01:00
2019-01-16 09:39:14 +00:00
$constraints = [
new ConstraintA ([ 'groups' => [ 'Default' , 'Entity' ]]),
new ConstraintB ([ 'groups' => [ 'Default' , 'Entity' ]]),
];
2014-09-13 00:52:47 +01:00
$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 ());
2019-01-16 09:39:14 +00:00
$constraints = [
new ConstraintA ([ 'groups' => [ 'Default' , 'Entity' ]]),
new ConstraintB ([ 'groups' => [ 'Default' , 'Entity' ]]),
];
2014-09-13 00:52:47 +01:00
$properties = $this -> metadata -> getPropertyMetadata ( 'lastName' );
$this -> assertCount ( 1 , $properties );
$this -> assertEquals ( 'getLastName' , $properties [ 0 ] -> getName ());
$this -> assertEquals ( $constraints , $properties [ 0 ] -> getConstraints ());
}
public function testAddMultipleGetterConstraints ()
{
2019-01-16 09:39:14 +00:00
$this -> metadata -> addGetterConstraints ( 'lastName' , [ new ConstraintA (), new ConstraintB ()]);
2014-09-13 00:52:47 +01:00
2019-01-16 09:39:14 +00:00
$constraints = [
new ConstraintA ([ 'groups' => [ 'Default' , 'Entity' ]]),
new ConstraintB ([ 'groups' => [ 'Default' , 'Entity' ]]),
];
2014-09-13 00:52:47 +01:00
$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 ());
2019-01-16 09:39:14 +00:00
$constraints = [
new ConstraintA ([ 'groups' => [
2010-06-24 10:24:08 +01:00
'Default' ,
'EntityParent' ,
'Entity' ,
2019-01-16 09:39:14 +00:00
]]),
new ConstraintA ([ 'groups' => [
2010-06-24 10:24:08 +01:00
'Default' ,
'Entity' ,
2019-01-16 09:39:14 +00:00
]]),
];
2010-06-24 10:24:08 +01:00
$this -> assertEquals ( $constraints , $this -> metadata -> getConstraints ());
}
public function testMergeConstraintsMergesMemberConstraints ()
{
$parent = new ClassMetadata ( self :: PARENTCLASS );
$parent -> addPropertyConstraint ( 'firstName' , new ConstraintA ());
2019-01-16 09:39:14 +00:00
$parent -> addPropertyConstraint ( 'firstName' , new ConstraintB ([ 'groups' => 'foo' ]));
2010-06-24 10:24:08 +01:00
$this -> metadata -> mergeConstraints ( $parent );
$this -> metadata -> addPropertyConstraint ( 'firstName' , new ConstraintA ());
2019-01-16 09:39:14 +00:00
$constraintA1 = new ConstraintA ([ 'groups' => [
2016-07-21 08:37:47 +01:00
'Default' ,
'EntityParent' ,
'Entity' ,
2019-01-16 09:39:14 +00:00
]]);
$constraintA2 = new ConstraintA ([ 'groups' => [
2016-07-21 08:37:47 +01:00
'Default' ,
'Entity' ,
2019-01-16 09:39:14 +00:00
]]);
$constraintB = new ConstraintB ([
'groups' => [ 'foo' ],
]);
2016-07-21 08:37:47 +01:00
2019-01-16 09:39:14 +00:00
$constraints = [
2016-07-21 08:37:47 +01:00
$constraintA1 ,
2017-01-06 13:26:32 +00:00
$constraintB ,
2016-07-21 08:37:47 +01:00
$constraintA2 ,
2019-01-16 09:39:14 +00:00
];
2016-07-21 08:37:47 +01:00
2019-01-16 09:39:14 +00:00
$constraintsByGroup = [
'Default' => [
2016-07-21 08:37:47 +01:00
$constraintA1 ,
$constraintA2 ,
2019-01-16 09:39:14 +00:00
],
'EntityParent' => [
2016-07-21 08:37:47 +01:00
$constraintA1 ,
2019-01-16 09:39:14 +00:00
],
'Entity' => [
2016-07-21 08:37:47 +01:00
$constraintA1 ,
$constraintA2 ,
2019-01-16 09:39:14 +00:00
],
'foo' => [
2017-01-06 13:26:32 +00:00
$constraintB ,
2019-01-16 09:39:14 +00:00
],
];
2010-06-24 10:24:08 +01:00
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 ());
2016-07-21 08:37:47 +01:00
$this -> assertEquals ( $constraintsByGroup , $members [ 0 ] -> constraintsByGroup );
2010-06-24 10:24:08 +01:00
}
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 ());
2019-01-16 09:39:14 +00:00
$parentConstraints = [
new ConstraintA ([ 'groups' => [
2010-06-24 10:24:08 +01:00
'Default' ,
'EntityParent' ,
'Entity' ,
2019-01-16 09:39:14 +00:00
]]),
];
$constraints = [
new ConstraintA ([ 'groups' => [
2010-06-24 10:24:08 +01:00
'Default' ,
'Entity' ,
2019-01-16 09:39:14 +00:00
]]),
];
2010-06-24 10:24:08 +01:00
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 ()
{
2019-01-16 09:39:14 +00:00
$this -> metadata -> addConstraint ( new ConstraintA ([ 'property1' => 'A' ]));
$this -> metadata -> addConstraint ( new ConstraintB ([ 'groups' => 'TestGroup' ]));
2010-06-29 16:20:29 +01:00
$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 ()
{
2019-01-16 09:39:14 +00:00
$this -> metadata -> setGroupSequence ([ 'Foo' , $this -> metadata -> getDefaultGroup ()]);
2017-03-18 09:10:35 +00:00
$this -> assertInstanceOf ( 'Symfony\Component\Validator\Constraints\GroupSequence' , $this -> metadata -> getGroupSequence ());
2010-11-18 22:10:21 +00:00
}
public function testGroupSequencesFailIfNotContainingDefaultGroup ()
{
2019-08-01 23:48:42 +01:00
$this -> expectException ( 'Symfony\Component\Validator\Exception\GroupDefinitionException' );
2019-01-16 09:39:14 +00:00
$this -> metadata -> setGroupSequence ([ 'Foo' , 'Bar' ]);
2010-11-18 22:10:21 +00:00
}
public function testGroupSequencesFailIfContainingDefault ()
{
2019-08-01 23:48:42 +01:00
$this -> expectException ( 'Symfony\Component\Validator\Exception\GroupDefinitionException' );
2019-01-16 09:39:14 +00:00
$this -> metadata -> setGroupSequence ([ 'Foo' , $this -> metadata -> getDefaultGroup (), Constraint :: DEFAULT_GROUP ]);
2010-11-18 22:10:21 +00:00
}
2010-06-24 09:40:05 +01:00
2012-02-02 14:59:37 +00:00
public function testGroupSequenceFailsIfGroupSequenceProviderIsSet ()
2012-01-30 22:51:45 +00:00
{
2019-08-01 23:48:42 +01:00
$this -> expectException ( 'Symfony\Component\Validator\Exception\GroupDefinitionException' );
2012-01-30 22:51:45 +00:00
$metadata = new ClassMetadata ( self :: PROVIDERCLASS );
$metadata -> setGroupSequenceProvider ( true );
2019-01-16 09:39:14 +00:00
$metadata -> setGroupSequence ([ 'GroupSequenceProviderEntity' , 'Foo' ]);
2012-01-30 22:51:45 +00:00
}
2012-02-02 14:59:37 +00:00
public function testGroupSequenceProviderFailsIfGroupSequenceIsSet ()
2012-01-30 22:51:45 +00:00
{
2019-08-01 23:48:42 +01:00
$this -> expectException ( 'Symfony\Component\Validator\Exception\GroupDefinitionException' );
2012-01-30 22:51:45 +00:00
$metadata = new ClassMetadata ( self :: PROVIDERCLASS );
2019-01-16 09:39:14 +00:00
$metadata -> setGroupSequence ([ '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
public function testGroupSequenceProviderFailsIfDomainClassIsInvalid ()
2012-01-27 01:33:45 +00:00
{
2019-08-01 23:48:42 +01:00
$this -> expectException ( 'Symfony\Component\Validator\Exception\GroupDefinitionException' );
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
2017-08-17 22:27:11 +01:00
public function testMergeConstraintsMergesGroupSequenceProvider ()
{
$parent = new ClassMetadata ( self :: PROVIDERCLASS );
$parent -> setGroupSequenceProvider ( true );
$metadata = new ClassMetadata ( self :: PROVIDERCHILDCLASS );
$metadata -> mergeConstraints ( $parent );
$this -> assertTrue ( $metadata -> isGroupSequenceProvider ());
}
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' );
}
2016-12-25 17:58:48 +00:00
}