AnnotationLoaderTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. $loader = new AnnotationLoader($reader);
  34. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  35. $this->assertTrue($loader->loadClassMetadata($metadata));
  36. }
  37. public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
  38. {
  39. $loader = new AnnotationLoader(new AnnotationReader());
  40. $metadata = new ClassMetadata('\stdClass');
  41. $this->assertFalse($loader->loadClassMetadata($metadata));
  42. }
  43. public function testLoadClassMetadata()
  44. {
  45. $loader = new AnnotationLoader(new AnnotationReader());
  46. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  47. $loader->loadClassMetadata($metadata);
  48. $expected = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  49. $expected->setGroupSequence(array('Foo', 'Entity'));
  50. $expected->addConstraint(new ConstraintA());
  51. $expected->addPropertyConstraint('firstName', new NotNull());
  52. $expected->addPropertyConstraint('firstName', new Min(3));
  53. $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Min(3))));
  54. $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Min(3)))));
  55. $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
  56. 'foo' => array(new NotNull(), new Min(3)),
  57. 'bar' => new Min(5),
  58. ))));
  59. $expected->addPropertyConstraint('firstName', new Choice(array(
  60. 'message' => 'Must be one of %choices%',
  61. 'choices' => array('A', 'B'),
  62. )));
  63. $expected->addGetterConstraint('lastName', new NotNull());
  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\Tests\Component\Validator\Fixtures\EntityParent');
  76. $loader->loadClassMetadata($parent_metadata);
  77. $expected_parent = new ClassMetadata('Symfony\Tests\Component\Validator\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\Tests\Component\Validator\Fixtures\EntityParent');
  90. $loader->loadClassMetadata($parent_metadata);
  91. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  92. // Merge parent metaData.
  93. $metadata->mergeConstraints($parent_metadata);
  94. $loader->loadClassMetadata($metadata);
  95. $expected_parent = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\EntityParent');
  96. $expected_parent->addPropertyConstraint('other', new NotNull());
  97. $expected_parent->getReflectionClass();
  98. $expected = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  99. $expected->mergeConstraints($expected_parent);
  100. $expected->setGroupSequence(array('Foo', 'Entity'));
  101. $expected->addConstraint(new ConstraintA());
  102. $expected->addPropertyConstraint('firstName', new NotNull());
  103. $expected->addPropertyConstraint('firstName', new Min(3));
  104. $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Min(3))));
  105. $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Min(3)))));
  106. $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
  107. 'foo' => array(new NotNull(), new Min(3)),
  108. 'bar' => new Min(5),
  109. ))));
  110. $expected->addPropertyConstraint('firstName', new Choice(array(
  111. 'message' => 'Must be one of %choices%',
  112. 'choices' => array('A', 'B'),
  113. )));
  114. $expected->addGetterConstraint('lastName', new NotNull());
  115. // load reflection class so that the comparison passes
  116. $expected->getReflectionClass();
  117. $this->assertEquals($expected, $metadata);
  118. }
  119. }