Skip to content

Commit

Permalink
[BUGFIX] Support overloaded methods in StandardVariableProvider (#467)
Browse files Browse the repository at this point in the history
Switches method_exists to is_callable to support overloaded
methods that extract property values.
  • Loading branch information
RinyVT authored and NamelessCoder committed Jul 17, 2019
1 parent ae32cc8 commit d8ccbcd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Core/Variables/StandardVariableProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ protected function detectAccessor($subject, $propertyName)
if (is_object($subject)) {
$upperCasePropertyName = ucfirst($propertyName);
$getter = 'get' . $upperCasePropertyName;
if (method_exists($subject, $getter)) {
if (is_callable([$subject, $getter])) {
return self::ACCESSOR_GETTER;
}
if ($this->isExtractableThroughAsserter($subject, $propertyName)) {
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Core/Variables/StandardVariableProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\Tests\Unit\Core\Fixtures\ClassWithMagicGetter;
use TYPO3Fluid\Fluid\Tests\Unit\Core\Fixtures\ClassWithProtectedGetter;
use TYPO3Fluid\Fluid\Tests\Unit\ViewHelpers\Fixtures\UserWithoutToString;
use TYPO3Fluid\Fluid\Tests\UnitTestCase;

Expand Down Expand Up @@ -276,4 +278,26 @@ public function getExtractRedectAccessorTestValues()
[['test' => 'test'], 'test', StandardVariableProvider::ACCESSOR_ASSERTER, 'test'],
];
}

/**
* @test
*/
public function testExtractCallsMagicMethodGetters()
{
$provider = new StandardVariableProvider();
$provider->setSource(['object' => new ClassWithMagicGetter()]);
$result = $provider->get('object.test');
$this->assertEquals('test result', $result);
}

/**
* @test
*/
public function testExtractReturnsNullOnProtectedGetters()
{
$provider = new StandardVariableProvider();
$provider->setSource(['object' => new ClassWithProtectedGetter()]);
$result = $provider->get('object.test');
$this->assertEquals(null, $result);
}
}

0 comments on commit d8ccbcd

Please sign in to comment.