ClassMetadataTest.php 5.8 KB

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