Yaffs site version 1.1
[yaffs-website] / vendor / symfony / validator / Tests / ConstraintViolationListTest.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\ConstraintViolation;
16 use Symfony\Component\Validator\ConstraintViolationList;
17
18 class ConstraintViolationListTest extends TestCase
19 {
20     protected $list;
21
22     protected function setUp()
23     {
24         $this->list = new ConstraintViolationList();
25     }
26
27     protected function tearDown()
28     {
29         $this->list = null;
30     }
31
32     public function testInit()
33     {
34         $this->assertCount(0, $this->list);
35     }
36
37     public function testInitWithViolations()
38     {
39         $violation = $this->getViolation('Error');
40         $this->list = new ConstraintViolationList(array($violation));
41
42         $this->assertCount(1, $this->list);
43         $this->assertSame($violation, $this->list[0]);
44     }
45
46     public function testAdd()
47     {
48         $violation = $this->getViolation('Error');
49         $this->list->add($violation);
50
51         $this->assertCount(1, $this->list);
52         $this->assertSame($violation, $this->list[0]);
53     }
54
55     public function testAddAll()
56     {
57         $violations = array(
58             10 => $this->getViolation('Error 1'),
59             20 => $this->getViolation('Error 2'),
60             30 => $this->getViolation('Error 3'),
61         );
62         $otherList = new ConstraintViolationList($violations);
63         $this->list->addAll($otherList);
64
65         $this->assertCount(3, $this->list);
66
67         $this->assertSame($violations[10], $this->list[0]);
68         $this->assertSame($violations[20], $this->list[1]);
69         $this->assertSame($violations[30], $this->list[2]);
70     }
71
72     public function testIterator()
73     {
74         $violations = array(
75             10 => $this->getViolation('Error 1'),
76             20 => $this->getViolation('Error 2'),
77             30 => $this->getViolation('Error 3'),
78         );
79
80         $this->list = new ConstraintViolationList($violations);
81
82         // indices are reset upon adding -> array_values()
83         $this->assertSame(array_values($violations), iterator_to_array($this->list));
84     }
85
86     public function testArrayAccess()
87     {
88         $violation = $this->getViolation('Error');
89         $this->list[] = $violation;
90
91         $this->assertSame($violation, $this->list[0]);
92         $this->assertTrue(isset($this->list[0]));
93
94         unset($this->list[0]);
95
96         $this->assertFalse(isset($this->list[0]));
97
98         $this->list[10] = $violation;
99
100         $this->assertSame($violation, $this->list[10]);
101         $this->assertTrue(isset($this->list[10]));
102     }
103
104     public function testToString()
105     {
106         $this->list = new ConstraintViolationList(array(
107             $this->getViolation('Error 1', 'Root'),
108             $this->getViolation('Error 2', 'Root', 'foo.bar'),
109             $this->getViolation('Error 3', 'Root', '[baz]'),
110             $this->getViolation('Error 4', '', 'foo.bar'),
111             $this->getViolation('Error 5', '', '[baz]'),
112         ));
113
114         $expected = <<<'EOF'
115 Root:
116     Error 1
117 Root.foo.bar:
118     Error 2
119 Root[baz]:
120     Error 3
121 foo.bar:
122     Error 4
123 [baz]:
124     Error 5
125
126 EOF;
127
128         $this->assertEquals($expected, (string) $this->list);
129     }
130
131     protected function getViolation($message, $root = null, $propertyPath = null)
132     {
133         return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
134     }
135 }