AnnotationLoaderTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 testLoadClassMetadataReturnsTrueIfSuccessful()
  16. {
  17. $loader = new AnnotationLoader();
  18. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  19. $this->assertTrue($loader->loadClassMetadata($metadata));
  20. }
  21. public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
  22. {
  23. $loader = new AnnotationLoader();
  24. $metadata = new ClassMetadata('\stdClass');
  25. $this->assertFalse($loader->loadClassMetadata($metadata));
  26. }
  27. public function testLoadClassMetadata()
  28. {
  29. $loader = new AnnotationLoader();
  30. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  31. $loader->loadClassMetadata($metadata);
  32. $expected = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  33. $expected->addConstraint(new NotNull());
  34. $expected->addConstraint(new ConstraintA());
  35. $expected->addConstraint(new Min(3));
  36. $expected->addConstraint(new Choice(array('A', 'B')));
  37. $expected->addConstraint(new All(array(new NotNull(), new Min(3))));
  38. $expected->addConstraint(new All(array('constraints' => array(new NotNull(), new Min(3)))));
  39. $expected->addConstraint(new Collection(array('fields' => array(
  40. 'foo' => array(new NotNull(), new Min(3)),
  41. 'bar' => new Min(5),
  42. ))));
  43. $expected->addPropertyConstraint('firstName', new Choice(array(
  44. 'message' => 'Must be one of %choices%',
  45. 'choices' => array('A', 'B'),
  46. )));
  47. $expected->addGetterConstraint('lastName', new NotNull());
  48. // load reflection class so that the comparison passes
  49. $expected->getReflectionClass();
  50. $this->assertEquals($expected, $metadata);
  51. }
  52. /**
  53. * Test MetaData merge with parent annotation.
  54. */
  55. public function testLoadParentClassMetadata()
  56. {
  57. $loader = new AnnotationLoader();
  58. // Load Parent MetaData
  59. $parent_metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\EntityParent');
  60. $loader->loadClassMetadata($parent_metadata);
  61. $expected_parent = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\EntityParent');
  62. $expected_parent->addPropertyConstraint('other', new NotNull());
  63. $expected_parent->getReflectionClass();
  64. $this->assertEquals($expected_parent, $parent_metadata);
  65. }
  66. /**
  67. * Test MetaData merge with parent annotation.
  68. */
  69. public function testLoadClassMetadataAndMerge()
  70. {
  71. $loader = new AnnotationLoader();
  72. // Load Parent MetaData
  73. $parent_metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\EntityParent');
  74. $loader->loadClassMetadata($parent_metadata);
  75. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  76. // Merge parent metaData.
  77. $metadata->mergeConstraints($parent_metadata);
  78. $loader->loadClassMetadata($metadata);
  79. $expected_parent = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\EntityParent');
  80. $expected_parent->addPropertyConstraint('other', new NotNull());
  81. $expected_parent->getReflectionClass();
  82. $expected = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  83. $expected->mergeConstraints($expected_parent);
  84. $expected->addConstraint(new NotNull());
  85. $expected->addConstraint(new ConstraintA());
  86. $expected->addConstraint(new Min(3));
  87. $expected->addConstraint(new Choice(array('A', 'B')));
  88. $expected->addConstraint(new All(array(new NotNull(), new Min(3))));
  89. $expected->addConstraint(new All(array('constraints' => array(new NotNull(), new Min(3)))));
  90. $expected->addConstraint(new Collection(array('fields' => array(
  91. 'foo' => array(new NotNull(), new Min(3)),
  92. 'bar' => new Min(5),
  93. ))));
  94. $expected->addPropertyConstraint('firstName', new Choice(array(
  95. 'message' => 'Must be one of %choices%',
  96. 'choices' => array('A', 'B'),
  97. )));
  98. $expected->addGetterConstraint('lastName', new NotNull());
  99. // load reflection class so that the comparison passes
  100. $expected->getReflectionClass();
  101. $this->assertEquals($expected, $metadata);
  102. }
  103. }