ClassMetadataTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 testMemberMetadatas()
  90. {
  91. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  92. $this->assertTrue($this->metadata->hasMemberMetadatas('firstName'));
  93. $this->assertFalse($this->metadata->hasMemberMetadatas('non_existant_field'));
  94. }
  95. public function testMergeConstraintsKeepsPrivateMembersSeparate()
  96. {
  97. $parent = new ClassMetadata(self::PARENTCLASS);
  98. $parent->addPropertyConstraint('internal', new ConstraintA());
  99. $this->metadata->mergeConstraints($parent);
  100. $this->metadata->addPropertyConstraint('internal', new ConstraintA());
  101. $parentConstraints = array(
  102. new ConstraintA(array('groups' => array(
  103. 'Default',
  104. 'EntityParent',
  105. 'Entity',
  106. ))),
  107. );
  108. $constraints = array(
  109. new ConstraintA(array('groups' => array(
  110. 'Default',
  111. 'Entity',
  112. ))),
  113. );
  114. $members = $this->metadata->getMemberMetadatas('internal');
  115. $this->assertEquals(2, count($members));
  116. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  117. $this->assertEquals($parentConstraints, $members[0]->getConstraints());
  118. $this->assertEquals(self::CLASSNAME, $members[1]->getClassName());
  119. $this->assertEquals($constraints, $members[1]->getConstraints());
  120. }
  121. public function testGetReflectionClass()
  122. {
  123. $reflClass = new \ReflectionClass(self::CLASSNAME);
  124. $this->assertEquals($reflClass, $this->metadata->getReflectionClass());
  125. }
  126. public function testSerialize()
  127. {
  128. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  129. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  130. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  131. $this->metadata->addGetterConstraint('lastName', new ConstraintB());
  132. $metadata = unserialize(serialize($this->metadata));
  133. $this->assertEquals($this->metadata, $metadata);
  134. }
  135. public function testGroupSequencesWorkIfContainingDefaultGroup()
  136. {
  137. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
  138. }
  139. public function testGroupSequencesFailIfNotContainingDefaultGroup()
  140. {
  141. $this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');
  142. $this->metadata->setGroupSequence(array('Foo', 'Bar'));
  143. }
  144. public function testGroupSequencesFailIfContainingDefault()
  145. {
  146. $this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');
  147. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
  148. }
  149. }