ClassMetadataTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Validator\Mapping;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Constraints\Valid;
  13. use Symfony\Component\Validator\Mapping\ClassMetadata;
  14. use Symfony\Component\Validator\Mapping\PropertyMetadata;
  15. use Symfony\Tests\Component\Validator\Fixtures\Entity;
  16. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  17. use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
  18. use Symfony\Tests\Component\Validator\Fixtures\PropertyConstraint;
  19. require_once __DIR__.'/../Fixtures/Entity.php';
  20. require_once __DIR__.'/../Fixtures/ConstraintA.php';
  21. require_once __DIR__.'/../Fixtures/ConstraintB.php';
  22. require_once __DIR__.'/../Fixtures/PropertyConstraint.php';
  23. class ClassMetadataTest extends \PHPUnit_Framework_TestCase
  24. {
  25. const CLASSNAME = 'Symfony\Tests\Component\Validator\Fixtures\Entity';
  26. const PARENTCLASS = 'Symfony\Tests\Component\Validator\Fixtures\EntityParent';
  27. protected $metadata;
  28. protected function setUp()
  29. {
  30. $this->metadata = new ClassMetadata(self::CLASSNAME);
  31. }
  32. protected function tearDown()
  33. {
  34. $this->metadata = null;
  35. }
  36. public function testAddConstraintDoesNotAcceptValid()
  37. {
  38. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  39. $this->metadata->addConstraint(new Valid());
  40. }
  41. public function testAddConstraintRequiresClassConstraints()
  42. {
  43. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  44. $this->metadata->addConstraint(new PropertyConstraint());
  45. }
  46. public function testAddPropertyConstraints()
  47. {
  48. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  49. $this->metadata->addPropertyConstraint('lastName', new ConstraintB());
  50. $this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
  51. }
  52. public function testMergeConstraintsMergesClassConstraints()
  53. {
  54. $parent = new ClassMetadata(self::PARENTCLASS);
  55. $parent->addConstraint(new ConstraintA());
  56. $this->metadata->mergeConstraints($parent);
  57. $this->metadata->addConstraint(new ConstraintA());
  58. $constraints = array(
  59. new ConstraintA(array('groups' => array(
  60. 'Default',
  61. 'EntityParent',
  62. 'Entity',
  63. ))),
  64. new ConstraintA(array('groups' => array(
  65. 'Default',
  66. 'Entity',
  67. ))),
  68. );
  69. $this->assertEquals($constraints, $this->metadata->getConstraints());
  70. }
  71. public function testMergeConstraintsMergesMemberConstraints()
  72. {
  73. $parent = new ClassMetadata(self::PARENTCLASS);
  74. $parent->addPropertyConstraint('firstName', new ConstraintA());
  75. $this->metadata->mergeConstraints($parent);
  76. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  77. $constraints = array(
  78. new ConstraintA(array('groups' => array(
  79. 'Default',
  80. 'EntityParent',
  81. 'Entity',
  82. ))),
  83. new ConstraintA(array('groups' => array(
  84. 'Default',
  85. 'Entity',
  86. ))),
  87. );
  88. $members = $this->metadata->getMemberMetadatas('firstName');
  89. $this->assertEquals(1, count($members));
  90. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  91. $this->assertEquals($constraints, $members[0]->getConstraints());
  92. }
  93. public function testMemberMetadatas()
  94. {
  95. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  96. $this->assertTrue($this->metadata->hasMemberMetadatas('firstName'));
  97. $this->assertFalse($this->metadata->hasMemberMetadatas('non_existant_field'));
  98. }
  99. public function testMergeConstraintsKeepsPrivateMembersSeparate()
  100. {
  101. $parent = new ClassMetadata(self::PARENTCLASS);
  102. $parent->addPropertyConstraint('internal', new ConstraintA());
  103. $this->metadata->mergeConstraints($parent);
  104. $this->metadata->addPropertyConstraint('internal', new ConstraintA());
  105. $parentConstraints = array(
  106. new ConstraintA(array('groups' => array(
  107. 'Default',
  108. 'EntityParent',
  109. 'Entity',
  110. ))),
  111. );
  112. $constraints = array(
  113. new ConstraintA(array('groups' => array(
  114. 'Default',
  115. 'Entity',
  116. ))),
  117. );
  118. $members = $this->metadata->getMemberMetadatas('internal');
  119. $this->assertEquals(2, count($members));
  120. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  121. $this->assertEquals($parentConstraints, $members[0]->getConstraints());
  122. $this->assertEquals(self::CLASSNAME, $members[1]->getClassName());
  123. $this->assertEquals($constraints, $members[1]->getConstraints());
  124. }
  125. public function testGetReflectionClass()
  126. {
  127. $reflClass = new \ReflectionClass(self::CLASSNAME);
  128. $this->assertEquals($reflClass, $this->metadata->getReflectionClass());
  129. }
  130. public function testSerialize()
  131. {
  132. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  133. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  134. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  135. $this->metadata->addGetterConstraint('lastName', new ConstraintB());
  136. $metadata = unserialize(serialize($this->metadata));
  137. $this->assertEquals($this->metadata, $metadata);
  138. }
  139. public function testGroupSequencesWorkIfContainingDefaultGroup()
  140. {
  141. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
  142. }
  143. public function testGroupSequencesFailIfNotContainingDefaultGroup()
  144. {
  145. $this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');
  146. $this->metadata->setGroupSequence(array('Foo', 'Bar'));
  147. }
  148. public function testGroupSequencesFailIfContainingDefault()
  149. {
  150. $this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');
  151. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
  152. }
  153. }