Skip to content

Commit d2a5755

Browse files
authored
Merge pull request #45 from tortuetorche/add-complex-nested-joins-test
Add a complex nested joins test
2 parents d981723 + 1cf4932 commit d2a5755

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/JoinRelationshipTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,57 @@ public function test_it_doesnt_join_the_same_relationship_twice_with_nested()
303303
);
304304
}
305305

306+
/** @test */
307+
public function test_it_doesnt_join_the_same_relationship_twice_with_complex_nested()
308+
{
309+
$query = User::query()
310+
->select('users.*')
311+
->leftJoinRelationship('posts')
312+
->rightJoinRelationship('posts.comments')
313+
->leftJoinRelationship('posts.images')
314+
->joinRelationship('posts.category')
315+
->leftJoinRelationship('posts.category.parent', [
316+
'parent' => function ($join) {
317+
$join->as('category_parent');
318+
},
319+
])
320+
->toSql();
321+
322+
// making sure it doesn't throw any errors
323+
User::query()->select('users.*')->joinRelationship('posts.comments')->joinRelationship('posts.images')->get();
324+
325+
$this->assertStringContainsString(
326+
'left join "posts" on "posts"."user_id" = "users"."id"',
327+
$query
328+
);
329+
330+
$this->assertEquals(
331+
1,
332+
substr_count($query, 'left join "posts" on "posts"."user_id" = "users"."id"'),
333+
'It should only make 1 join with the posts table'
334+
);
335+
336+
$this->assertStringContainsString(
337+
'right join "comments" on "comments"."post_id" = "posts"."id"',
338+
$query
339+
);
340+
341+
$this->assertStringContainsString(
342+
'left join "images" on "images"."imageable_id" = "posts"."id" and "imageable_type" = ?',
343+
$query
344+
);
345+
346+
$this->assertStringContainsString(
347+
'inner join "categories" on "posts"."category_id" = "categories"."id"',
348+
$query
349+
);
350+
351+
$this->assertStringContainsString(
352+
'left join "categories" as "category_parent" on "categories"."parent_id" = "category_parent"."id"',
353+
$query
354+
);
355+
}
356+
306357
/** @test */
307358
public function test_it_join_belongs_to_relationship()
308359
{

0 commit comments

Comments
 (0)