Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
einar-hansen committed Jul 25, 2024
1 parent 5db79a5 commit b58917d
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,120 @@ public function testOrWhereAny()
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testDynamicWhereAll()
{
$builder = $this->getBuilder();
$builder->select('*')->from('products')->whereAllBetween(['price', 'discounted_price'], [100, 200]);
$this->assertSame('select * from "products" where ("price" between ? and ? and "discounted_price" between ? and ?)', $builder->toSql());
$this->assertEquals([100, 200, 100, 200], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('products')->where('name', 'like', '%shirt%')->orWhereAllNotBetween(['price', 'discounted_price'], [100, 200]);
$this->assertSame('select * from "products" where "name" like ? or ("price" not between ? and ? and "discounted_price" not between ? and ?)', $builder->toSql());
$this->assertEquals(['%shirt%', 100, 200, 100, 200], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAllIn(['role', 'permission'], ['admin', 'moderator']);
$this->assertSame('select * from "users" where ("role" in (?, ?) and "permission" in (?, ?))', $builder->toSql());
$this->assertEquals(['admin', 'moderator', 'admin', 'moderator'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAllDate(['created_at', 'updated_at'], '2024-07-25');
$this->assertSame('select * from "users" where (date("created_at") = ? and date("updated_at") = ?)', $builder->toSql());
$this->assertEquals(['2024-07-25', '2024-07-25'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->orWhereAllNotIn(['id', 'parent_id'], [1, 2]);
$this->assertSame('select * from "users" where "name" = ? or ("id" not in (?, ?) and "parent_id" not in (?, ?))', $builder->toSql());
$this->assertEquals(['John', 1, 2, 1, 2], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAllLike(['last_name', 'email'], '%Doe%');
$this->assertSame('select * from "users" where ("last_name" like ? and "email" like ?)', $builder->toSql());
$this->assertEquals(['%Doe%', '%Doe%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->orWhereAllNotLike(['last_name', 'email'], '%Doe%', false);
$this->assertSame('select * from "users" where "name" = ? or ("last_name" not like ? and "email" not like ?)', $builder->toSql());
$this->assertEquals(['John', '%Doe%', '%Doe%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAllColumn(['id', 'parent_id'], '>=', 'id');
$this->assertSame('select * from "users" where ("id" >= "id" and "parent_id" >= "id")', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

public function testBuilderThrowsExpectedExceptionWithUndefinedWhereAllMethod()
{
$this->expectException(BadMethodCallException::class);

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->orWhereAllHelloWorld(['id']);

}

public function testDynamicWhereAny()
{
$builder = $this->getBuilder();
$builder->select('*')->from('products')->whereAnyNotBetween(['price', 'discounted_price'], [100, 200]);
$this->assertSame('select * from "products" where ("price" not between ? and ? or "discounted_price" not between ? and ?)', $builder->toSql());
$this->assertEquals([100, 200, 100, 200], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->whereAnyNotLike(['last_name', 'email'], '%Doe%');
$this->assertSame('select * from "users" where "name" = ? and ("last_name" not like ? or "email" not like ?)', $builder->toSql());
$this->assertEquals(['John', '%Doe%', '%Doe%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->orWhereAnyLike(['last_name', 'email'], '%Doe%');
$this->assertSame('select * from "users" where "name" = ? or ("last_name" like ? or "email" like ?)', $builder->toSql());
$this->assertEquals(['John', '%Doe%', '%Doe%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->orWhereAnyNotNull(['last_name', 'email']);
$this->assertSame('select * from "users" where "name" = ? or ("last_name" is not null or "email" is not null)', $builder->toSql());
$this->assertEquals(['John'], $builder->getBindings());
}

public function testBuilderThrowsExpectedExceptionWithUndefinedWhereAnyMethod()
{
$this->expectException(BadMethodCallException::class);

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->orWhereAnyNonExistingMethod(['id']);
}

public function testDynamicWhereNone()
{
$builder = $this->getBuilder();
$builder->select('*')->from('products')->whereNoneBetween(['price', 'discounted_price'], [100, 200]);
$this->assertSame('select * from "products" where not ("price" between ? and ? or "discounted_price" between ? and ?)', $builder->toSql());
$this->assertEquals([100, 200, 100, 200], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->whereNoneLike(['last_name', 'email'], '%Doe%');
$this->assertSame('select * from "users" where "name" = ? and not ("last_name" like ? or "email" like ?)', $builder->toSql());
$this->assertEquals(['John', '%Doe%', '%Doe%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->orWhereNoneIn(['id', 'parent_id'], [1, 2, 3]);
$this->assertSame('select * from "users" where "name" = ? or not ("id" in (?, ?, ?) or "parent_id" in (?, ?, ?))', $builder->toSql());
$this->assertEquals(['John', 1,2,3,1,2,3], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->orWhereNoneNull(['last_name', 'email']);
$this->assertSame('select * from "users" where "name" = ? or not ("last_name" is null or "email" is null)', $builder->toSql());
$this->assertEquals(['John'], $builder->getBindings());
}

public function testBuilderThrowsExpectedExceptionWithUndefinedWhereNoneMethod()
{
$this->expectException(BadMethodCallException::class);

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('name', 'John')->orWhereNonRandomString(['id']);
}

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

0 comments on commit b58917d

Please sign in to comment.