Skip to content

Commit

Permalink
[11.x] Fix crash of method PreventsCircularRecursion::withoutRecursio…
Browse files Browse the repository at this point in the history
…n() on mocked models (#52943)

* fix: verify that onceable return is not null before accessing attribute

* chore: fix code style
  • Loading branch information
maximetassy committed Sep 27, 2024
1 parent d1a7bc9 commit ef9113e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ protected function withoutRecursion($callback, $default = null)

$onceable = Onceable::tryFromTrace($trace, $callback);

if (is_null($onceable)) {
return call_user_func($callback);
}

$stack = static::getRecursiveCallStack($this);

if (array_key_exists($onceable->hash, $stack)) {
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseConcernsPreventsCircularRecursionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Database;

use Illuminate\Database\Eloquent\Concerns\PreventsCircularRecursion;
use Mockery;
use PHPUnit\Framework\TestCase;

class DatabaseConcernsPreventsCircularRecursionTest extends TestCase
Expand Down Expand Up @@ -172,6 +173,18 @@ public function testRecursiveCallsToCircularLinkedListCallsEachInstanceOnce()
$this->assertEquals(3, $second->instanceStack);
$this->assertEquals(3, $third->instanceStack);
}

public function testMockedModelCallToWithoutRecursionMethodWorks(): void
{
$mock = Mockery::mock(TestModel::class)->makePartial();

// Model toArray method implementation
$toArray = $mock->withoutRecursion(
fn () => array_merge($mock->attributesToArray(), $mock->relationsToArray()),
fn () => $mock->attributesToArray(),
);
$this->assertEquals([], $toArray);
}
}

class PreventsCircularRecursionWithRecursiveMethod
Expand Down

0 comments on commit ef9113e

Please sign in to comment.