From b58917dbac001264187a5ec2188d3c4be69ff480 Mon Sep 17 00:00:00 2001 From: Einar Hansen <49709354+einar-hansen@users.noreply.github.com> Date: Thu, 25 Jul 2024 15:55:45 +0200 Subject: [PATCH] Add tests --- tests/Database/DatabaseQueryBuilderTest.php | 114 ++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index c345ffcfb5c8..6c9eb1fd7421 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -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();