8000 [Form] Also display the hint about adder/remover on invalid property … · defrag/symfony@6ad4018 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ad4018

Browse files
author
Bart van den Burg
committed
[Form] Also display the hint about adder/remover on invalid property access
1 parent 9e32d90 commit 6ad4018

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ class PropertyPathCollectionTest_CarNoAdderAndRemover
7878
public function getAxes() {}
7979
}
8080

81+
class PropertyPathCollectionTest_CarNoAdderAndRemoverWithProperty
82+
{
83+
protected $axes = array();
84+
85+
public function getAxes() {}
86+
}
87+
8188
class PropertyPathCollectionTest_CompositeCar
8289
{
8390
public function getStructure() {}
@@ -244,42 +251,56 @@ public function testMapFormToDataFailsIfOnlyRemoverFound()
244251
/**
245252
* @dataProvider noAdderRemoverData
246253
*/
247-
public function testNoAdderAndRemoverThrowsSensibleError($path, $message)
254+
public function testNoAdderAndRemoverThrowsSensibleError($car, $path, $message)
248255
{
249-
$car = $this->getMock(__CLASS__ . '_CarNoAdderAndRemover');
250-
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
251-
$axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
256+
$axes = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
252257

253258
try {
254-
$path->setValue($car, $axesAfter);
259+
$path->setValue($car, $axes);
255260
$this->fail('An expected exception was not thrown!');
256-
} catch (\Symfony\Component\Form\Exception\InvalidPropertyException $e) {
257-
$this->assertEquals(str_replace("{class}", get_class($car), $message), $e->getMessage());
261+
} catch (\Symfony\Component\Form\Exception\FormException $e) {
262+
$this->assertEquals($message, $e->getMessage());
258263
}
259264
}
260265

261266
public function noAdderRemoverData()
262267
{
263268
$data = array();
264269

270+
$car = $this->getMock(__CLASS__ . '_CarNoAdderAndRemover');
265271
$propertyPath = new PropertyPath('axes');
266272
$expectedMessage = sprintf(
267273
'Neither element "axes" nor method "setAxes()" exists in class '
268-
.'"{class}", nor could adders and removers be found based on the '
274+
.'"%s", nor could adders and removers be found based on the '
269275
.'guessed singulars: %s (provide a singular by suffixing the '
270276
.'property path with "|{singular}" to override the guesser)',
277+
get_class($car),
271278
implode(', ', (array) $singulars = FormUtil::singularify('Axes'))
272279
);
273-
$data[] = array($propertyPath, $expectedMessage);
280+
$data[] = array($car, $propertyPath, $expectedMessage);
274281

275282
$propertyPath = new PropertyPath('axes|boo');
276283
$expectedMessage = sprintf(
277284
'Neither element "axes" nor method "setAxes()" exists in class '
278-
.'"{class}", nor could adders and removers be found based on the '
285+
.'"%s", nor could adders and removers be found based on the '
279286
.'passed singular: %s',
287+
get_class($car),
280288
'boo'
281289
);
282-
$data[] = array($propertyPath, $expectedMessage);
290+
$data[] = array($car, $propertyPath, $expectedMessage);
291+
292+
$car = $this->getMock(__CLASS__ . '_CarNoAdderAndRemoverWithProperty');
293+
$propertyPath = new PropertyPath('axes');
294+
$expectedMessage = sprintf(
295+
'Property "axes" is not public in class "%s", nor could adders and '
296+
.'removers be found based on the guessed singulars: %s '
297+
.'(provide a singular by suffixing the property path with '
298+
.'"|{singular}" to override the guesser). Maybe you should '
299+
.'create the method "setAxes()"?',
300+
get_class($car),
301+
implode(', ', (array) $singulars = FormUtil::singularify('Axes'))
302+
);
303+
$data[] = array($car, $propertyPath, $expectedMessage);
283304

284305
return $data;
285306
}

src/Symfony/Component/Form/Util/PropertyPath.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ protected function writeProperty(&$objectOrArray, $property, $singular, $isIndex
516516
$objectOrArray->$property = $value;
517517
} elseif ($reflClass->hasProperty($property)) {
518518
if (!$reflClass->getProperty($property)->isPublic()) {
519-
throw new PropertyAccessDeniedException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "%s()"?', $property, $reflClass->name, $setter));
519+
throw new PropertyAccessDeniedException(sprintf('Property "%s" is not public in class "%s"%s. Maybe you should create the method "%s()"?', $property, $reflClass->name, $adderRemoverError, $setter));
520520
}
521521

522522
$objectOrArray->$property = $value;

0 commit comments

Comments
 (0)
0