AnnotationLoaderTest.php 5.7 KB

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