/vendor/symfony/symfony/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php

https://gitlab.com/pr0055/symfonypizza · PHP · 298 lines · 220 code · 58 blank · 20 comment · 0 complexity · 9321dc566ab3785bbe97730c73b8142e MD5 · raw file

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