Skip to content

Commit 067606e

Browse files
committed
Add test case asserting forPageAfterId nests existing where clause
1 parent 66b8225 commit 067606e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/Database/DatabaseEloquentIntegrationTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,23 @@ public function testForPageBeforeIdCorrectlyPaginates()
11051105
$this->assertEquals(1, $results->first()->id);
11061106
}
11071107

1108+
public function testForPageBeforeIdCorrectlyNestExistingWhereClauses()
1109+
{
1110+
EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
1111+
EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
1112+
EloquentTestUser::create(['id' => 3, 'email' => 'jamesotwell@gmail.com']);
1113+
EloquentTestUser::create(['id' => 4, 'email' => 'victoriaotwell@gmail.com']);
1114+
1115+
// Without nesting the query, it would generate something along the lines
1116+
// of `WHERE id = 4 OR (id = 1 AND id < 3)`, which would return 2 rows,
1117+
// When correctly nested, it generates a proper where clause, such as:
1118+
// `WHERE (id = 4 OR id = 1) AND id < 3`, which returns a single row
1119+
$results = EloquentTestUser::where('id', 4)->orWhere('id', 1)->forPageBeforeId(15, 3);
1120+
$this->assertInstanceOf(Builder::class, $results);
1121+
$this->assertEquals(1, $results->count());
1122+
$this->assertEquals(1, $results->first()->id);
1123+
}
1124+
11081125
public function testForPageAfterIdCorrectlyPaginates()
11091126
{
11101127
EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
@@ -1119,6 +1136,20 @@ public function testForPageAfterIdCorrectlyPaginates()
11191136
$this->assertEquals(2, $results->first()->id);
11201137
}
11211138

1139+
public function testForPageAfterIdCorrectlyNestExistingWhereClauses()
1140+
{
1141+
EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
1142+
EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
1143+
1144+
// Without nesting the query, it would generate something along the lines
1145+
// of `WHERE id = 1 OR (id = 2 AND id > 1)`, which would always return 1.
1146+
// When correctly nested, it generates a proper where clause, such as:
1147+
// `WHERE (id = 1 OR id = 2) AND id > 1`
1148+
$results = EloquentTestUser::where('id', 1)->orWhere('id', 2)->forPageAfterId(15, 1);
1149+
$this->assertInstanceOf(Builder::class, $results);
1150+
$this->assertEquals(2, $results->first()->id);
1151+
}
1152+
11221153
public function testMorphToRelationsAcrossDatabaseConnections()
11231154
{
11241155
$item = null;

0 commit comments

Comments
 (0)