From 5447b1b0737565463ffcb4288dda171540f37e17 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Tue, 20 Jun 2023 20:07:11 +0200 Subject: [PATCH] Test HasOneDeep relationships from third-party packages --- composer.json | 4 ++-- .../LaravelAdjacencyList/AncestorsTest.php | 12 ++++++++++++ .../LaravelAdjacencyList/DescendantsTest.php | 10 ++++++++++ .../LaravelAdjacencyList/Models/User.php | 17 +++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4c32bf7..a4a87c1 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,8 @@ "illuminate/pagination": "^9.0", "phpunit/phpunit": "^9.5", "staudenmeir/eloquent-eager-limit": "^1.7", - "staudenmeir/eloquent-json-relations": "^1.7", - "staudenmeir/laravel-adjacency-list": "^1.12", + "staudenmeir/eloquent-json-relations": "^1.7.2", + "staudenmeir/laravel-adjacency-list": "^1.12.5", "nesbot/carbon": "^2.62.1", "korridor/laravel-has-many-merged": "^0.0.3" }, diff --git a/tests/Concatenation/LaravelAdjacencyList/AncestorsTest.php b/tests/Concatenation/LaravelAdjacencyList/AncestorsTest.php index 721b53a..99cab71 100644 --- a/tests/Concatenation/LaravelAdjacencyList/AncestorsTest.php +++ b/tests/Concatenation/LaravelAdjacencyList/AncestorsTest.php @@ -50,6 +50,18 @@ public function testEagerLoadingAndSelf() $this->assertEquals([100, 110], $users[10]->ancestorAndSelfPosts->pluck('id')->all()); } + public function testEagerLoadingWithHasOneDeep() + { + $users = User::with([ + 'ancestorPost' => fn (HasManyDeep $query) => $query->orderBy('id'), + ])->get(); + + $this->assertNull($users[0]->ancestorPost); + $this->assertEquals(10, $users[1]->ancestorPost->id); + $this->assertEquals(10, $users[7]->ancestorPost->id); + $this->assertNull($users[10]->ancestorPost); + } + public function testLazyEagerLoading() { $users = User::all()->load([ diff --git a/tests/Concatenation/LaravelAdjacencyList/DescendantsTest.php b/tests/Concatenation/LaravelAdjacencyList/DescendantsTest.php index 78fb8b5..736c910 100644 --- a/tests/Concatenation/LaravelAdjacencyList/DescendantsTest.php +++ b/tests/Concatenation/LaravelAdjacencyList/DescendantsTest.php @@ -47,6 +47,16 @@ public function testEagerLoadingAndSelf() $this->assertEquals([100, 110], $users[9]->descendantPostsAndSelf->pluck('id')->all()); } + public function testEagerLoadingWithHasOneDeep() + { + $users = User::with('descendantPost')->get(); + + $this->assertEquals(20, $users[0]->descendantPost->id); + $this->assertEquals(50, $users[1]->descendantPost->id); + $this->assertNull($users[8]->descendantPost); + $this->assertEquals(100, $users[9]->descendantPost->id); + } + public function testLazyEagerLoading() { $users = User::all()->load('descendantPosts'); diff --git a/tests/Concatenation/LaravelAdjacencyList/Models/User.php b/tests/Concatenation/LaravelAdjacencyList/Models/User.php index 664ed98..0538086 100644 --- a/tests/Concatenation/LaravelAdjacencyList/Models/User.php +++ b/tests/Concatenation/LaravelAdjacencyList/Models/User.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Staudenmeir\EloquentHasManyDeep\HasManyDeep; +use Staudenmeir\EloquentHasManyDeep\HasOneDeep; use Staudenmeir\EloquentHasManyDeep\HasRelationships; use Staudenmeir\EloquentHasManyDeep\HasTableAlias; use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships; @@ -16,6 +17,14 @@ class User extends Model use HasTableAlias; use SoftDeletes; + public function ancestorPost(): HasOneDeep + { + return $this->hasOneDeepFromRelations( + $this->ancestors(), + (new static())->posts() + ); + } + public function ancestorPosts(): HasManyDeep { return $this->hasManyDeepFromRelations( @@ -40,6 +49,14 @@ public function bloodlinePosts(): HasManyDeep ); } + public function descendantPost(): HasOneDeep + { + return $this->hasOneDeepFromRelations( + $this->descendants(), + (new static())->posts() + ); + } + public function descendantPosts(): HasManyDeep { return $this->hasManyDeepFromRelations(