8000 [Form] Made PropertyPath deterministic: "[prop]" always refers to ind… · symfony/symfony@c2a243f · GitHub
[go: up one dir, main page]

Skip to content

Commit c2a243f

Browse files
committed
[Form] Made PropertyPath deterministic: "[prop]" always refers to indices (array or ArrayAccess), "prop" always refers to properties
1 parent 2996340 commit c2a243f

File tree

2 files changed

+208
-134
lines changed

2 files changed

+208
-134
lines changed

src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php

Lines changed: 94 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,28 @@ public function testGetValueReadsArray()
2121
{
2222
$array = array('firstName' => 'Bernhard');
2323

24-
$path = new PropertyPath('firstName');
24+
$path = new PropertyPath('[firstName]');
2525

2626
$this->assertEquals('Bernhard', $path->getValue($array));
2727
}
2828

29-
public function testGetValueIgnoresSingular()
29+
/**
30+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
31+
*/
32+
public function testGetValueThrowsExceptionIfIndexNotationExpected()
3033
{
31-
$array = array('children' => 'Many');
34+
$array = array('firstName' => 'Bernhard');
3235

33-
$path = new PropertyPath('children|child');
36+
$path = new PropertyPath('firstName');
3437

35-
$this->assertEquals('Many', $path->getValue($array));
38+
$path->getValue($array);
3639
}
3740

3841
public function testGetValueReadsZeroIndex()
3942
{
4043
$array = array('Bernhard');
4144

42-
$path = new PropertyPath('0');
45+
$path = new PropertyPath('[0]');
4346

4447
$this->assertEquals('Bernhard', $path->getValue($array));
4548
}
@@ -53,20 +56,11 @@ public function testGetValueReadsIndexWithSpecialChars()
5356
$this->assertEquals('Bernhard', $path->getValue($array));
5457
}
5558

56-
public function testGetValueReadsElementWithSpecialCharsExceptDot()
57-
{
58-
$array = array('%!@$§' => 'Bernhard');
59-
60-
$path = new PropertyPath('%!@$§');
61-
62-
$this->assertEquals('Bernhard', $path->getValue($array));
63-
}
64-
6559
public function testGetValueReadsNestedIndexWithSpecialChars()
6660
{
6761
$array = array('root' => array('%!@$§.' => 'Bernhard'));
6862

69-
$path = new PropertyPath('root[%!@$§.]');
63+
$path = new PropertyPath('[root][%!@$§.]');
7064

7165
$this->assertEquals('Bernhard', $path->getValue($array));
7266
}
@@ -75,7 +69,7 @@ public function testGetValueReadsArrayWithCustomPropertyPath()
7569
{
7670
$array = array('child' => array('index' => array('firstName' => 'Bernhard')));
7771

78-
$path = new PropertyPath('child[index].firstName');
72+
$path = new PropertyPath('[child][index][firstName]');
7973

8074
$this->assertEquals('Bernhard', $path->getValue($array));
8175
}
@@ -84,7 +78,7 @@ public function testGetValueReadsArrayWithMissingIndexForCustomPropertyPath()
8478
{
8579
$array = array('child' => array('index' => array()));
8680

87-
$path = new PropertyPath('child[index].firstName');
81+
$path = new PropertyPath('[child][index][firstName]');
8882

8983
$this->assertNull($path->getValue($array));
9084
}
@@ -99,6 +93,24 @@ public function testGetValueReadsProperty()
9993
$this->assertEquals('Bernhard', $path->getValue($object));
10094
}
10195

96+
public function testGetValueIgnoresSingular()
97+
{
98+
$object = (object) array('children' => 'Many');
99+
100+
$path = new PropertyPath('children|child');
101+
102+
$this->assertEquals('Many', $path->getValue($object));
103+
}
104+
105+
public function testGetValueReadsPropertyWithSpecialCharsExceptDot()
106+
{
107+
$array = (object) array('%!@$§' => 'Bernhard');
108+
109+
$path = new PropertyPath('%!@$§');
110+
111+
$this->assertEquals('Bernhard', $path->getValue($array));
112+
}
113+
102114
public function testGetValueReadsPropertyWithCustomPropertyPath()
103115
{
104116
$object = new Author();
@@ -121,21 +133,23 @@ public function testGetValueReadsArrayAccess()
121133
$this->assertEquals('Bernhard', $path->getValue($object));
122134
}
123135

136+
/**
137+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
138+
*/
124139
public function testGetValueThrowsExceptionIfArrayAccessExpected()
125140
{
126141
$path = new PropertyPath('[firstName]');
127142

128-
$this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
129-
130143
$path->getValue(new Author());
131144
}
132145

146+
/**
147+
* @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException
148+
*/
133149
public function testGetValueThrowsExceptionIfPropertyIsNotPublic()
134150
{
135151
$path = new PropertyPath('privateProperty');
136152

137-
$this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
138-
139153
$path->getValue(new Author());
140154
}
141155

@@ -159,12 +173,13 @@ public function testGetValueCamelizesGetterNames()
159173
$this->assertEquals('Schussek', $path->getValue($object));
160174
}
161175

176+
/**
177+
* @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException
178+
*/
162179
public function testGetValueThrowsExceptionIfGetterIsNotPublic()
163180
{
164181
$path = new PropertyPath('privateGetter');
165182

166-
$this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
167-
168183
$path->getValue(new Author());
169184
}
170185

