PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/RestAPI/vendor/symfony/validator/Tests/Mapping/ClassMetadataTest.php

https://gitlab.com/martinstti/silex-microframework-rest
PHP | 280 lines | 204 code | 56 blank | 20 comment | 0 complexity | 7a2c395eb344a10755324190122e4986 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. $constraints = array(
  110. new ConstraintA(array('groups' => array(
  111. 'Default',
  112. 'EntityParent',
  113. 'Entity',
  114. ))),
  115. new ConstraintA(array('groups' => array(
  116. 'Default',
  117. 'Entity',
  118. ))),
  119. );
  120. $members = $this->metadata->getPropertyMetadata('firstName');
  121. $this->assertCount(1, $members);
  122. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  123. $this->assertEquals($constraints, $members[0]->getConstraints());
  124. }
  125. public function testMemberMetadatas()
  126. {
  127. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  128. $this->assertTrue($this->metadata->hasPropertyMetadata('firstName'));
  129. $this->assertFalse($this->metadata->hasPropertyMetadata('non_existent_field'));
  130. }
  131. public function testMergeConstraintsKeepsPrivateMembersSeparate()
  132. {
  133. $parent = new ClassMetadata(self::PARENTCLASS);
  134. $parent->addPropertyConstraint('internal', new ConstraintA());
  135. $this->metadata->mergeConstraints($parent);
  136. $this->metadata->addPropertyConstraint('internal', new ConstraintA());
  137. $parentConstraints = array(
  138. new ConstraintA(array('groups' => array(
  139. 'Default',
  140. 'EntityParent',
  141. 'Entity',
  142. ))),
  143. );
  144. $constraints = array(
  145. new ConstraintA(array('groups' => array(
  146. 'Default',
  147. 'Entity',
  148. ))),
  149. );
  150. $members = $this->metadata->getPropertyMetadata('internal');
  151. $this->assertCount(2, $members);
  152. $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
  153. $this->assertEquals($parentConstraints, $members[0]->getConstraints());
  154. $this->assertEquals(self::CLASSNAME, $members[1]->getClassName());
  155. $this->assertEquals($constraints, $members[1]->getConstraints());
  156. }
  157. public function testGetReflectionClass()
  158. {
  159. $reflClass = new \ReflectionClass(self::CLASSNAME);
  160. $this->assertEquals($reflClass, $this->metadata->getReflectionClass());
  161. }
  162. public function testSerialize()
  163. {
  164. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  165. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  166. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  167. $this->metadata->addGetterConstraint('lastName', new ConstraintB());
  168. $metadata = unserialize(serialize($this->metadata));
  169. $this->assertEquals($this->metadata, $metadata);
  170. }
  171. public function testGroupSequencesWorkIfContainingDefaultGroup()
  172. {
  173. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
  174. }
  175. public function testGroupSequencesFailIfNotContainingDefaultGroup()
  176. {
  177. $this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');
  178. $this->metadata->setGroupSequence(array('Foo', 'Bar'));
  179. }
  180. public function testGroupSequencesFailIfContainingDefault()
  181. {
  182. $this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');
  183. $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
  184. }
  185. /**
  186. * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
  187. */
  188. public function testGroupSequenceFailsIfGroupSequenceProviderIsSet()
  189. {
  190. $metadata = new ClassMetadata(self::PROVIDERCLASS);
  191. $metadata->setGroupSequenceProvider(true);
  192. $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
  193. }
  194. /**
  195. * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
  196. */
  197. public function testGroupSequenceProviderFailsIfGroupSequenceIsSet()
  198. {
  199. $metadata = new ClassMetadata(self::PROVIDERCLASS);
  200. $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
  201. $metadata->setGroupSequenceProvider(true);
  202. }
  203. /**
  204. * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
  205. */
  206. public function testGroupSequenceProviderFailsIfDomainClassIsInvalid()
  207. {
  208. $metadata = new ClassMetadata('stdClass');
  209. $metadata->setGroupSequenceProvider(true);
  210. }
  211. public function testGroupSequenceProvider()
  212. {
  213. $metadata = new ClassMetadata(self::PROVIDERCLASS);
  214. $metadata->setGroupSequenceProvider(true);
  215. $this->assertTrue($metadata->isGroupSequenceProvider());
  216. }
  217. /**
  218. * https://github.com/symfony/symfony/issues/11604.
  219. */
  220. public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadata()
  221. {
  222. $this->assertCount(0, $this->metadata->getPropertyMetadata('foo'), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property');
  223. }
  224. }