47346324b88a38907af23f7af7e7a4121e9a5031
[yaffs-website] / vendor / symfony / validator / Tests / Validator / AbstractLegacyApiTest.php
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\Validator;
13
14 use Symfony\Component\Validator\Constraints\Callback;
15 use Symfony\Component\Validator\Constraints\Valid;
16 use Symfony\Component\Validator\ConstraintViolationInterface;
17 use Symfony\Component\Validator\ExecutionContextInterface;
18 use Symfony\Component\Validator\MetadataFactoryInterface;
19 use Symfony\Component\Validator\Tests\Fixtures\Entity;
20 use Symfony\Component\Validator\Tests\Fixtures\Reference;
21 use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
22
23 /**
24  * Verifies that a validator satisfies the API of Symfony < 2.5.
25  *
26  * @author Bernhard Schussek <bschussek@gmail.com>
27  * @group legacy
28  */
29 abstract class AbstractLegacyApiTest extends AbstractValidatorTest
30 {
31     /**
32      * @var LegacyValidatorInterface
33      */
34     protected $validator;
35
36     /**
37      * @param MetadataFactoryInterface $metadataFactory
38      *
39      * @return LegacyValidatorInterface
40      */
41     abstract protected function createValidator(MetadataFactoryInterface $metadataFactory, array $objectInitializers = array());
42
43     protected function setUp()
44     {
45         parent::setUp();
46
47         $this->validator = $this->createValidator($this->metadataFactory);
48     }
49
50     protected function validate($value, $constraints = null, $groups = null)
51     {
52         if (null === $constraints) {
53             $constraints = new Valid();
54         }
55
56         if ($constraints instanceof Valid) {
57             return $this->validator->validate($value, $groups, $constraints->traverse, $constraints->deep);
58         }
59
60         return $this->validator->validateValue($value, $constraints, $groups);
61     }
62
63     protected function validateProperty($object, $propertyName, $groups = null)
64     {
65         return $this->validator->validateProperty($object, $propertyName, $groups);
66     }
67
68     protected function validatePropertyValue($object, $propertyName, $value, $groups = null)
69     {
70         return $this->validator->validatePropertyValue($object, $propertyName, $value, $groups);
71     }
72
73     /**
74      * @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
75      */
76     public function testTraversableTraverseDisabled()
77     {
78         $test = $this;
79         $entity = new Entity();
80         $traversable = new \ArrayIterator(array('key' => $entity));
81
82         $callback = function () use ($test) {
83             $test->fail('Should not be called');
84         };
85
86         $this->metadata->addConstraint(new Callback(array(
87             'callback' => $callback,
88             'groups' => 'Group',
89         )));
90
91         $this->validator->validate($traversable, 'Group');
92     }
93
94     /**
95      * @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
96      */
97     public function testRecursiveTraversableRecursiveTraversalDisabled()
98     {
99         $test = $this;
100         $entity = new Entity();
101         $traversable = new \ArrayIterator(array(
102             2 => new \ArrayIterator(array('key' => $entity)),
103         ));
104
105         $callback = function () use ($test) {
106             $test->fail('Should not be called');
107         };
108
109         $this->metadata->addConstraint(new Callback(array(
110             'callback' => $callback,
111             'groups' => 'Group',
112         )));
113
114         $this->validator->validate($traversable, 'Group');
115     }
116
117     public function testValidateInContext()
118     {
119         $test = $this;
120         $entity = new Entity();
121         $entity->reference = new Reference();
122
123         $callback1 = function ($value, ExecutionContextInterface $context) use ($test) {
124             $previousValue = $context->getValue();
125             $previousMetadata = $context->getMetadata();
126             $previousPath = $context->getPropertyPath();
127             $previousGroup = $context->getGroup();
128
129             $context->validate($value->reference, 'subpath');
130
131             // context changes shouldn't leak out of the validate() call
132             $test->assertSame($previousValue, $context->getValue());
133             $test->assertSame($previousMetadata, $context->getMetadata());
134             $test->assertSame($previousPath, $context->getPropertyPath());
135             $test->assertSame($previousGroup, $context->getGroup());
136         };
137
138         $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
139             $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
140             $test->assertNull($context->getPropertyName());
141             $test->assertSame('subpath', $context->getPropertyPath());
142             $test->assertSame('Group', $context->getGroup());
143             $test->assertSame($test->referenceMetadata, $context->getMetadata());
144             $test->assertSame($test->metadataFactory, $context->getMetadataFactory());
145             $test->assertSame($entity, $context->getRoot());
146             $test->assertSame($entity->reference, $context->getValue());
147             $test->assertSame($entity->reference, $value);
148
149             $context->addViolation('Message %param%', array('%param%' => 'value'));
150         };
151
152         $this->metadata->addConstraint(new Callback(array(
153             'callback' => $callback1,
154             'groups' => 'Group',
155         )));
156         $this->referenceMetadata->addConstraint(new Callback(array(
157             'callback' => $callback2,
158             'groups' => 'Group',
159         )));
160
161         $violations = $this->validator->validate($entity, 'Group');
162
163         /* @var ConstraintViolationInterface[] $violations */
164         $this->assertCount(1, $violations);
165         $this->assertSame('Message value', $violations[0]->getMessage());
166         $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
167         $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
168         $this->assertSame('subpath', $violations[0]->getPropertyPath());
169         $this->assertSame($entity, $violations[0]->getRoot());
170         $this->assertSame($entity->reference, $violations[0]->getInvalidValue());
171         $this->assertNull($violations[0]->getPlural());
172         $this->assertNull($violations[0]->getCode());
173     }
174
175     public function testValidateArrayInContext()
176     {
177         $test = $this;
178         $entity = new Entity();
179         $entity->reference = new Reference();
180
181         $callback1 = function ($value, ExecutionContextInterface $context) use ($test) {
182             $previousValue = $context->getValue();
183             $previousMetadata = $context->getMetadata();
184             $previousPath = $context->getPropertyPath();
185             $previousGroup = $context->getGroup();
186
187             $context->validate(array('key' => $value->reference), 'subpath');
188
189             // context changes shouldn't leak out of the validate() call
190             $test->assertSame($previousValue, $context->getValue());
191             $test->assertSame($previousMetadata, $context->getMetadata());
192             $test->assertSame($previousPath, $context->getPropertyPath());
193             $test->assertSame($previousGroup, $context->getGroup());
194         };
195
196         $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
197             $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
198             $test->assertNull($context->getPropertyName());
199             $test->assertSame('subpath[key]', $context->getPropertyPath());
200             $test->assertSame('Group', $context->getGroup());
201             $test->assertSame($test->referenceMetadata, $context->getMetadata());
202             $test->assertSame($test->metadataFactory, $context->getMetadataFactory());
203             $test->assertSame($entity, $context->getRoot());
204             $test->assertSame($entity->reference, $context->getValue());
205             $test->assertSame($entity->reference, $value);
206
207             $context->addViolation('Message %param%', array('%param%' => 'value'));
208         };
209
210         $this->metadata->addConstraint(new Callback(array(
211             'callback' => $callback1,
212             'groups' => 'Group',
213         )));
214         $this->referenceMetadata->addConstraint(new Callback(array(
215             'callback' => $callback2,
216             'groups' => 'Group',
217         )));
218
219         $violations = $this->validator->validate($entity, 'Group');
220
221         /* @var ConstraintViolationInterface[] $violations */
222         $this->assertCount(1, $violations);
223         $this->assertSame('Message value', $violations[0]->getMessage());
224         $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
225         $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
226         $this->assertSame('subpath[key]', $violations[0]->getPropertyPath());
227         $this->assertSame($entity, $violations[0]->getRoot());
228         $this->assertSame($entity->reference, $violations[0]->getInvalidValue());
229         $this->assertNull($violations[0]->getPlural());
230         $this->assertNull($violations[0]->getCode());
231     }
232
233     public function testAddCustomizedViolation()
234     {
235         $entity = new Entity();
236
237         $callback = function ($value, ExecutionContextInterface $context) {
238             $context->addViolation(
239                 'Message %param%',
240                 array('%param%' => 'value'),
241                 'Invalid value',
242                 2,
243                 'Code'
244             );
245         };
246
247         $this->metadata->addConstraint(new Callback($callback));
248
249         $violations = $this->validator->validate($entity);
250
251         /* @var ConstraintViolationInterface[] $violations */
252         $this->assertCount(1, $violations);
253         $this->assertSame('Message value', $violations[0]->getMessage());
254         $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
255         $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
256         $this->assertSame('', $violations[0]->getPropertyPath());
257         $this->assertSame($entity, $violations[0]->getRoot());
258         $this->assertSame('Invalid value', $violations[0]->getInvalidValue());
259         $this->assertSame(2, $violations[0]->getPlural());
260         $this->assertSame('Code', $violations[0]->getCode());
261     }
262
263     public function testInitializeObjectsOnFirstValidation()
264     {
265         $test = $this;
266         $entity = new Entity();
267         $entity->initialized = false;
268
269         // prepare initializers that set "initialized" to true
270         $initializer1 = $this->getMockBuilder('Symfony\\Component\\Validator\\ObjectInitializerInterface')->getMock();
271         $initializer2 = $this->getMockBuilder('Symfony\\Component\\Validator\\ObjectInitializerInterface')->getMock();
272
273         $initializer1->expects($this->once())
274             ->method('initialize')
275             ->with($entity)
276             ->will($this->returnCallback(function ($object) {
277                 $object->initialized = true;
278             }));
279
280         $initializer2->expects($this->once())
281             ->method('initialize')
282             ->with($entity);
283
284         $this->validator = $this->createValidator($this->metadataFactory, array(
285             $initializer1,
286             $initializer2,
287         ));
288
289         // prepare constraint which
290         // * checks that "initialized" is set to true
291         // * validates the object again
292         $callback = function ($object, ExecutionContextInterface $context) use ($test) {
293             $test->assertTrue($object->initialized);
294
295             // validate again in same group
296             $context->validate($object);
297
298             // validate again in other group
299             $context->validate($object, '', 'SomeGroup');
300         };
301
302         $this->metadata->addConstraint(new Callback($callback));
303
304         $this->validate($entity);
305
306         $this->assertTrue($entity->initialized);
307     }
308
309     public function testGetMetadataFactory()
310     {
311         $this->assertSame($this->metadataFactory, $this->validator->getMetadataFactory());
312     }
313 }