Skip to content

Commit

Permalink
bug #18732 [PropertyAccess][DX] Enhance exception that say that some …
Browse files Browse the repository at this point in the history
…methods are missing if they don't (nykopol)

This PR was squashed before being merged into the 2.7 branch (closes #18732).

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18694
| License       | MIT
| Doc PR        |

When you try do define a manyToMany association but you don't give an array or \Traversable, the raised exception say that some methods are missing while they don't. This PR check if the adder and setter methods exists and if so, give a exception that pointing on the real problem.

Commits
-------

c46519b [PropertyAccess][DX] Enhance exception that say that some methods are missing if they don't
  • Loading branch information
fabpot committed Jun 15, 2016
2 parents 347cd87 + c46519b commit d7e466a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Expand Up @@ -714,6 +714,17 @@ private function getWriteAccessInfo($class, $property, $value)
// 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(
Expand Down
Expand Up @@ -194,4 +194,15 @@ public function testIsWritableReturnsFalseIfNoAdderNorRemoverExists()

$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');
}
}

0 comments on commit d7e466a

Please sign in to comment.