AnnotationLoaderTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Loader;
  11. use Doctrine\Common\Annotations\AnnotationReader;
  12. use PHPUnit\Framework\TestCase;
  13. use Symfony\Component\Validator\Constraints\All;
  14. use Symfony\Component\Validator\Constraints\Callback;
  15. use Symfony\Component\Validator\Constraints\Choice;
  16. use Symfony\Component\Validator\Constraints\Collection;
  17. use Symfony\Component\Validator\Constraints\IsTrue;
  18. use Symfony\Component\Validator\Constraints\NotNull;
  19. use Symfony\Component\Validator\Constraints\Range;
  20. use Symfony\Component\Validator\Mapping\ClassMetadata;
  21. use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
  22. use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
  23. class AnnotationLoaderTest extends TestCase
  24. {
  25. public function testLoadClassMetadataReturnsTrueIfSuccessful()
  26. {
  27. $reader = new AnnotationReader();
  28. $loader = new AnnotationLoader($reader);
  29. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  30. $this->assertTrue($loader->loadClassMetadata($metadata));
  31. }
  32. public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
  33. {
  34. $loader = new AnnotationLoader(new AnnotationReader());
  35. $metadata = new ClassMetadata('\stdClass');
  36. $this->assertFalse($loader->loadClassMetadata($metadata));
  37. }
  38. public function testLoadClassMetadata()
  39. {
  40. $loader = new AnnotationLoader(new AnnotationReader());
  41. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  42. $loader->loadClassMetadata($metadata);
  43. $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  44. $expected->setGroupSequence(array('Foo', 'Entity'));
  45. $expected->addConstraint(new ConstraintA());
  46. $expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
  47. $expected->addConstraint(new Callback(array('callback' => 'validateMe', 'payload' => 'foo')));
  48. $expected->addConstraint(new Callback('validateMeStatic'));
  49. $expected->addPropertyConstraint('firstName', new NotNull());
  50. $expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
  51. $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
  52. $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
  53. $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
  54. 'foo' => array(new NotNull(), new Range(array('min' => 3))),
  55. 'bar' => new Range(array('min' => 5)),
  56. ))));
  57. $expected->addPropertyConstraint('firstName', new Choice(array(
  58. 'message' => 'Must be one of %choices%',
  59. 'choices' => array('A', 'B'),
  60. )));
  61. $expected->addGetterConstraint('lastName', new NotNull());
  62. $expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
  63. $expected->addGetterConstraint('permissions', new IsTrue());
  64. // load reflection class so that the comparison passes
  65. $expected->getReflectionClass();
  66. $this->assertEquals($expected, $metadata);
  67. }
  68. /**
  69. * Test MetaData merge with parent annotation.
  70. */
  71. public function testLoadParentClassMetadata()
  72. {
  73. $loader = new AnnotationLoader(new AnnotationReader());
  74. // Load Parent MetaData
  75. $parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
  76. $loader->loadClassMetadata($parent_metadata);
  77. $expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
  78. $expected_parent->addPropertyConstraint('other', new NotNull());
  79. $expected_parent->getReflectionClass();
  80. $this->assertEquals($expected_parent, $parent_metadata);
  81. }
  82. /**
  83. * Test MetaData merge with parent annotation.
  84. */
  85. public function testLoadClassMetadataAndMerge()
  86. {
  87. $loader = new AnnotationLoader(new AnnotationReader());
  88. // Load Parent MetaData
  89. $parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
  90. $loader->loadClassMetadata($parent_metadata);
  91. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  92. // Merge parent metaData.
  93. $metadata->mergeConstraints($parent_metadata);
  94. $loader->loadClassMetadata($metadata);
  95. $expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
  96. $expected_parent->addPropertyConstraint('other', new NotNull());
  97. $expected_parent->getReflectionClass();
  98. $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  99. $expected->mergeConstraints($expected_parent);
  100. $expected->setGroupSequence(array('Foo', 'Entity'));
  101. $expected->addConstraint(new ConstraintA());
  102. $expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
  103. $expected->addConstraint(new Callback(array('callback' => 'validateMe', 'payload' => 'foo')));
  104. $expected->addConstraint(new Callback('validateMeStatic'));
  105. $expected->addPropertyConstraint('firstName', new NotNull());
  106. $expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
  107. $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
  108. $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
  109. $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
  110. 'foo' => array(new NotNull(), new Range(array('min' => 3))),
  111. 'bar' => new Range(array('min' => 5)),
  112. ))));
  113. $expected->addPropertyConstraint('firstName', new Choice(array(
  114. 'message' => 'Must be one of %choices%',
  115. 'choices' => array('A', 'B'),
  116. )));
  117. $expected->addGetterConstraint('lastName', new NotNull());
  118. $expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
  119. $expected->addGetterConstraint('permissions', new IsTrue());
  120. // load reflection class so that the comparison passes
  121. $expected->getReflectionClass();
  122. $this->assertEquals($expected, $metadata);
  123. }
  124. public function testLoadGroupSequenceProviderAnnotation()
  125. {
  126. $loader = new AnnotationLoader(new AnnotationReader());
  127. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
  128. $loader->loadClassMetadata($metadata);
  129. $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
  130. $expected->setGroupSequenceProvider(true);
  131. $expected->getReflectionClass();
  132. $this->assertEquals($expected, $metadata);
  133. }
  134. }