Skip to content

Commit ef9113e

Browse files
authored
[11.x] Fix crash of method PreventsCircularRecursion::withoutRecursion() on mocked models (#52943)
* fix: verify that onceable return is not null before accessing attribute * chore: fix code style
1 parent d1a7bc9 commit ef9113e

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/Illuminate/Database/Eloquent/Concerns/PreventsCircularRecursion.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ protected function withoutRecursion($callback, $default = null)
2828

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

31+
if (is_null($onceable)) {
32+
return call_user_func($callback);
33+
}
34+
3135
$stack = static::getRecursiveCallStack($this);
3236

3337
if (array_key_exists($onceable->hash, $stack)) {

tests/Database/DatabaseConcernsPreventsCircularRecursionTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Database;
44

55
use Illuminate\Database\Eloquent\Concerns\PreventsCircularRecursion;
6+
use Mockery;
67
use PHPUnit\Framework\TestCase;
78

89
class DatabaseConcernsPreventsCircularRecursionTest extends TestCase
@@ -172,6 +173,18 @@ public function testRecursiveCallsToCircularLinkedListCallsEachInstanceOnce()
172173
$this->assertEquals(3, $second->instanceStack);
173174
$this->assertEquals(3, $third->instanceStack);
174175
}
176+
177+
public function testMockedModelCallToWithoutRecursionMethodWorks(): void
178+
{
179+
$mock = Mockery::mock(TestModel::class)->makePartial();
180+
181+
// Model toArray method implementation
182+
$toArray = $mock->withoutRecursion(
183+
fn () => array_merge($mock->attributesToArray(), $mock->relationsToArray()),
184+
fn () => $mock->attributesToArray(),
185+
);
186+
$this->assertEquals([], $toArray);
187+
}
175188
}
176189

177190
class PreventsCircularRecursionWithRecursiveMethod

0 commit comments

Comments
 (0)