ClassMetadataTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Symfony\Tests\Component\Validator\Mapping;
  3. use Symfony\Component\Validator\Constraint;
  4. use Symfony\Component\Validator\Constraints\Valid;
  5. use Symfony\Component\Validator\Mapping\ClassMetadata;
  6. use Symfony\Component\Validator\Mapping\PropertyMetadata;
  7. use Symfony\Tests\Component\Validator\Fixtures\Entity;
  8. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  9. use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
  10. require_once __DIR__.'/../Fixtures/Entity.php';
  11. require_once __DIR__.'/../Fixtures/ConstraintA.php';
  12. require_once __DIR__.'/../Fixtures/ConstraintB.php';
  13. class ClassMetadataTest extends \PHPUnit_Framework_TestCase
  14. {
  15. const CLASSNAME = 'Symfony\Tests\Component\Validator\Fixtures\Entity';
  16. const PARENTCLASS = 'Symfony\Tests\Component\Validator\Fixtures\EntityParent';
  17. protected $metadata;
  18. public function setUp()
  19. {
  20. $this->metadata = new ClassMetadata(self::CLASSNAME);
  21. }
  22. public function testAddConstraintDoesNotAcceptValid()
  23. {
  24. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  25. $this->metadata->addConstraint(new Valid());
  26. }
  27. public function testAddPropertyConstraints()
  28. {
  29. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  30. $this->metadata->addPropertyConstraint('lastName', new ConstraintB());
  31. $this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
  32. }
  33. public function testMergeConstraintsMergesClassConstraints()
  34. {
  35. $parent = new ClassMetadata(self::PARENTCLASS);
  36. $parent->addConstraint(new ConstraintA());
  37. $this->metadata->mergeConstraints($parent);
  38. $this->metadata->addConstraint(new ConstraintA());
  39. $constraints = array(
  40. new ConstraintA(array('groups' => array(
  41. 'Default',
  42. 'EntityParent',
  43. 'Entity',
  44. ))),
  45. new ConstraintA(array('groups' => array(
  46. 'Default',
  47. 'Entity',
  48. ))),
  49. );
  50. $this->assertEquals($constraints, $this->metadata->getConstraints());
  51. }
  52. public function testMergeConstraintsMergesMemberConstraints()
  53. {
  54. $parent = new ClassMetadata(self::PARENTCLASS);
  55. $parent->addPropertyConstraint('firstName', new ConstraintA());
  56. $this->metadata->mergeConstraints($parent);
  57. $this->metadata->addPropertyConstraint('firstName', 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. $members = $this->metadata->getMemberMetadatas('firstName');
  70. $this->assertEquals(1, count($members));
  71. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  72. $this->assertEquals($constraints, $members[0]->getConstraints());
  73. }
  74. public function testMergeConstraintsKeepsPrivateMembersSeperate()
  75. {
  76. $parent = new ClassMetadata(self::PARENTCLASS);
  77. $parent->addPropertyConstraint('internal', new ConstraintA());
  78. $this->metadata->mergeConstraints($parent);
  79. $this->metadata->addPropertyConstraint('internal', new ConstraintA());
  80. $parentConstraints = array(
  81. new ConstraintA(array('groups' => array(
  82. 'Default',
  83. 'EntityParent',
  84. 'Entity',
  85. ))),
  86. );
  87. $constraints = array(
  88. new ConstraintA(array('groups' => array(
  89. 'Default',
  90. 'Entity',
  91. ))),
  92. );
  93. $members = $this->metadata->getMemberMetadatas('internal');
  94. $this->assertEquals(2, count($members));
  95. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  96. $this->assertEquals($parentConstraints, $members[0]->getConstraints());
  97. $this->assertEquals(self::CLASSNAME, $members[1]->getClassName());
  98. $this->assertEquals($constraints, $members[1]->getConstraints());
  99. }
  100. public function testGetReflectionClass()
  101. {
  102. $reflClass = new \ReflectionClass(self::CLASSNAME);
  103. $this->assertEquals($reflClass, $this->metadata->getReflectionClass());
  104. }
  105. public function testSerialize()
  106. {
  107. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  108. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  109. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  110. $this->metadata->addGetterConstraint('lastName', new ConstraintB());
  111. $metadata = unserialize(serialize($this->metadata));
  112. $this->assertEquals($this->metadata, $metadata);
  113. }
  114. public function testGroupSequencesWorkIfContainingDefaultGroup()
  115. {
  116. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
  117. }
  118. public function testGroupSequencesFailIfNotContainingDefaultGroup()
  119. {
  120. $this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');
  121. $this->metadata->setGroupSequence(array('Foo', 'Bar'));
  122. }
  123. public function testGroupSequencesFailIfContainingDefault()
  124. {
  125. $this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');
  126. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
  127. }
  128. }