Yaffs site version 1.1
[yaffs-website] / vendor / symfony / validator / Tests / LegacyExecutionContextTest.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;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Validator\Constraints\Collection;
16 use Symfony\Component\Validator\Constraints\All;
17 use Symfony\Component\Validator\ConstraintValidatorFactory;
18 use Symfony\Component\Validator\ConstraintViolation;
19 use Symfony\Component\Validator\ConstraintViolationList;
20 use Symfony\Component\Validator\ExecutionContext;
21 use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
22 use Symfony\Component\Validator\ValidationVisitor;
23
24 /**
25  * @group legacy
26  */
27 class LegacyExecutionContextTest extends TestCase
28 {
29     const TRANS_DOMAIN = 'trans_domain';
30
31     private $visitor;
32     private $violations;
33     private $metadata;
34     private $metadataFactory;
35     private $globalContext;
36     private $translator;
37
38     /**
39      * @var ExecutionContext
40      */
41     private $context;
42
43     protected function setUp()
44     {
45         $this->visitor = $this->getMockBuilder('Symfony\Component\Validator\ValidationVisitor')
46             ->disableOriginalConstructor()
47             ->getMock();
48         $this->violations = new ConstraintViolationList();
49         $this->metadata = $this->getMockBuilder('Symfony\Component\Validator\MetadataInterface')->getMock();
50         $this->metadataFactory = $this->getMockBuilder('Symfony\Component\Validator\MetadataFactoryInterface')->getMock();
51         $this->globalContext = $this->getMockBuilder('Symfony\Component\Validator\GlobalExecutionContextInterface')->getMock();
52         $this->globalContext->expects($this->any())
53             ->method('getRoot')
54             ->will($this->returnValue('Root'));
55         $this->globalContext->expects($this->any())
56             ->method('getViolations')
57             ->will($this->returnValue($this->violations));
58         $this->globalContext->expects($this->any())
59             ->method('getVisitor')
60             ->will($this->returnValue($this->visitor));
61         $this->globalContext->expects($this->any())
62             ->method('getMetadataFactory')
63             ->will($this->returnValue($this->metadataFactory));
64         $this->translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
65         $this->context = new ExecutionContext($this->globalContext, $this->translator, self::TRANS_DOMAIN, $this->metadata, 'currentValue', 'Group', 'foo.bar');
66     }
67
68     protected function tearDown()
69     {
70         $this->globalContext = null;
71         $this->context = null;
72     }
73
74     public function testInit()
75     {
76         $this->assertCount(0, $this->context->getViolations());
77         $this->assertSame('Root', $this->context->getRoot());
78         $this->assertSame('foo.bar', $this->context->getPropertyPath());
79         $this->assertSame('Group', $this->context->getGroup());
80     }
81
82     public function testClone()
83     {
84         $clone = clone $this->context;
85
86         // Cloning the context keeps the reference to the original violation
87         // list. This way we can efficiently duplicate context instances during
88         // the validation run and only modify the properties that need to be
89         // changed.
90         $this->assertSame($this->context->getViolations(), $clone->getViolations());
91     }
92
93     public function testAddViolation()
94     {
95         $this->translator->expects($this->once())
96             ->method('trans')
97             ->with('Error', array('foo' => 'bar'))
98             ->will($this->returnValue('Translated error'));
99
100         $this->context->addViolation('Error', array('foo' => 'bar'), 'invalid');
101
102         $this->assertEquals(new ConstraintViolationList(array(
103             new ConstraintViolation(
104                 'Translated error',
105                 'Error',
106                 array('foo' => 'bar'),
107                 'Root',
108                 'foo.bar',
109                 'invalid'
110             ),
111         )), $this->context->getViolations());
112     }
113
114     public function testAddViolationUsesPreconfiguredValueIfNotPassed()
115     {
116         $this->translator->expects($this->once())
117             ->method('trans')
118             ->with('Error', array())
119             ->will($this->returnValue('Translated error'));
120
121         $this->context->addViolation('Error');
122
123         $this->assertEquals(new ConstraintViolationList(array(
124             new ConstraintViolation(
125                 'Translated error',
126                 'Error',
127                 array(),
128                 'Root',
129                 'foo.bar',
130                 'currentValue'
131             ),
132         )), $this->context->getViolations());
133     }
134
135     public function testAddViolationUsesPassedNullValue()
136     {
137         $this->translator->expects($this->once())
138             ->method('trans')
139             ->with('Error', array('foo1' => 'bar1'))
140             ->will($this->returnValue('Translated error'));
141         $this->translator->expects($this->once())
142             ->method('transChoice')
143             ->with('Choice error', 1, array('foo2' => 'bar2'))
144             ->will($this->returnValue('Translated choice error'));
145
146         // passed null value should override preconfigured value "invalid"
147         $this->context->addViolation('Error', array('foo1' => 'bar1'), null);
148         $this->context->addViolation('Choice error', array('foo2' => 'bar2'), null, 1);
149
150         $this->assertEquals(new ConstraintViolationList(array(
151             new ConstraintViolation(
152                 'Translated error',
153                 'Error',
154                 array('foo1' => 'bar1'),
155                 'Root',
156                 'foo.bar',
157                 null
158             ),
159             new ConstraintViolation(
160                 'Translated choice error',
161                 'Choice error',
162                 array('foo2' => 'bar2'),
163                 'Root',
164                 'foo.bar',
165                 null,
166                 1
167             ),
168         )), $this->context->getViolations());
169     }
170
171     public function testAddViolationAt()
172     {
173         $this->translator->expects($this->once())
174             ->method('trans')
175             ->with('Error', array('foo' => 'bar'))
176             ->will($this->returnValue('Translated error'));
177
178         // override preconfigured property path
179         $this->context->addViolationAt('bam.baz', 'Error', array('foo' => 'bar'), 'invalid');
180
181         $this->assertEquals(new ConstraintViolationList(array(
182             new ConstraintViolation(
183                 'Translated error',
184                 'Error',
185                 array('foo' => 'bar'),
186                 'Root',
187                 'foo.bar.bam.baz',
188                 'invalid'
189             ),
190         )), $this->context->getViolations());
191     }
192
193     public function testAddViolationAtUsesPreconfiguredValueIfNotPassed()
194     {
195         $this->translator->expects($this->once())
196             ->method('trans')
197             ->with('Error', array())
198             ->will($this->returnValue('Translated error'));
199
200         $this->context->addViolationAt('bam.baz', 'Error');
201
202         $this->assertEquals(new ConstraintViolationList(array(
203             new ConstraintViolation(
204                 'Translated error',
205                 'Error',
206                 array(),
207                 'Root',
208                 'foo.bar.bam.baz',
209                 'currentValue'
210             ),
211         )), $this->context->getViolations());
212     }
213
214     public function testAddViolationAtUsesPassedNullValue()
215     {
216         $this->translator->expects($this->once())
217             ->method('trans')
218             ->with('Error', array('foo' => 'bar'))
219             ->will($this->returnValue('Translated error'));
220         $this->translator->expects($this->once())
221             ->method('transChoice')
222             ->with('Choice error', 2, array('foo' => 'bar'))
223             ->will($this->returnValue('Translated choice error'));
224
225         // passed null value should override preconfigured value "invalid"
226         $this->context->addViolationAt('bam.baz', 'Error', array('foo' => 'bar'), null);
227         $this->context->addViolationAt('bam.baz', 'Choice error', array('foo' => 'bar'), null, 2);
228
229         $this->assertEquals(new ConstraintViolationList(array(
230             new ConstraintViolation(
231                 'Translated error',
232                 'Error',
233                 array('foo' => 'bar'),
234                 'Root',
235                 'foo.bar.bam.baz',
236                 null
237             ),
238             new ConstraintViolation(
239                 'Translated choice error',
240                 'Choice error',
241                 array('foo' => 'bar'),
242                 'Root',
243                 'foo.bar.bam.baz',
244                 null,
245                 2
246             ),
247         )), $this->context->getViolations());
248     }
249
250     public function testAddViolationPluralTranslationError()
251     {
252         $this->translator->expects($this->once())
253             ->method('transChoice')
254             ->with('foo')
255             ->will($this->throwException(new \InvalidArgumentException()));
256         $this->translator->expects($this->once())
257             ->method('trans')
258             ->with('foo');
259
260         $this->context->addViolation('foo', array(), null, 2);
261     }
262
263     public function testGetPropertyPath()
264     {
265         $this->assertEquals('foo.bar', $this->context->getPropertyPath());
266     }
267
268     public function testGetPropertyPathWithIndexPath()
269     {
270         $this->assertEquals('foo.bar[bam]', $this->context->getPropertyPath('[bam]'));
271     }
272
273     public function testGetPropertyPathWithEmptyPath()
274     {
275         $this->assertEquals('foo.bar', $this->context->getPropertyPath(''));
276     }
277
278     public function testGetPropertyPathWithEmptyCurrentPropertyPath()
279     {
280         $this->context = new ExecutionContext($this->globalContext, $this->translator, self::TRANS_DOMAIN, $this->metadata, 'currentValue', 'Group', '');
281
282         $this->assertEquals('bam.baz', $this->context->getPropertyPath('bam.baz'));
283     }
284
285     public function testGetPropertyPathWithNestedCollectionsAndAllMixed()
286     {
287         $constraints = new Collection(array(
288             'shelves' => new All(array('constraints' => array(
289                 new Collection(array(
290                     'name' => new ConstraintA(),
291                     'books' => new All(array('constraints' => array(
292                         new ConstraintA(),
293                     ))),
294                 )),
295             ))),
296             'name' => new ConstraintA(),
297         ));
298         $data = array(
299             'shelves' => array(
300                 array(
301                     'name' => 'Research',
302                     'books' => array('foo', 'bar'),
303                 ),
304                 array(
305                     'name' => 'VALID',
306                     'books' => array('foozy', 'VALID', 'bazzy'),
307                 ),
308             ),
309             'name' => 'Library',
310         );
311         $expectedViolationPaths = array(
312             '[shelves][0][name]',
313             '[shelves][0][books][0]',
314             '[shelves][0][books][1]',
315             '[shelves][1][books][0]',
316             '[shelves][1][books][2]',
317             '[name]',
318         );
319
320         $visitor = new ValidationVisitor('Root', $this->metadataFactory, new ConstraintValidatorFactory(), $this->translator);
321         $context = new ExecutionContext($visitor, $this->translator, self::TRANS_DOMAIN);
322         $context->validateValue($data, $constraints);
323
324         foreach ($context->getViolations() as $violation) {
325             $violationPaths[] = $violation->getPropertyPath();
326         }
327
328         $this->assertEquals($expectedViolationPaths, $violationPaths);
329     }
330 }
331
332 class ExecutionContextTest_TestClass
333 {
334     public $myProperty;
335 }