Skip to content

Commit

Permalink
Add query builder test for lateral join
Browse files Browse the repository at this point in the history
  • Loading branch information
Bakke committed Feb 12, 2024
1 parent 86413c5 commit 00560b4
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,70 @@ public function testRightJoinSub()
$builder->from('users')->rightJoinSub(['foo'], 'sub', 'users.id', '=', 'sub.id');
}

public function testJoinLateral()
{
$builder = $this->getBuilder();
$builder->from('users')->joinLateral('select * from "contacts" where "contracts"."user_id" = "users"."id"', 'sub');
$this->assertSame('select * from "users" inner join lateral (select * from "contacts" where "contracts"."user_id" = "users"."id") as "sub" on true', $builder->toSql());

$builder = $this->getBuilder();
$builder->from('users')->joinLateral(function ($q) {
$q->from('contacts')->whereColumn('contracts.user_id', 'users.id');
}, 'sub');
$this->assertSame('select * from "users" inner join lateral (select * from "contacts" where "contracts"."user_id" = "users"."id") as "sub" on true', $builder->toSql());

$builder = $this->getBuilder();
$eloquentBuilder = new EloquentBuilder($this->getBuilder()->from('contacts')->whereColumn('contracts.user_id', 'users.id'));
$builder->from('users')->joinLateral($eloquentBuilder, 'sub');
$this->assertSame('select * from "users" inner join lateral (select * from "contacts" where "contracts"."user_id" = "users"."id") as "sub" on true', $builder->toSql());

$builder = $this->getBuilder();
$sub1 = $this->getBuilder()->from('contacts')->whereColumn('contracts.user_id', 'users.id')->where('name', 'foo');
$sub2 = $this->getBuilder()->from('contacts')->whereColumn('contracts.user_id', 'users.id')->where('name', 'bar');
$builder->from('users')
->joinLateral($sub1, 'sub1')
->joinLateral($sub2, 'sub2');
$expected = 'select * from "users" ';
$expected .= 'inner join lateral (select * from "contacts" where "contracts"."user_id" = "users"."id" and "name" = ?) as "sub1" on true ';
$expected .= 'inner join lateral (select * from "contacts" where "contracts"."user_id" = "users"."id" and "name" = ?) as "sub2" on true';
$this->assertEquals($expected, $builder->toSql());
$this->assertEquals(['foo', 'bar'], $builder->getRawBindings()['join']);

$this->expectException(InvalidArgumentException::class);
$builder = $this->getBuilder();
$builder->from('users')->joinLateral(['foo'], 'sub');
}

public function testJoinLateralWithPrefix()
{
$builder = $this->getBuilder();
$builder->getGrammar()->setTablePrefix('prefix_');
$builder->from('users')->joinLateral('select * from "contacts" where "contracts"."user_id" = "users"."id"', 'sub');
$this->assertSame('select * from "prefix_users" inner join lateral (select * from "contacts" where "contracts"."user_id" = "users"."id") as "prefix_sub" on true', $builder->toSql());
}

public function testLeftJoinLateral()
{
$builder = $this->getBuilder();
$builder->from('users')->leftJoinLateral($this->getBuilder()->from('contacts')->whereColumn('contracts.user_id', 'users.id'), 'sub');
$this->assertSame('select * from "users" left join lateral (select * from "contacts" where "contracts"."user_id" = "users"."id") as "sub" on true', $builder->toSql());

$this->expectException(InvalidArgumentException::class);
$builder = $this->getBuilder();
$builder->from('users')->leftJoinLateral(['foo'], 'sub');
}

public function testRightJoinLateral()
{
$builder = $this->getBuilder();
$builder->from('users')->rightJoinLateral($this->getBuilder()->from('contacts')->whereColumn('contracts.user_id', 'users.id'), 'sub');
$this->assertSame('select * from "users" right join lateral (select * from "contacts" where "contracts"."user_id" = "users"."id") as "sub" on true', $builder->toSql());

$this->expectException(InvalidArgumentException::class);
$builder = $this->getBuilder();
$builder->from('users')->rightJoinLateral(['foo'], 'sub');
}

public function testRawExpressionsInSelect()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit 00560b4

Please sign in to comment.