|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\ConstraintValidatorInterface; |
| 15 | +use Symfony\Component\Validator\ConstraintViolation; |
| 16 | +use Symfony\Component\Validator\Context\ExecutionContext; |
| 17 | +use Symfony\Component\Validator\Context\ExecutionContextInterface; |
| 18 | +use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 19 | +use Symfony\Component\Validator\Mapping\PropertyMetadata; |
| 20 | +use Symfony\Component\Validator\Tests\Fixtures\StubGlobalExecutionContext; |
| 21 | + |
| 22 | +/** |
| 23 | + * @since 2.5.3 |
| 24 | + * @author Bernhard Schussek <bschussek@gmail.com> |
| 25 | + */ |
| 26 | +abstract class AbstractConstraintValidatorTest extends \PHPUnit_Framework_TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var ExecutionContextInterface |
| 30 | + */ |
| 31 | + protected $context; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ConstraintValidatorInterface |
| 35 | + */ |
| 36 | + protected $validator; |
| 37 | + |
| 38 | + protected $group; |
| 39 | + |
| 40 | + protected $metadata; |
| 41 | + |
| 42 | + protected $object; |
| 43 | + |
| 44 | + protected $value; |
| 45 | + |
| 46 | + protected $root; |
| 47 | + |
| 48 | + protected $propertyPath; |
| 49 | + |
| 50 | + protected function setUp() |
| 51 | + { |
| 52 | + $this->group = 'MyGroup'; |
| 53 | + $this->metadata = null; |
| 54 | + $this->object = null; |
| 55 | + $this->value = 'InvalidValue'; |
| 56 | + $this->root = 'root'; |
| 57 | + $this->propertyPath = 'property.path'; |
| 58 | + $this->context = $this->createContext(); |
| 59 | + $this->validator = $this->createValidator(); |
| 60 | + $this->validator->initialize($this->context); |
| 61 | + |
| 62 | + \Locale::setDefault('en'); |
| 63 | + } |
| 64 | + |
| 65 | + protected function createContext() |
| 66 | + { |
| 67 | + $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface'); |
| 68 | + |
| 69 | + return $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext') |
| 70 | + ->setConstructorArgs(array( |
| 71 | + new StubGlobalExecutionContext($this->root), |
| 72 | + $translator, |
| 73 | + null, |
| 74 | + $this->metadata, |
| 75 | + $this->value, |
| 76 | + $this->group, |
| 77 | + $this->propertyPath |
| 78 | + )) |
| 79 | + ->setMethods(array('validate', 'validateValue')) |
| 80 | + ->getMock(); |
| 81 | + } |
| 82 | + |
| 83 | + protected function createViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null) |
| 84 | + { |
| 85 | + return new ConstraintViolation( |
| 86 | + null, |
| 87 | + $message, |
| 88 | + $parameters, |
| 89 | + $this->root, |
| 90 | + $propertyPath, |
| 91 | + $invalidValue, |
| 92 | + $plural, |
| 93 | + $code |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + protected function setGroup($group) |
| 98 | + { |
| 99 | + $this->group = $group; |
| 100 | + $this->context = $this->createContext(); |
| 101 | + $this->validator->initialize($this->context); |
| 102 | + } |
| 103 | + |
| 104 | + protected function setObject($object) |
| 105 | + { |
| 106 | + $this->object = $object; |
| 107 | + $this->metadata = is_object($object) |
| 108 | + ? new ClassMetadata(get_class($object)) |
| 109 | + : null; |
| 110 | + $this->context = $this->createContext(); |
| 111 | + $this->validator->initialize($this->context); |
| 112 | + } |
| 113 | + |
| 114 | + protected function setProperty($object, $property) |
| 115 | + { |
| 116 | + $this->object = $object; |
| 117 | + $this->metadata = is_object($object) |
| 118 | + ? new PropertyMetadata(get_class($object), $property) |
| 119 | + : null; |
| 120 | + $this->context = $this->createContext(); |
| 121 | + $this->validator->initialize($this->context); |
| 122 | + } |
| 123 | + |
| 124 | + protected function setValue($value) |
| 125 | + { |
| 126 | + $this->value = $value; |
| 127 | + $this->context = $this->createContext(); |
| 128 | + $this->validator->initialize($this->context); |
| 129 | + } |
| 130 | + |
| 131 | + protected function setRoot($root) |
| 132 | + { |
| 133 | + $this->root = $root; |
| 134 | + $this->context = $this->createContext(); |
| 135 | + $this->validator->initialize($this->context); |
| 136 | + } |
| 137 | + |
| 138 | + protected function setPropertyPath($propertyPath) |
| 139 | + { |
| 140 | + $this->propertyPath = $propertyPath; |
| 141 | + $this->context = $this->createContext(); |
| 142 | + $this->validator->initialize($this->context); |
| 143 | + } |
| 144 | + |
| 145 | + protected function expectNoValidate() |
| 146 | + { |
| 147 | + $this->context->expects($this->never()) |
| 148 | + ->method('validate'); |
| 149 | + $this->context->expects($this->never()) |
| 150 | + ->method('validateValue'); |
| 151 | + } |
| 152 | + |
| 153 | + protected function expectValidateAt($i, $propertyPath, $value, $group) |
| 154 | + { |
| 155 | + $this->context->expects($this->at($i)) |
| 156 | + ->method('validate') |
| 157 | + ->with($value, $propertyPath, $group); |
| 158 | + } |
| 159 | + |
| 160 | + protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group) |
| 161 | + { |
| 162 | + $this->context->expects($this->at($i)) |
| 163 | + ->method('validateValue') |
| 164 | + ->with($value, $constraints, $propertyPath, $group); |
| 165 | + } |
| 166 | + |
| 167 | + protected function assertNoViolation() |
| 168 | + { |
| 169 | + $this->assertCount(0, $this->context->getViolations()); |
| 170 | + } |
| 171 | + |
| 172 | + protected function assertViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null) |
| 173 | + { |
| 174 | + $violations = $this->context->getViolations(); |
| 175 | + |
| 176 | + $this->assertCount(1, $violations); |
| 177 | + $this->assertEquals($this->createViolation($message, $parameters, $propertyPath, $invalidValue, $plural, $code), $violations[0]); |
| 178 | + } |
| 179 | + |
| 180 | + protected function assertViolations(array $expected) |
| 181 | + { |
| 182 | + $violations = $this->context->getViolations(); |
| 183 | + |
| 184 | + $this->assertCount(count($expected), $violations); |
| 185 | + |
| 186 | + $i = 0; |
| 187 | + |
| 188 | + foreach ($expected as $violation) { |
| 189 | + $this->assertEquals($violation, $violations[$i++]); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + abstract protected function createValidator(); |
| 194 | +} |
0 commit comments