]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
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\Form\Tests\Extension\Validator\ViolationMapper; | |
13 | ||
14 | use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationPath; | |
15 | ||
16 | /** | |
17 | * @author Bernhard Schussek <bschussek@gmail.com> | |
18 | */ | |
19 | class ViolationPathTest extends \PHPUnit_Framework_TestCase | |
20 | { | |
21 | public function providePaths() | |
22 | { | |
23 | return array( | |
24 | array('children[address]', array( | |
25 | array('address', true, true), | |
26 | )), | |
27 | array('children[address].children[street]', array( | |
28 | array('address', true, true), | |
29 | array('street', true, true), | |
30 | )), | |
31 | array('children[address][street]', array( | |
32 | array('address', true, true), | |
33 | ), 'children[address]'), | |
34 | array('children[address].data', array( | |
35 | array('address', true, true), | |
36 | ), 'children[address]'), | |
37 | array('children[address].data.street', array( | |
38 | array('address', true, true), | |
39 | array('street', false, false), | |
40 | )), | |
41 | array('children[address].data[street]', array( | |
42 | array('address', true, true), | |
43 | array('street', false, true), | |
44 | )), | |
45 | array('children[address].children[street].data.name', array( | |
46 | array('address', true, true), | |
47 | array('street', true, true), | |
48 | array('name', false, false), | |
49 | )), | |
50 | array('children[address].children[street].data[name]', array( | |
51 | array('address', true, true), | |
52 | array('street', true, true), | |
53 | array('name', false, true), | |
54 | )), | |
55 | array('data.address', array( | |
56 | array('address', false, false), | |
57 | )), | |
58 | array('data[address]', array( | |
59 | array('address', false, true), | |
60 | )), | |
61 | array('data.address.street', array( | |
62 | array('address', false, false), | |
63 | array('street', false, false), | |
64 | )), | |
65 | array('data[address].street', array( | |
66 | array('address', false, true), | |
67 | array('street', false, false), | |
68 | )), | |
69 | array('data.address[street]', array( | |
70 | array('address', false, false), | |
71 | array('street', false, true), | |
72 | )), | |
73 | array('data[address][street]', array( | |
74 | array('address', false, true), | |
75 | array('street', false, true), | |
76 | )), | |
77 | // A few invalid examples | |
78 | array('data', array(), ''), | |
79 | array('children', array(), ''), | |
80 | array('children.address', array(), ''), | |
81 | array('children.address[street]', array(), ''), | |
82 | ); | |
83 | } | |
84 | ||
85 | /** | |
86 | * @dataProvider providePaths | |
87 | */ | |
88 | public function testCreatePath($string, $entries, $slicedPath = null) | |
89 | { | |
90 | if (null === $slicedPath) { | |
91 | $slicedPath = $string; | |
92 | } | |
93 | ||
94 | $path = new ViolationPath($string); | |
95 | ||
96 | $this->assertSame($slicedPath, $path->__toString()); | |
97 | $this->assertSame(count($entries), count($path->getElements())); | |
98 | $this->assertSame(count($entries), $path->getLength()); | |
99 | ||
100 | foreach ($entries as $index => $entry) { | |
101 | $this->assertEquals($entry[0], $path->getElement($index)); | |
102 | $this->assertSame($entry[1], $path->mapsForm($index)); | |
103 | $this->assertSame($entry[2], $path->isIndex($index)); | |
104 | $this->assertSame(!$entry[2], $path->isProperty($index)); | |
105 | } | |
106 | } | |
107 | ||
108 | public function provideParents() | |
109 | { | |
110 | return array( | |
111 | array('children[address]', null), | |
112 | array('children[address].children[street]', 'children[address]'), | |
113 | array('children[address].data.street', 'children[address]'), | |
114 | array('children[address].data[street]', 'children[address]'), | |
115 | array('data.address', null), | |
116 | array('data.address.street', 'data.address'), | |
117 | array('data.address[street]', 'data.address'), | |
118 | array('data[address].street', 'data[address]'), | |
119 | array('data[address][street]', 'data[address]'), | |
120 | ); | |
121 | } | |
122 | ||
123 | /** | |
124 | * @dataProvider provideParents | |
125 | */ | |
126 | public function testGetParent($violationPath, $parentPath) | |
127 | { | |
128 | $path = new ViolationPath($violationPath); | |
129 | $parent = $parentPath === null ? null : new ViolationPath($parentPath); | |
130 | ||
131 | $this->assertEquals($parent, $path->getParent()); | |
132 | } | |
133 | ||
134 | public function testGetElement() | |
135 | { | |
136 | $path = new ViolationPath('children[address].data[street].name'); | |
137 | ||
138 | $this->assertEquals('street', $path->getElement(1)); | |
139 | } | |
140 | ||
141 | /** | |
142 | * @expectedException \OutOfBoundsException | |
143 | */ | |
144 | public function testGetElementDoesNotAcceptInvalidIndices() | |
145 | { | |
146 | $path = new ViolationPath('children[address].data[street].name'); | |
147 | ||
148 | $path->getElement(3); | |
149 | } | |
150 | ||
151 | /** | |
152 | * @expectedException \OutOfBoundsException | |
153 | */ | |
154 | public function testGetElementDoesNotAcceptNegativeIndices() | |
155 | { | |
156 | $path = new ViolationPath('children[address].data[street].name'); | |
157 | ||
158 | $path->getElement(-1); | |
159 | } | |
160 | ||
161 | public function testIsProperty() | |
162 | { | |
163 | $path = new ViolationPath('children[address].data[street].name'); | |
164 | ||
165 | $this->assertFalse($path->isProperty(1)); | |
166 | $this->assertTrue($path->isProperty(2)); | |
167 | } | |
168 | ||
169 | /** | |
170 | * @expectedException \OutOfBoundsException | |
171 | */ | |
172 | public function testIsPropertyDoesNotAcceptInvalidIndices() | |
173 | { | |
174 | $path = new ViolationPath('children[address].data[street].name'); | |
175 | ||
176 | $path->isProperty(3); | |
177 | } | |
178 | ||
179 | /** | |
180 | * @expectedException \OutOfBoundsException | |
181 | */ | |
182 | public function testIsPropertyDoesNotAcceptNegativeIndices() | |
183 | { | |
184 | $path = new ViolationPath('children[address].data[street].name'); | |
185 | ||
186 | $path->isProperty(-1); | |
187 | } | |
188 | ||
189 | public function testIsIndex() | |
190 | { | |
191 | $path = new ViolationPath('children[address].data[street].name'); | |
192 | ||
193 | $this->assertTrue($path->isIndex(1)); | |
194 | $this->assertFalse($path->isIndex(2)); | |
195 | } | |
196 | ||
197 | /** | |
198 | * @expectedException \OutOfBoundsException | |
199 | */ | |
200 | public function testIsIndexDoesNotAcceptInvalidIndices() | |
201 | { | |
202 | $path = new ViolationPath('children[address].data[street].name'); | |
203 | ||
204 | $path->isIndex(3); | |
205 | } | |
206 | ||
207 | /** | |
208 | * @expectedException \OutOfBoundsException | |
209 | */ | |
210 | public function testIsIndexDoesNotAcceptNegativeIndices() | |
211 | { | |
212 | $path = new ViolationPath('children[address].data[street].name'); | |
213 | ||
214 | $path->isIndex(-1); | |
215 | } | |
216 | ||
217 | public function testMapsForm() | |
218 | { | |
219 | $path = new ViolationPath('children[address].data[street].name'); | |
220 | ||
221 | $this->assertTrue($path->mapsForm(0)); | |
222 | $this->assertFalse($path->mapsForm(1)); | |
223 | $this->assertFalse($path->mapsForm(2)); | |
224 | } | |
225 | ||
226 | /** | |
227 | * @expectedException \OutOfBoundsException | |
228 | */ | |
229 | public function testMapsFormDoesNotAcceptInvalidIndices() | |
230 | { | |
231 | $path = new ViolationPath('children[address].data[street].name'); | |
232 | ||
233 | $path->mapsForm(3); | |
234 | } | |
235 | ||
236 | /** | |
237 | * @expectedException \OutOfBoundsException | |
238 | */ | |
239 | public function testMapsFormDoesNotAcceptNegativeIndices() | |
240 | { | |
241 | $path = new ViolationPath('children[address].data[street].name'); | |
242 | ||
243 | $path->mapsForm(-1); | |
244 | } | |
245 | } |