ClassMetadataTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. public function testAddConstraintDoesNotAcceptValid()
  33. {
  34. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  35. $this->metadata->addConstraint(new Valid());
  36. }
  37. public function testAddConstraintRequiresClassConstraints()
  38. {
  39. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  40. $this->metadata->addConstraint(new PropertyConstraint());
  41. }
  42. public function testAddPropertyConstraints()
  43. {
  44. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  45. $this->metadata->addPropertyConstraint('lastName', new ConstraintB());
  46. $this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
  47. }
  48. public function testMergeConstraintsMergesClassConstraints()
  49. {
  50. $parent = new ClassMetadata(self::PARENTCLASS);
  51. $parent->addConstraint(new ConstraintA());
  52. $this->metadata->mergeConstraints($parent);
  53. $this->metadata->addConstraint(new ConstraintA());
  54. $constraints = array(
  55. new ConstraintA(array('groups' => array(
  56. 'Default',
  57. 'EntityParent',
  58. 'Entity',
  59. ))),
  60. new ConstraintA(array('groups' => array(
  61. 'Default',
  62. 'Entity',
  63. ))),
  64. );
  65. $this->assertEquals($constraints, $this->metadata->getConstraints());
  66. }
  67. public function testMergeConstraintsMergesMemberConstraints()
  68. {
  69. $parent = new ClassMetadata(self::PARENTCLASS);
  70. $parent->addPropertyConstraint('firstName', new ConstraintA());
  71. $this->metadata->mergeConstraints($parent);
  72. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  73. $constraints = array(
  74. new ConstraintA(array('groups' => array(
  75. 'Default',
  76. 'EntityParent',
  77. 'Entity',
  78. ))),
  79. new ConstraintA(array('groups' => array(
  80. 'Default',
  81. 'Entity',
  82. ))),
  83. );
  84. $members = $this->metadata->getMemberMetadatas('firstName');
  85. $this->assertEquals(1, count($members));
  86. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  87. $this->assertEquals($constraints, $members[0]->getConstraints());
  88. }
  89. public function testMergeConstraintsKeepsPrivateMembersSeperate()
  90. {
  91. $parent = new ClassMetadata(self::PARENTCLASS);
  92. $parent->addPropertyConstraint('internal', new ConstraintA());
  93. $this->metadata->mergeConstraints($parent);
  94. $this->metadata->addPropertyConstraint('internal', new ConstraintA());
  95. $parentConstraints = array(
  96. new ConstraintA(array('groups' => array(
  97. 'Default',
  98. 'EntityParent',
  99. 'Entity',
  100. ))),
  101. );
  102. $constraints = array(
  103. new ConstraintA(array('groups' => array(
  104. 'Default',
  105. 'Entity',
  106. ))),
  107. );
  108. $members = $this->metadata->getMemberMetadatas('internal');
  109. $this->assertEquals(2, count($members));
  110. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  111. $this->assertEquals($parentConstraints, $members[0]->getConstraints());
  112. $this->assertEquals(self::CLASSNAME, $members[1]->getClassName());
  113. $this->assertEquals($constraints, $members[1]->getConstraints());
  114. }
  115. public function testGetReflectionClass()
  116. {
  117. $reflClass = new \ReflectionClass(self::CLASSNAME);
  118. $this->assertEquals($reflClass, $this->metadata->getReflectionClass());
  119. }
  120. public function testSerialize()
  121. {
  122. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  123. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  124. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  125. $this->metadata->addGetterConstraint('lastName', new ConstraintB());
  126. $metadata = unserialize(serialize($this->metadata));
  127. $this->assertEquals($this->metadata, $metadata);
  128. }
  129. public function testGroupSequencesWorkIfContainingDefaultGroup()
  130. {
  131. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
  132. }
  133. public function testGroupSequencesFailIfNotContainingDefaultGroup()
  134. {
  135. $this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');
  136. $this->metadata->setGroupSequence(array('Foo', 'Bar'));
  137. }
  138. public function testGroupSequencesFailIfContainingDefault()
  139. {
  140. $this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');
  141. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
  142. }
  143. }