AnnotationLoaderTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Symfony\Tests\Components\Validator\Mapping\Loader;
  3. require_once __DIR__.'/../../../../../../bootstrap.php';
  4. require_once __DIR__.'/../../Fixtures/Entity.php';
  5. use Symfony\Components\Validator\Constraints\All;
  6. use Symfony\Components\Validator\Constraints\Collection;
  7. use Symfony\Components\Validator\Constraints\NotNull;
  8. use Symfony\Components\Validator\Constraints\Min;
  9. use Symfony\Components\Validator\Constraints\Choice;
  10. use Symfony\Components\Validator\Mapping\ClassMetadata;
  11. use Symfony\Components\Validator\Mapping\Loader\AnnotationLoader;
  12. class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testLoadClassMetadataReturnsTrueIfSuccessful()
  15. {
  16. $loader = new AnnotationLoader();
  17. $metadata = new ClassMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
  18. $this->assertTrue($loader->loadClassMetadata($metadata));
  19. }
  20. public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
  21. {
  22. $loader = new AnnotationLoader();
  23. $metadata = new ClassMetadata('\stdClass');
  24. $this->assertFalse($loader->loadClassMetadata($metadata));
  25. }
  26. public function testLoadClassMetadata()
  27. {
  28. $loader = new AnnotationLoader();
  29. $metadata = new ClassMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
  30. $loader->loadClassMetadata($metadata);
  31. $expected = new ClassMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
  32. $expected->addConstraint(new NotNull());
  33. $expected->addConstraint(new Min(3));
  34. $expected->addConstraint(new Choice(array('A', 'B')));
  35. $expected->addConstraint(new All(array(new NotNull(), new Min(3))));
  36. $expected->addConstraint(new All(array('constraints' => array(new NotNull(), new Min(3)))));
  37. $expected->addConstraint(new Collection(array('fields' => array(
  38. 'foo' => array(new NotNull(), new Min(3)),
  39. 'bar' => new Min(5),
  40. ))));
  41. $expected->addPropertyConstraint('firstName', new Choice(array(
  42. 'message' => 'Must be one of %choices%',
  43. 'choices' => array('A', 'B'),
  44. )));
  45. $expected->addGetterConstraint('lastName', new NotNull());
  46. // load reflection class so that the comparison passes
  47. $expected->getReflectionClass();
  48. $this->assertEquals($expected, $metadata);
  49. }
  50. }