-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathConstraintViolationListTest.php
166 lines (133 loc) · 4.52 KB
/
ConstraintViolationListTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
class ConstraintViolationListTest extends TestCase
{
protected ConstraintViolationList $list;
protected function setUp(): void
{
$this->list = new ConstraintViolationList();
}
public function testInit()
{
$this->assertCount(0, $this->list);
}
public function testInitWithViolations()
{
$violation = $this->getViolation('Error');
$this->list = new ConstraintViolationList([$violation]);
$this->assertCount(1, $this->list);
$this->assertSame($violation, $this->list[0]);
}
public function testAdd()
{
$violation = $this->getViolation('Error');
$this->list->add($violation);
$this->assertCount(1, $this->list);
$this->assertSame($violation, $this->list[0]);
}
public function testAddAll()
{
$violations = [
10 => $this->getViolation('Error 1'),
20 => $this->getViolation('Error 2'),
30 => $this->getViolation('Error 3'),
];
$otherList = new ConstraintViolationList($violations);
$this->list->addAll($otherList);
$this->assertCount(3, $this->list);
$this->assertSame($violations[10], $this->list[0]);
$this->assertSame($violations[20], $this->list[1]);
$this->assertSame($violations[30], $this->list[2]);
}
public function testIterator()
{
$violations = [
10 => $this->getViolation('Error 1'),
20 => $this->getViolation('Error 2'),
30 => $this->getViolation('Error 3'),
];
$this->list = new ConstraintViolationList($violations);
// indices are reset upon adding -> array_values()
$this->assertSame(array_values($violations), iterator_to_array($this->list));
}
public function testArrayAccess()
{
$violation = $this->getViolation('Error');
$this->list[] = $violation;
$this->assertSame($violation, $this->list[0]);
$this->assertArrayHasKey(0, $this->list);
unset($this->list[0]);
$this->assertArrayNotHasKey(0, $this->list);
$this->list[10] = $violation;
$this->assertSame($violation, $this->list[10]);
$this->assertArrayHasKey(10, $this->list);
}
public function testToString()
{
$this->list = new ConstraintViolationList([
$this->getViolation('Error 1', 'Root'),
$this->getViolation('Error 2', 'Root', 'foo.bar'),
$this->getViolation('Error 3', 'Root', '[baz]'),
$this->getViolation('Error 4', '', 'foo.bar'),
$this->getViolation('Error 5', '', '[baz]'),
]);
$expected = <<<'EOF'
Root:
Error 1
Root.foo.bar:
Error 2
Root[baz]:
Error 3
foo.bar:
Error 4
[baz]:
Error 5
EOF;
$this->assertEquals($expected, (string) $this->list);
}
/**
* @dataProvider findByCodesProvider
*/
public function testFindByCodes($code, $violationsCount)
{
$violations = [
$this->getViolation('Error', null, null, 'code1'),
$this->getViolation('Error', null, null, 'code1'),
$this->getViolation('Error', null, null, 'code2'),
];
$list = new ConstraintViolationList($violations);
$specificErrors = $list->findByCodes($code);
$this->assertInstanceOf(ConstraintViolationList::class, $specificErrors);
$this->assertCount($violationsCount, $specificErrors);
}
public static function findByCodesProvider()
{
return [
['code1', 2],
[['code1', 'code2'], 3],
['code3', 0],
];
}
public function testCreateFromMessage()
{
$list = ConstraintViolationList::createFromMessage('my message');
$this->assertCount(1, $list);
$this->assertInstanceOf(ConstraintViolation::class, $list[0]);
$this->assertSame('my message', $list[0]->getMessage());
}
protected function getViolation($message, $root = null, $propertyPath = null, $code = null)
{
return new ConstraintViolation($message, $message, [], $root, $propertyPath, null, null, $code);
}
}