AnnotationLoaderTest.php 5.8 KB

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