ClassMetadataTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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\Component\Validator\Tests\Mapping;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\Constraints\Valid;
  14. use Symfony\Component\Validator\Mapping\ClassMetadata;
  15. use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
  16. use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
  17. use Symfony\Component\Validator\Tests\Fixtures\PropertyConstraint;
  18. class ClassMetadataTest extends TestCase
  19. {
  20. const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
  21. const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
  22. const PROVIDERCLASS = 'Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity';
  23. const PROVIDERCHILDCLASS = 'Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderChildEntity';
  24. protected $metadata;
  25. protected function setUp()
  26. {
  27. $this->metadata = new ClassMetadata(self::CLASSNAME);
  28. }
  29. protected function tearDown()
  30. {
  31. $this->metadata = null;
  32. }
  33. public function testAddConstraintDoesNotAcceptValid()
  34. {
  35. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  36. $this->metadata->addConstraint(new Valid());
  37. }
  38. public function testAddConstraintRequiresClassConstraints()
  39. {
  40. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  41. $this->metadata->addConstraint(new PropertyConstraint());
  42. }
  43. public function testAddPropertyConstraints()
  44. {
  45. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  46. $this->metadata->addPropertyConstraint('lastName', new ConstraintB());
  47. $this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
  48. }
  49. public function testAddMultiplePropertyConstraints()
  50. {
  51. $this->metadata->addPropertyConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
  52. $constraints = array(
  53. new ConstraintA(array('groups' => array('Default', 'Entity'))),
  54. new ConstraintB(array('groups' => array('Default', 'Entity'))),
  55. );
  56. $properties = $this->metadata->getPropertyMetadata('lastName');
  57. $this->assertCount(1, $properties);
  58. $this->assertEquals('lastName', $properties[0]->getName());
  59. $this->assertEquals($constraints, $properties[0]->getConstraints());
  60. }
  61. public function testAddGetterConstraints()
  62. {
  63. $this->metadata->addGetterConstraint('lastName', new ConstraintA());
  64. $this->metadata->addGetterConstraint('lastName', new ConstraintB());
  65. $constraints = array(
  66. new ConstraintA(array('groups' => array('Default', 'Entity'))),
  67. new ConstraintB(array('groups' => array('Default', 'Entity'))),
  68. );
  69. $properties = $this->metadata->getPropertyMetadata('lastName');
  70. $this->assertCount(1, $properties);
  71. $this->assertEquals('getLastName', $properties[0]->getName());
  72. $this->assertEquals($constraints, $properties[0]->getConstraints());
  73. }
  74. public function testAddMultipleGetterConstraints()
  75. {
  76. $this->metadata->addGetterConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
  77. $constraints = array(
  78. new ConstraintA(array('groups' => array('Default', 'Entity'))),
  79. new ConstraintB(array('groups' => array('Default', 'Entity'))),
  80. );
  81. $properties = $this->metadata->getPropertyMetadata('lastName');
  82. $this->assertCount(1, $properties);
  83. $this->assertEquals('getLastName', $properties[0]->getName());
  84. $this->assertEquals($constraints, $properties[0]->getConstraints());
  85. }
  86. public function testMergeConstraintsMergesClassConstraints()
  87. {
  88. $parent = new ClassMetadata(self::PARENTCLASS);
  89. $parent->addConstraint(new ConstraintA());
  90. $this->metadata->mergeConstraints($parent);
  91. $this->metadata->addConstraint(new ConstraintA());
  92. $constraints = array(
  93. new ConstraintA(array('groups' => array(
  94. 'Default',
  95. 'EntityParent',
  96. 'Entity',
  97. ))),
  98. new ConstraintA(array('groups' => array(
  99. 'Default',
  100. 'Entity',
  101. ))),
  102. );
  103. $this->assertEquals($constraints, $this->metadata->getConstraints());
  104. }
  105. public function testMergeConstraintsMergesMemberConstraints()
  106. {
  107. $parent = new ClassMetadata(self::PARENTCLASS);
  108. $parent->addPropertyConstraint('firstName', new ConstraintA());
  109. $parent->addPropertyConstraint('firstName', new ConstraintB(array('groups' => 'foo')));
  110. $this->metadata->mergeConstraints($parent);
  111. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  112. $constraintA1 = new ConstraintA(array('groups' => array(
  113. 'Default',
  114. 'EntityParent',
  115. 'Entity',
  116. )));
  117. $constraintA2 = new ConstraintA(array('groups' => array(
  118. 'Default',
  119. 'Entity',
  120. )));
  121. $constraintB = new ConstraintB(array(
  122. 'groups' => array('foo'),
  123. ));
  124. $constraints = array(
  125. $constraintA1,
  126. $constraintB,
  127. $constraintA2,
  128. );
  129. $constraintsByGroup = array(
  130. 'Default' => array(
  131. $constraintA1,
  132. $constraintA2,
  133. ),
  134. 'EntityParent' => array(
  135. $constraintA1,
  136. ),
  137. 'Entity' => array(
  138. $constraintA1,
  139. $constraintA2,
  140. ),
  141. 'foo' => array(
  142. $constraintB,
  143. ),
  144. );
  145. $members = $this->metadata->getPropertyMetadata('firstName');
  146. $this->assertCount(1, $members);
  147. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  148. $this->assertEquals($constraints, $members[0]->getConstraints());
  149. $this->assertEquals($constraintsByGroup, $members[0]->constraintsByGroup);
  150. }
  151. public function testMemberMetadatas()
  152. {
  153. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  154. $this->assertTrue($this->metadata->hasPropertyMetadata('firstName'));
  155. $this->assertFalse($this->metadata->hasPropertyMetadata('non_existent_field'));
  156. }
  157. public function testMergeConstraintsKeepsPrivateMembersSeparate()
  158. {
  159. $parent = new ClassMetadata(self::PARENTCLASS);
  160. $parent->addPropertyConstraint('internal', new ConstraintA());
  161. $this->metadata->mergeConstraints($parent);
  162. $this->metadata->addPropertyConstraint('internal', new ConstraintA());
  163. $parentConstraints = array(
  164. new ConstraintA(array('groups' => array(
  165. 'Default',
  166. 'EntityParent',
  167. 'Entity',
  168. ))),
  169. );
  170. $constraints = array(
  171. new ConstraintA(array('groups' => array(
  172. 'Default',
  173. 'Entity',
  174. ))),
  175. );
  176. $members = $this->metadata->getPropertyMetadata('internal');
  177. $this->assertCount(2, $members);
  178. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  179. $this->assertEquals($parentConstraints, $members[0]->getConstraints());
  180. $this->assertEquals(self::CLASSNAME, $members[1]->getClassName());
  181. $this->assertEquals($constraints, $members[1]->getConstraints());
  182. }
  183. public function testGetReflectionClass()
  184. {
  185. $reflClass = new \ReflectionClass(self::CLASSNAME);
  186. $this->assertEquals($reflClass, $this->metadata->getReflectionClass());
  187. }
  188. public function testSerialize()
  189. {
  190. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  191. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  192. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  193. $this->metadata->addGetterConstraint('lastName', new ConstraintB());
  194. $metadata = unserialize(serialize($this->metadata));
  195. $this->assertEquals($this->metadata, $metadata);
  196. }
  197. public function testGroupSequencesWorkIfContainingDefaultGroup()
  198. {
  199. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
  200. $this->assertInstanceOf('Symfony\Component\Validator\Constraints\GroupSequence', $this->metadata->getGroupSequence());
  201. }
  202. /**
  203. * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
  204. */
  205. public function testGroupSequencesFailIfNotContainingDefaultGroup()
  206. {
  207. $this->metadata->setGroupSequence(array('Foo', 'Bar'));
  208. }
  209. /**
  210. * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
  211. */
  212. public function testGroupSequencesFailIfContainingDefault()
  213. {
  214. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
  215. }
  216. /**
  217. * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
  218. */
  219. public function testGroupSequenceFailsIfGroupSequenceProviderIsSet()
  220. {
  221. $metadata = new ClassMetadata(self::PROVIDERCLASS);
  222. $metadata->setGroupSequenceProvider(true);
  223. $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
  224. }
  225. /**
  226. * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
  227. */
  228. public function testGroupSequenceProviderFailsIfGroupSequenceIsSet()
  229. {
  230. $metadata = new ClassMetadata(self::PROVIDERCLASS);
  231. $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
  232. $metadata->setGroupSequenceProvider(true);
  233. }
  234. /**
  235. * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
  236. */
  237. public function testGroupSequenceProviderFailsIfDomainClassIsInvalid()
  238. {
  239. $metadata = new ClassMetadata('stdClass');
  240. $metadata->setGroupSequenceProvider(true);
  241. }
  242. public function testGroupSequenceProvider()
  243. {
  244. $metadata = new ClassMetadata(self::PROVIDERCLASS);
  245. $metadata->setGroupSequenceProvider(true);
  246. $this->assertTrue($metadata->isGroupSequenceProvider());
  247. }
  248. public function testMergeConstraintsMergesGroupSequenceProvider()
  249. {
  250. $parent = new ClassMetadata(self::PROVIDERCLASS);
  251. $parent->setGroupSequenceProvider(true);
  252. $metadata = new ClassMetadata(self::PROVIDERCHILDCLASS);
  253. $metadata->mergeConstraints($parent);
  254. $this->assertTrue($metadata->isGroupSequenceProvider());
  255. }
  256. /**
  257. * https://github.com/symfony/symfony/issues/11604.
  258. */
  259. public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadata()
  260. {
  261. $this->assertCount(0, $this->metadata->getPropertyMetadata('foo'), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property');
  262. }
  263. }