ClassMetadataTest.php 6.5 KB

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