AnnotationLoaderTest.php 2.3 KB

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