From 6157be0a1d7c4fc38c6cd3311ad305aa7aebadb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Garc=C3=ADa=20Sanz?= Date: Tue, 1 Apr 2014 11:04:09 +0200 Subject: [PATCH] Wrong number of parameters Wrong number of parameters Wrong number of parameters Wrong number of parameters --- .../PropertyAccess/PropertyAccessor.php | 6 ++-- .../Tests/Fixtures/TestClass.php | 36 +++++++++++++++++++ .../Tests/PropertyAccessorTest.php | 10 ++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index c52b598cb9..a3e6c8a5dc 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -570,7 +570,7 @@ class PropertyAccessor implements PropertyAccessorInterface } /** - * Returns whether a method is public and has a specific number of required parameters. + * Returns whether a method is public and has the number of required parameters. * * @param \ReflectionClass $class The class of the method * @param string $methodName The method name @@ -584,7 +584,9 @@ class PropertyAccessor implements PropertyAccessorInterface if ($class->hasMethod($methodName)) { $method = $class->getMethod($methodName); - if ($method->isPublic() && $method->getNumberOfRequiredParameters() === $parameters) { + if ($method->isPublic() + && $method->getNumberOfRequiredParameters() <= $parameters + && $method->getNumberOfParameters() >= $parameters) { return true; } } diff --git a/src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php b/src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php index 178d7f618a..9765c77da2 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php +++ b/src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php @@ -18,6 +18,9 @@ class TestClass private $privateProperty; private $publicAccessor; + private $publicAccessorWithDefaultValue; + private $publicAccessorWithRequiredAndDefaultValue; + private $publicAccessorWithMoreRequiredParameters; private $publicIsAccessor; private $publicHasAccessor; @@ -25,6 +28,9 @@ class TestClass { $this->publicProperty = $value; $this->publicAccessor = $value; + $this->publicAccessorWithDefaultValue = $value; + $this->publicAccessorWithRequiredAndDefaultValue = $value; + $this->publicAccessorWithMoreRequiredParameters = $value; $this->publicIsAccessor = $value; $this->publicHasAccessor = $value; } @@ -34,11 +40,41 @@ class TestClass $this->publicAccessor = $value; } + public function setPublicAccessorWithDefaultValue($value = null) + { + $this->publicAccessorWithDefaultValue = $value; + } + + public function setPublicAccessorWithRequiredAndDefaultValue($value, $optional = null) + { + $this->publicAccessorWithRequiredAndDefaultValue = $value; + } + + public function setPublicAccessorWithMoreRequiredParameters($value, $needed) + { + $this->publicAccessorWithMoreRequiredParameters = $value; + } + public function getPublicAccessor() { return $this->publicAccessor; } + public function getPublicAccessorWithDefaultValue() + { + return $this->publicAccessorWithDefaultValue; + } + + public function getPublicAccessorWithRequiredAndDefaultValue() + { + return $this->publicAccessorWithRequiredAndDefaultValue; + } + + public function getPublicAccessorWithMoreRequiredParameters() + { + return $this->publicAccessorWithMoreRequiredParameters; + } + public function setPublicIsAccessor($value) { $this->publicIsAccessor = $value; diff --git a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php index a6b09fa0ab..6fc5f7022f 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php +++ b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php @@ -43,6 +43,8 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase // Accessor methods array(new TestClass('Bernhard'), 'publicProperty', 'Bernhard'), array(new TestClass('Bernhard'), 'publicAccessor', 'Bernhard'), + array(new TestClass('Bernhard'), 'publicAccessorWithDefaultValue', 'Bernhard'), + array(new TestClass('Bernhard'), 'publicAccessorWithRequiredAndDefaultValue', 'Bernhard'), array(new TestClass('Bernhard'), 'publicIsAccessor', 'Bernhard'), array(new TestClass('Bernhard'), 'publicHasAccessor', 'Bernhard'), @@ -250,6 +252,14 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase $this->assertEquals('Updated', $author->__get('magicProperty')); } + /** + * @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException + */ + public function testSetValueThrowsExceptionIfThereAreMissingParameters() + { + $this->propertyAccessor->setValue(new TestClass('Bernhard'), 'publicAccessorWithMoreRequiredParameters', 'Updated'); + } + /** * @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException */