Skip to content

Commit

Permalink
Fix for dynamic methods defined as [class, method] (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin authored Dec 23, 2024
1 parent f044001 commit 91eaa7b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Database/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,12 @@ protected function getRelationMethodType(string $name, bool $ignoreResolved = fa
$object = $this->extensionData['extensions'][$extension];
$method = new \ReflectionMethod($object, $name);
} elseif (isset($this->extensionData['dynamicMethods'][$name])) {
$method = new \ReflectionFunction($this->extensionData['dynamicMethods'][$name]->getClosure());
$dynamicMethod = $this->extensionData['dynamicMethods'][$name];
if (is_array($dynamicMethod)) {
$method = new \ReflectionMethod(...$dynamicMethod);
} else {
$method = new \ReflectionFunction($dynamicMethod->getClosure());
}
} else {
if (!isset(static::$resolvedNonRelationMethods[static::class])) {
static::$resolvedNonRelationMethods[static::class] = [$name];
Expand Down
24 changes: 24 additions & 0 deletions tests/Database/Concerns/HasRelationshipsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ public function testHasRelation()
$this->assertFalse($author->hasRelation('invalid'));
}

public function testRelationDynamicMethods()
{
$author = new Author();

$author->addDynamicMethod('dynamicClassMethodFromArray', [$author, 'messages']);
$this->assertTrue($author->hasRelation('dynamicClassMethodFromArray'));
$this->assertEquals('hasMany', $author->getRelationType('dynamicClassMethodFromArray'));
}

public function testNonRelationDynamicMethods()
{
$author = new Author();

$author->addDynamicMethod('dynamicMethodAsClosure', function () {
});
$this->assertFalse($author->hasRelation('dynamicMethodAsClosure'));

$author->addDynamicMethod('dynamicStaticClassMethodFromArray', [Author::class, 'hasDatabaseTable']);
$this->assertFalse($author->hasRelation('dynamicStaticClassMethodFromArray'));

$author->addDynamicMethod('dynamicClassMethodFromArray', [$author, 'isDatabaseReady']);
$this->assertFalse($author->hasRelation('dynamicClassMethodFromArray'));
}

public function testGetRelationType()
{
$author = new Author();
Expand Down

0 comments on commit 91eaa7b

Please sign in to comment.