Skip to content

Commit 2a381e7

Browse files
committed
Add tests
1 parent 01dd3ca commit 2a381e7

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,120 @@ public function testOrWhereAny()
13761376
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
13771377
}
13781378

1379+
public function testDynamicWhereAll()
1380+
{
1381+
$builder = $this->getBuilder();
1382+
$builder->select('*')->from('products')->whereAllBetween(['price', 'discounted_price'], [100, 200]);
1383+
$this->assertSame('select * from "products" where ("price" between ? and ? and "discounted_price" between ? and ?)', $builder->toSql());
1384+
$this->assertEquals([100, 200, 100, 200], $builder->getBindings());
1385+
1386+
$builder = $this->getBuilder();
1387+
$builder->select('*')->from('products')->where('name', 'like', '%shirt%')->orWhereAllNotBetween(['price', 'discounted_price'], [100, 200]);
1388+
$this->assertSame('select * from "products" where "name" like ? or ("price" not between ? and ? and "discounted_price" not between ? and ?)', $builder->toSql());
1389+
$this->assertEquals(['%shirt%', 100, 200, 100, 200], $builder->getBindings());
1390+
1391+
$builder = $this->getBuilder();
1392+
$builder->select('*')->from('users')->whereAllIn(['role', 'permission'], ['admin', 'moderator']);
1393+
$this->assertSame('select * from "users" where ("role" in (?, ?) and "permission" in (?, ?))', $builder->toSql());
1394+
$this->assertEquals(['admin', 'moderator', 'admin', 'moderator'], $builder->getBindings());
1395+
1396+
$builder = $this->getBuilder();
1397+
$builder->select('*')->from('users')->whereAllDate(['created_at', 'updated_at'], '2024-07-25');
1398+
$this->assertSame('select * from "users" where (date("created_at") = ? and date("updated_at") = ?)', $builder->toSql());
1399+
$this->assertEquals(['2024-07-25', '2024-07-25'], $builder->getBindings());
1400+
1401+
$builder = $this->getBuilder();
1402+
$builder->select('*')->from('users')->where('name', 'John')->orWhereAllNotIn(['id', 'parent_id'], [1, 2]);
1403+
$this->assertSame('select * from "users" where "name" = ? or ("id" not in (?, ?) and "parent_id" not in (?, ?))', $builder->toSql());
1404+
$this->assertEquals(['John', 1, 2, 1, 2], $builder->getBindings());
1405+
1406+
$builder = $this->getBuilder();
1407+
$builder->select('*')->from('users')->whereAllLike(['last_name', 'email'], '%Doe%');
1408+
$this->assertSame('select * from "users" where ("last_name" like ? and "email" like ?)', $builder->toSql());
1409+
$this->assertEquals(['%Doe%', '%Doe%'], $builder->getBindings());
1410+
1411+
$builder = $this->getBuilder();
1412+
$builder->select('*')->from('users')->where('name', 'John')->orWhereAllNotLike(['last_name', 'email'], '%Doe%', false);
1413+
$this->assertSame('select * from "users" where "name" = ? or ("last_name" not like ? and "email" not like ?)', $builder->toSql());
1414+
$this->assertEquals(['John', '%Doe%', '%Doe%'], $builder->getBindings());
1415+
1416+
$builder = $this->getBuilder();
1417+
$builder->select('*')->from('users')->whereAllColumn(['id', 'parent_id'], '>=', 'id');
1418+
$this->assertSame('select * from "users" where ("id" >= "id" and "parent_id" >= "id")', $builder->toSql());
1419+
$this->assertEquals([], $builder->getBindings());
1420+
}
1421+
1422+
public function testBuilderThrowsExpectedExceptionWithUndefinedWhereAllMethod()
1423+
{
1424+
$this->expectException(BadMethodCallException::class);
1425+
1426+
$builder = $this->getBuilder();
1427+
$builder->select('*')->from('users')->where('name', 'John')->orWhereAllHelloWorld(['id']);
1428+
1429+
}
1430+
1431+
public function testDynamicWhereAny()
1432+
{
1433+
$builder = $this->getBuilder();
1434+
$builder->select('*')->from('products')->whereAnyNotBetween(['price', 'discounted_price'], [100, 200]);
1435+
$this->assertSame('select * from "products" where ("price" not between ? and ? or "discounted_price" not between ? and ?)', $builder->toSql());
1436+
$this->assertEquals([100, 200, 100, 200], $builder->getBindings());
1437+
1438+
$builder = $this->getBuilder();
1439+
$builder->select('*')->from('users')->where('name', 'John')->whereAnyNotLike(['last_name', 'email'], '%Doe%');
1440+
$this->assertSame('select * from "users" where "name" = ? and ("last_name" not like ? or "email" not like ?)', $builder->toSql());
1441+
$this->assertEquals(['John', '%Doe%', '%Doe%'], $builder->getBindings());
1442+
1443+
$builder = $this->getBuilder();
1444+
$builder->select('*')->from('users')->where('name', 'John')->orWhereAnyLike(['last_name', 'email'], '%Doe%');
1445+
$this->assertSame('select * from "users" where "name" = ? or ("last_name" like ? or "email" like ?)', $builder->toSql());
1446+
$this->assertEquals(['John', '%Doe%', '%Doe%'], $builder->getBindings());
1447+
1448+
$builder = $this->getBuilder();
1449+
$builder->select('*')->from('users')->where('name', 'John')->orWhereAnyNotNull(['last_name', 'email']);
1450+
$this->assertSame('select * from "users" where "name" = ? or ("last_name" is not null or "email" is not null)', $builder->toSql());
1451+
$this->assertEquals(['John'], $builder->getBindings());
1452+
}
1453+
1454+
public function testBuilderThrowsExpectedExceptionWithUndefinedWhereAnyMethod()
1455+
{
1456+
$this->expectException(BadMethodCallException::class);
1457+
1458+
$builder = $this->getBuilder();
1459+
$builder->select('*')->from('users')->where('name', 'John')->orWhereAnyNonExistingMethod(['id']);
1460+
}
1461+
1462+
public function testDynamicWhereNone()
1463+
{
1464+
$builder = $this->getBuilder();
1465+
$builder->select('*')->from('products')->whereNoneBetween(['price', 'discounted_price'], [100, 200]);
1466+
$this->assertSame('select * from "products" where not ("price" between ? and ? or "discounted_price" between ? and ?)', $builder->toSql());
1467+
$this->assertEquals([100, 200, 100, 200], $builder->getBindings());
1468+
1469+
$builder = $this->getBuilder();
1470+
$builder->select('*')->from('users')->where('name', 'John')->whereNoneLike(['last_name', 'email'], '%Doe%');
1471+
$this->assertSame('select * from "users" where "name" = ? and not ("last_name" like ? or "email" like ?)', $builder->toSql());
1472+
$this->assertEquals(['John', '%Doe%', '%Doe%'], $builder->getBindings());
1473+
1474+
$builder = $this->getBuilder();
1475+
$builder->select('*')->from('users')->where('name', 'John')->orWhereNoneIn(['id', 'parent_id'], [1, 2, 3]);
1476+
$this->assertSame('select * from "users" where "name" = ? or not ("id" in (?, ?, ?) or "parent_id" in (?, ?, ?))', $builder->toSql());
1477+
$this->assertEquals(['John', 1,2,3,1,2,3], $builder->getBindings());
1478+
1479+
$builder = $this->getBuilder();
1480+
$builder->select('*')->from('users')->where('name', 'John')->orWhereNoneNull(['last_name', 'email']);
1481+
$this->assertSame('select * from "users" where "name" = ? or not ("last_name" is null or "email" is null)', $builder->toSql());
1482+
$this->assertEquals(['John'], $builder->getBindings());
1483+
}
1484+
1485+
public function testBuilderThrowsExpectedExceptionWithUndefinedWhereNoneMethod()
1486+
{
1487+
$this->expectException(BadMethodCallException::class);
1488+
1489+
$builder = $this->getBuilder();
1490+
$builder->select('*')->from('users')->where('name', 'John')->orWhereNonRandomString(['id']);
1491+
}
1492+
13791493
public function testUnions()
13801494
{
13811495
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)