AnnotationLoaderTest.php 5.9 KB

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