ValidatorTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Symfony\Tests\Components\Validator;
  3. require_once __DIR__.'/../../../../bootstrap.php';
  4. use Symfony\Components\Validator\Constraint;
  5. use Symfony\Components\Validator\Validator;
  6. use Symfony\Components\Validator\ConstraintViolation;
  7. use Symfony\Components\Validator\ConstraintViolationList;
  8. use Symfony\Components\Validator\Mapping\Metadata;
  9. use Symfony\Components\Validator\Specification\PropertySpecification;
  10. use Symfony\Components\Validator\Specification\ClassSpecification;
  11. use Symfony\Components\Validator\Specification\Specification;
  12. class ValidatorTest_Class
  13. {
  14. public $firstName = 'Bernhard';
  15. public $reference;
  16. public function getLastName()
  17. {
  18. return 'Schussek';
  19. }
  20. public function isAustralian()
  21. {
  22. return false;
  23. }
  24. }
  25. class ValidatorTest extends \PHPUnit_Framework_TestCase
  26. {
  27. public function testValidatePropertyConstraint()
  28. {
  29. /*
  30. $subject = new ValidatorTest_Class();
  31. $subjectClass = get_class($subject);
  32. $constraint = new Constraint();
  33. $property = new PropertySpecification($subjectClass, 'firstName', array($constraint));
  34. $class = new ClassSpecification($subjectClass, array($property));
  35. $specification = new Specification(array($class));
  36. $metadata = new Metadata($specification);
  37. $validatorMock = $this->getMock('Symfony\Components\Validator\ConstraintValidatorInterface');
  38. $validatorMock->expects($this->once())
  39. ->method('isValid')
  40. ->with($this->equalTo('Bernhard'), $this->equalTo($constraint))
  41. ->will($this->returnValue(false));
  42. $validatorMock->expects($this->atLeastOnce())
  43. ->method('getMessageTemplate')
  44. ->will($this->returnValue('message'));
  45. $validatorMock->expects($this->atLeastOnce())
  46. ->method('getMessageParameters')
  47. ->will($this->returnValue(array('param' => 'value')));
  48. $factoryMock = $this->getMock('Symfony\Components\Validator\ConstraintValidatorFactoryInterface');
  49. $factoryMock->expects($this->once())
  50. ->method('getInstance')
  51. ->with($this->equalTo($constraint->validatedBy()))
  52. ->will($this->returnValue($validatorMock));
  53. $validator = new Validator($metadata, $factoryMock);
  54. $builder = new PropertyPathBuilder();
  55. $expected = new ConstraintViolationList();
  56. $expected->add(new ConstraintViolation(
  57. 'message',
  58. array('param' => 'value'),
  59. $subjectClass,
  60. $builder->atProperty('firstName')->getPropertyPath(),
  61. 'Bernhard'
  62. ));
  63. $this->assertEquals($expected, $validator->validateProperty($subject, 'firstName'));
  64. */
  65. }
  66. }