|
2 | 2 |
|
3 | 3 | namespace Kirschbaum\PowerJoins\Tests; |
4 | 4 |
|
5 | | -use Kirschbaum\PowerJoins\Tests\Models\Comment; |
6 | | -use Kirschbaum\PowerJoins\Tests\Models\Group; |
7 | 5 | use Kirschbaum\PowerJoins\Tests\Models\Post; |
8 | 6 | use Kirschbaum\PowerJoins\Tests\Models\User; |
| 7 | +use Kirschbaum\PowerJoins\Tests\Models\Group; |
| 8 | +use Kirschbaum\PowerJoins\Tests\Models\Image; |
| 9 | +use Kirschbaum\PowerJoins\Tests\Models\Comment; |
9 | 10 |
|
10 | 11 | class PowerJoinHasTest extends TestCase |
11 | 12 | { |
@@ -171,4 +172,29 @@ public function test_where_has_with_joins_on_belongs_to_many_relationship() |
171 | 172 | ); |
172 | 173 | $this->assertCount(1, $powerJoinQuery->get()); |
173 | 174 | } |
| 175 | + |
| 176 | + public function test_power_join_has_with_morph_to() |
| 177 | + { |
| 178 | + $post = factory(Post::class)->state('published')->create(); |
| 179 | + $postImage = factory(Image::class)->state('owner:post')->create(['imageable_id' => $post->id]); |
| 180 | + $user = factory(Post::class)->create(); |
| 181 | + $userImage = factory(Image::class)->state('owner:user')->create(['imageable_id' => $user->id]); |
| 182 | + |
| 183 | + $postImagesQueried = Image::query() |
| 184 | + ->powerJoinHas('imageable', morphable: Post::class) |
| 185 | + ->get(); |
| 186 | + |
| 187 | + $userImagesQueried = Image::query() |
| 188 | + ->powerJoinHas('imageable', morphable: User::class) |
| 189 | + ->get(); |
| 190 | + |
| 191 | + $this->assertCount(1, $postImagesQueried); |
| 192 | + $this->assertCount(1, Image::powerJoinHas('imageable', morphable: Post::class, callback: fn ($query) => $query->where('posts.published', true))->get()); |
| 193 | + $this->assertCount(0, Image::powerJoinHas('imageable', morphable: Post::class, callback: fn ($query) => $query->where('posts.published', false))->get()); |
| 194 | + $this->assertCount(0, Image::powerJoinHas('imageable', count: 2, morphable: Post::class)->get()); |
| 195 | + $this->assertTrue($postImage->is($postImagesQueried->sole())); |
| 196 | + |
| 197 | + $this->assertCount(1, $userImagesQueried); |
| 198 | + $this->assertTrue($userImage->is($userImagesQueried->sole())); |
| 199 | + } |
174 | 200 | } |
0 commit comments