[PropertyAccess][DX] Enhance exception that say that some methods are missing if they don't

This commit is contained in:
Johan DESMYTER 2016-05-09 19:57:57 +03:00 committed by Fabien Potencier
parent a53aba327e
commit c46519b40b
2 changed files with 22 additions and 0 deletions

View File

@ -714,6 +714,17 @@ class PropertyAccessor implements PropertyAccessorInterface
// we call the getter and hope the __call do the job
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_MAGIC;
$access[self::ACCESS_NAME] = $setter;
} elseif (null !== $methods = $this->findAdderAndRemover($reflClass, $singulars)) {
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
$access[self::ACCESS_NAME] = sprintf(
'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
'the new value must be an array or an instance of \Traversable, '.
'"%s" given.',
$property,
$reflClass->name,
implode('()", "', $methods),
is_object($value) ? get_class($value) : gettype($value)
);
} else {
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
$access[self::ACCESS_NAME] = sprintf(

View File

@ -194,4 +194,15 @@ abstract class PropertyAccessorCollectionTest extends PropertyAccessorArrayAcces
$this->assertFalse($this->propertyAccessor->isWritable($car, 'axes', $axes));
}
/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
* expectedExceptionMessageRegExp /The property "axes" in class "Mock_PropertyAccessorCollectionTest_Car[^"]*" can be defined with the methods "addAxis()", "removeAxis()" but the new value must be an array or an instance of \Traversable, "string" given./
*/
public function testSetValueFailsIfAdderAndRemoverExistButValueIsNotTraversable()
{
$car = $this->getMock(__CLASS__.'_Car');
$this->propertyAccessor->setValue($car, 'axes', 'Not an array or Traversable');
}
}