ExecuteValidatorTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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;
  11. use Symfony\Component\Validator\ExecutionContext;
  12. use Symfony\Component\Validator\ConstraintViolation;
  13. use Symfony\Component\Validator\ConstraintViolationList;
  14. use Symfony\Component\Validator\Constraints\Execute;
  15. use Symfony\Component\Validator\Constraints\ExecuteValidator;
  16. class ExecuteValidatorTest_Object
  17. {
  18. public function validateOne(ExecutionContext $context)
  19. {
  20. $context->setCurrentClass('Foo');
  21. $context->setCurrentProperty('bar');
  22. $context->setGroup('mygroup');
  23. $context->setPropertyPath('foo.bar');
  24. $context->addViolation('My message', array('parameter'), 'invalidValue');
  25. }
  26. public function validateTwo(ExecutionContext $context)
  27. {
  28. $context->addViolation('Other message', array('other parameter'), 'otherInvalidValue');
  29. }
  30. }
  31. class ExecuteValidatorTest extends \PHPUnit_Framework_TestCase
  32. {
  33. protected $validator;
  34. protected $walker;
  35. protected $context;
  36. protected function setUp()
  37. {
  38. $this->walker = $this->getMock('Symfony\Component\Validator\GraphWalker', array(), array(), '', false);
  39. $metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
  40. $this->context = new ExecutionContext('Root', $this->walker, $metadataFactory);
  41. $this->context->setCurrentClass('InitialClass');
  42. $this->context->setCurrentProperty('initialProperty');
  43. $this->context->setGroup('InitialGroup');
  44. $this->context->setPropertyPath('initial.property.path');
  45. $this->validator = new ExecuteValidator();
  46. $this->validator->initialize($this->context);
  47. }
  48. public function testNullIsValid()
  49. {
  50. $this->assertTrue($this->validator->isValid(null, new Execute('foo')));
  51. }
  52. public function testExecuteSingleMethod()
  53. {
  54. $object = new ExecuteValidatorTest_Object();
  55. $this->assertTrue($this->validator->isValid($object, new Execute('validateOne')));
  56. $violations = new ConstraintViolationList();
  57. $violations->add(new ConstraintViolation(
  58. 'My message',
  59. array('parameter'),
  60. 'Root',
  61. 'foo.bar',
  62. 'invalidValue'
  63. ));
  64. $this->assertEquals($violations, $this->context->getViolations());
  65. $this->assertEquals('InitialClass', $this->context->getCurrentClass());
  66. $this->assertEquals('initialProperty', $this->context->getCurrentProperty());
  67. $this->assertEquals('InitialGroup', $this->context->getGroup());
  68. $this->assertEquals('initial.property.path', $this->context->getPropertyPath());
  69. }
  70. public function testExecuteMultipleMethods()
  71. {
  72. $object = new ExecuteValidatorTest_Object();
  73. $this->assertTrue($this->validator->isValid($object, new Execute(array(
  74. 'validateOne', 'validateTwo'
  75. ))));
  76. $violations = new ConstraintViolationList();
  77. $violations->add(new ConstraintViolation(
  78. 'My message',
  79. array('parameter'),
  80. 'Root',
  81. 'foo.bar',
  82. 'invalidValue'
  83. ));
  84. // context was reset
  85. $violations->add(new ConstraintViolation(
  86. 'Other message',
  87. array('other parameter'),
  88. 'Root',
  89. 'initial.property.path',
  90. 'otherInvalidValue'
  91. ));
  92. $this->assertEquals($violations, $this->context->getViolations());
  93. }
  94. }