Skip to content

Commit

Permalink
Test HasOneDeep relationships from third-party packages
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Jun 20, 2023
1 parent 018dfa2 commit 5447b1b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
12 changes: 12 additions & 0 deletions tests/Concatenation/LaravelAdjacencyList/AncestorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
10 changes: 10 additions & 0 deletions tests/Concatenation/LaravelAdjacencyList/DescendantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
17 changes: 17 additions & 0 deletions tests/Concatenation/LaravelAdjacencyList/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 5447b1b

Please sign in to comment.