GraphWalkerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. require_once __DIR__.'/Fixtures/Entity.php';
  4. require_once __DIR__.'/Fixtures/ConstraintA.php';
  5. require_once __DIR__.'/Fixtures/ConstraintAValidator.php';
  6. use Symfony\Tests\Component\Validator\Fixtures\Entity;
  7. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  8. use Symfony\Component\Validator\GraphWalker;
  9. use Symfony\Component\Validator\ConstraintViolation;
  10. use Symfony\Component\Validator\ConstraintViolationList;
  11. use Symfony\Component\Validator\ConstraintValidatorFactory;
  12. use Symfony\Component\Validator\Mapping\ClassMetadata;
  13. use Symfony\Component\Validator\Mapping\PropertyMetadata;
  14. use Symfony\Component\Validator\Constraints\All;
  15. use Symfony\Component\Validator\Constraints\Any;
  16. use Symfony\Component\Validator\Constraints\Valid;
  17. class GraphWalkerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. const CLASSNAME = 'Symfony\Tests\Component\Validator\Fixtures\Entity';
  20. protected $interpolator;
  21. protected $factory;
  22. protected $metadata;
  23. public function setUp()
  24. {
  25. $this->interpolator = $this->getMock('Symfony\Component\Validator\MessageInterpolator\MessageInterpolatorInterface');
  26. $this->factory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
  27. $this->walker = new GraphWalker('Root', $this->factory, new ConstraintValidatorFactory(), $this->interpolator);
  28. $this->metadata = new ClassMetadata(self::CLASSNAME);
  29. }
  30. public function testWalkClassValidatesConstraints()
  31. {
  32. $this->metadata->addConstraint(new ConstraintA());
  33. $this->walker->walkClass($this->metadata, new Entity(), 'Default', '');
  34. $this->assertEquals(1, count($this->walker->getViolations()));
  35. }
  36. public function testWalkClassValidatesPropertyConstraints()
  37. {
  38. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  39. $this->walker->walkClass($this->metadata, new Entity(), 'Default', '');
  40. $this->assertEquals(1, count($this->walker->getViolations()));
  41. }
  42. public function testWalkClassValidatesGetterConstraints()
  43. {
  44. $this->metadata->addGetterConstraint('lastName', new ConstraintA());
  45. $this->walker->walkClass($this->metadata, new Entity(), 'Default', '');
  46. $this->assertEquals(1, count($this->walker->getViolations()));
  47. }
  48. public function testWalkPropertyValueValidatesConstraints()
  49. {
  50. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  51. $this->walker->walkPropertyValue($this->metadata, 'firstName', 'value', 'Default', '');
  52. $this->assertEquals(1, count($this->walker->getViolations()));
  53. }
  54. public function testWalkConstraintBuildsAViolationIfFailed()
  55. {
  56. $constraint = new ConstraintA();
  57. $this->interpolator->expects($this->once())
  58. ->method('interpolate')
  59. ->with($this->equalTo('message'), $this->equalTo(array('param' => 'value')))
  60. ->will($this->returnValue('interpolated text'));
  61. $this->walker->walkConstraint($constraint, 'foobar', 'Default', 'firstName.path');
  62. $violations = new ConstraintViolationList();
  63. $violations->add(new ConstraintViolation(
  64. 'interpolated text',
  65. 'Root',
  66. 'firstName.path',
  67. 'foobar'
  68. ));
  69. $this->assertEquals($violations, $this->walker->getViolations());
  70. }
  71. public function testWalkConstraintBuildsNoViolationIfSuccessful()
  72. {
  73. $constraint = new ConstraintA();
  74. $this->walker->walkConstraint($constraint, 'VALID', 'Default', 'firstName.path');
  75. $this->assertEquals(0, count($this->walker->getViolations()));
  76. }
  77. }