From c46519b40b28d73dd1a40915ba115ca7ce28b69a Mon Sep 17 00:00:00 2001 From: Johan DESMYTER Date: Mon, 9 May 2016 19:57:57 +0300 Subject: [PATCH] [PropertyAccess][DX] Enhance exception that say that some methods are missing if they don't --- .../Component/PropertyAccess/PropertyAccessor.php | 11 +++++++++++ .../Tests/PropertyAccessorCollectionTest.php | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index a1ff632231..23fc68e33d 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -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( diff --git a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCollectionTest.php b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCollectionTest.php index be8aba80d3..17518468eb 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCollectionTest.php +++ b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCollectionTest.php @@ -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'); + } }