@@ -198,66 +213,82 @@ public function testGetValueReadsMagicGet()
198213
$this->assertSame('foobar', $path->getValue($object));
199214
}
200215

216+
/**
217+
* @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException
218+
*/
201219
public function testGetValueThrowsExceptionIfIsserIsNotPublic()
202220
{
203221
$path = new PropertyPath('privateIsser');
204222

205-
$this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
206-
207223
$path->getValue(new Author());
208224
}
209225

226+
/**
227+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
228+
*/
210229
public function testGetValueThrowsExceptionIfPropertyDoesNotExist()
211230
{
212231
$path = new PropertyPath('foobar');
213232

214-
$this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
215-
216233
$path->getValue(new Author());
217234
}
218235

236+
/**
237+
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
238+
*/
219239
public function testGetValueThrowsExceptionIfNotObjectOrArray()
220240
{
221241
$path = new PropertyPath('foobar');
222242

223-
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
224-
225243
$path->getValue('baz');
226244
}
227245

246+
/**
247+
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
248+
*/
228249
public function testGetValueThrowsExceptionIfNull()
229250
{
230251
$path = new PropertyPath('foobar');
231252

232-
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
233-
234253
$path->getValue(null);
235254
}
236255

256+
/**
257+
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
258+
*/
237259
public function testGetValueThrowsExceptionIfEmpty()
238260
{
239261
$path = new PropertyPath('foobar');
240262

241-
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
242-
243263
$path->getValue('');
244264
}
245265

246266
public function testSetValueUpdatesArrays()
247267
{
248268
$array = array();
249269

250-
$path = new PropertyPath('firstName');
270+
$path = new PropertyPath('[firstName]');
251271
$path->setValue($array, 'Bernhard');
252272

253273
$this->assertEquals(array('firstName' => 'Bernhard'), $array);
254274
}
255275

276+
/**
277+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
278+
*/
279+
public function testSetValueThrowsExceptionIfIndexNotationExpected()
280+
{
281+
$array = array();
282+
283+
$path = new PropertyPath('firstName');
284+
$path->setValue($array, 'Bernhard');
285+
}
286+
256287
public function testSetValueUpdatesArraysWithCustomPropertyPath()
257288
{
258289
$array = array();
259290

260-
$path = new PropertyPath('child[index].firstName');
291+
$path = new PropertyPath('[child][index][firstName]');
261292
$path->setValue($array, 'Bernhard');
262293

263294
$this->assertEquals(array('child' => array('index' => array('firstName' => 'Bernhard'))), $array);
@@ -305,12 +336,13 @@ public function testSetValueUpdateMagicSet()
305336
$this->assertEquals('foobar', $object->__get('magicProperty'));
306337
}
307338

339+
/**
340+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
341+
*/
308342
public function testSetValueThrowsExceptionIfArrayAccessExpected()
309343
{
310344
$path = new PropertyPath('[firstName]');
311345

312-
$this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
313-
314346
$path->setValue(new Author(), 'Bernhard');
315347
}
316348

@@ -334,42 +366,46 @@ public function testSetValueCamelizesSetterNames()
334366
$this->assertEquals('Schussek', $object->getLastName());
335367
}
336368

369+
/**
370+
* @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException
371+
*/
337372
public function testSetValueThrowsExceptionIfGetterIsNotPublic()
338373
{
339374
$path = new PropertyPath('privateSetter');
340375

341-
$this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
342-
343376
$path->setValue(new Author(), 'foobar');
344377
}
345378

379+
/**
380+
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
381+
*/
346382
public function testSetValueThrowsExceptionIfNotObjectOrArray()
347383
{
348384
$path = new PropertyPath('foobar');
349385
$value = 'baz';
350386

351-
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
352-
353387
$path->setValue($value, 'bam');
354388
}
355389

390+
/**
391+
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
392+
*/
356393
public function testSetValueThrowsExceptionIfNull()
357394
{
358395
$path = new PropertyPath('foobar');
359396
$value = null;
360397

361-
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
362-
363398
$path->setValue($value, 'bam');
364399
}
365400

401+
/**
402+
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
403+
*/
366404
public function testSetValueThrowsExceptionIfEmpty()
367405
{
368406
$path = new PropertyPath('foobar');
369407
$value = '';
370408

371-
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
372-
373409
$path->setValue($value, 'bam');
374410
}
375411

@@ -380,31 +416,35 @@ public function testToString()
380416
$this->assertEquals('reference.traversable[index].property', $path->__toString());
381417
}
382418

419+
/**
420+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyPathException
421+
*/
383422
public function testInvalidPropertyPath_noDotBeforeProperty()
384423
{
385-
$this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
386-
387424
new PropertyPath('[index]property');
388425
}
389426

427+
/**
428+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyPathException
429+
*/
390430
public function testInvalidPropertyPath_dotAtTheBeginning()
391431
{
392-
$this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
393-
394432
new PropertyPath('.property');
395433
}
396434

435+
/**
436+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyPathException
437+
*/
397438
public function testInvalidPropertyPath_unexpectedCharacters()
398439
{
399-
$this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
400-
401440
new PropertyPath('property.$form');
402441
}
403442

443+
/**
444+
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyPathException
445+
*/
404446
public function testInvalidPropertyPath_null()
405447
{
406-
$this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
407-
408448
new PropertyPath(null);
409449
}
410450

0 commit comments

Comments
 (0)
0