AnnotationLoaderTest.php 5.2 KB

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