Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cellard committed Oct 24, 2024
1 parent 13802fc commit 01aaed5
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1322,29 +1322,48 @@ public function testWhereHasPivot()

DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'foo'],
['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'rab'],
]);

$relationTag = $post->tags()->whereHasPivot('posts',
fn(BelongsToMany $builder) => $builder->wherePivot('flag', 'foo')
)->first();

$this->assertEquals($relationTag->getAttributes(), $tag->getAttributes());

$relationTag = $post->tags()->whereHasPivot('posts',
fn(BelongsToMany $builder) => $builder->wherePivotIn('flag', ['bar', 'rab'])
)->first();

$this->assertEquals($relationTag->getAttributes(), $tag->getAttributes());

$relationTag = $post->tags()->whereHasPivot('posts',
fn(BelongsToMany $builder) => $builder->wherePivotNull('flag')
)->first();

$this->assertNull($relationTag);
}

public function testWhereHasPivotForwardCall()
{
$tag = Tag::create(['name' => Str::random()])->fresh();
$post = Post::create(['title' => Str::random()]);
$tag = Tag::create(['id' => 1, 'name' => Str::random()])->fresh();
$post = Post::create(['id' => 2, 'title' => Str::random()]);

DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'foo'],
]);

$relationTag = $post->tags()->whereHasPivot('posts',
fn(BelongsToMany $builder) => $builder->whereKey($tag->getKey())
fn(BelongsToMany $builder) => $builder->whereKey(2)
)->first();

$this->assertEquals($relationTag->getAttributes(), $tag->getAttributes());

$relationTag = $post->tags()->whereHasPivot('posts',
fn(BelongsToMany $builder) => $builder->whereKey(1)
)->first();

$this->assertNull($relationTag);
}
}

Expand Down

0 comments on commit 01aaed5

Please sign in to comment.