Skip to content

[12.x] Reduce repeated inserts in EloquentCursorPaginateTest #56543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 53 additions & 39 deletions tests/Integration/Database/EloquentCursorPaginateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,24 @@ protected function afterRefreshingDatabase()

public function testCursorPaginationOnTopOfColumns()
{
for ($i = 1; $i <= 50; $i++) {
TestPost::create([
for ($i = 1; $i <= 16; $i++) {
$posts[] = [
'title' => 'Title '.$i,
]);
];
}
TestPost::fillAndInsert($posts);

$this->assertCount(15, TestPost::cursorPaginate(15, ['id', 'title']));
}

public function testPaginationWithUnion()
{
TestPost::create(['title' => 'Hello world', 'user_id' => 1]);
TestPost::create(['title' => 'Goodbye world', 'user_id' => 2]);
TestPost::create(['title' => 'Howdy', 'user_id' => 3]);
TestPost::create(['title' => '4th', 'user_id' => 4]);
TestPost::fillAndInsert([
['title' => 'Hello world', 'user_id' => 1],
['title' => 'Goodbye world', 'user_id' => 2],
['title' => 'Howdy', 'user_id' => 3],
['title' => '4th', 'user_id' => 4],
]);

$table1 = TestPost::query()->whereIn('user_id', [1, 2]);
$table2 = TestPost::query()->whereIn('user_id', [3, 4]);
Expand All @@ -57,9 +60,10 @@ public function testPaginationWithUnion()
public function testPaginationWithDistinct()
{
for ($i = 1; $i <= 3; $i++) {
TestPost::create(['title' => 'Hello world']);
TestPost::create(['title' => 'Goodbye world']);
$posts[] = ['title' => 'Hello world'];
$posts[] = ['title' => 'Goodbye world'];
}
TestPost::fillAndInsert($posts);

$query = TestPost::query()->distinct();

Expand All @@ -71,9 +75,10 @@ public function testPaginationWithDistinct()
public function testPaginationWithWhereClause()
{
for ($i = 1; $i <= 3; $i++) {
TestPost::create(['title' => 'Hello world', 'user_id' => null]);
TestPost::create(['title' => 'Goodbye world', 'user_id' => 2]);
$posts[] = ['title' => 'Hello world', 'user_id' => null];
$posts[] = ['title' => 'Goodbye world', 'user_id' => 2];
}
TestPost::fillAndInsert($posts);

$query = TestPost::query()->whereNull('user_id');

Expand All @@ -84,12 +89,14 @@ public function testPaginationWithWhereClause()

public function testPaginationWithHasClause()
{
TestUser::fillAndInsert([[], [], []]);

for ($i = 1; $i <= 3; $i++) {
TestUser::create();
TestPost::create(['title' => 'Hello world', 'user_id' => null]);
TestPost::create(['title' => 'Goodbye world', 'user_id' => 2]);
TestPost::create(['title' => 'Howdy', 'user_id' => 3]);
$posts[] = ['title' => 'Hello world', 'user_id' => null];
$posts[] = ['title' => 'Goodbye world', 'user_id' => 2];
$posts[] = ['title' => 'Howdy', 'user_id' => 3];
}
TestPost::fillAndInsert($posts);

$query = TestUser::query()->has('posts');

Expand All @@ -100,12 +107,13 @@ public function testPaginationWithHasClause()

public function testPaginationWithWhereHasClause()
{
TestUser::fillAndInsert([[], [], []]);
for ($i = 1; $i <= 3; $i++) {
TestUser::create();
TestPost::create(['title' => 'Hello world', 'user_id' => null]);
TestPost::create(['title' => 'Goodbye world', 'user_id' => 2]);
TestPost::create(['title' => 'Howdy', 'user_id' => 3]);
$posts[] = ['title' => 'Hello world', 'user_id' => null];
$posts[] = ['title' => 'Goodbye world', 'user_id' => 2];
$posts[] = ['title' => 'Howdy', 'user_id' => 3];
}
TestPost::fillAndInsert($posts);

$query = TestUser::query()->whereHas('posts', function ($query) {
$query->where('title', 'Howdy');
Expand All @@ -118,12 +126,13 @@ public function testPaginationWithWhereHasClause()

public function testPaginationWithWhereExistsClause()
{
TestUser::fillAndInsert([[], [], []]);
for ($i = 1; $i <= 3; $i++) {
TestUser::create();
TestPost::create(['title' => 'Hello world', 'user_id' => null]);
TestPost::create(['title' => 'Goodbye world', 'user_id' => 2]);
TestPost::create(['title' => 'Howdy', 'user_id' => 3]);
$posts[] = ['title' => 'Hello world', 'user_id' => null];
$posts[] = ['title' => 'Goodbye world', 'user_id' => 2];
$posts[] = ['title' => 'Howdy', 'user_id' => 3];
}
TestPost::fillAndInsert($posts);

$query = TestUser::query()->whereExists(function ($query) {
$query->select(DB::raw(1))
Expand All @@ -138,13 +147,14 @@ public function testPaginationWithWhereExistsClause()

public function testPaginationWithMultipleWhereClauses()
{
TestUser::fillAndInsert([[], [], [], []]);
for ($i = 1; $i <= 4; $i++) {
TestUser::create();
TestPost::create(['title' => 'Hello world', 'user_id' => null]);
TestPost::create(['title' => 'Goodbye world', 'user_id' => 2]);
TestPost::create(['title' => 'Howdy', 'user_id' => 3]);
TestPost::create(['title' => 'Howdy', 'user_id' => 4]);
$posts[] = ['title' => 'Hello world', 'user_id' => null];
$posts[] = ['title' => 'Goodbye world', 'user_id' => 2];
$posts[] = ['title' => 'Howdy', 'user_id' => 3];
$posts[] = ['title' => 'Howdy', 'user_id' => 4];
}
TestPost::fillAndInsert($posts);

$query = TestUser::query()->whereExists(function ($query) {
$query->select(DB::raw(1))
Expand All @@ -170,8 +180,10 @@ public function testPaginationWithMultipleWhereClauses()

public function testPaginationWithMultipleUnionAndMultipleWhereClauses()
{
TestPost::create(['title' => 'Post A', 'user_id' => 100]);
TestPost::create(['title' => 'Post B', 'user_id' => 101]);
TestPost::fillAndInsert([
['title' => 'Post A', 'user_id' => 100],
['title' => 'Post B', 'user_id' => 101],
]);

$table1 = TestPost::select(['id', 'title', 'user_id'])->where('user_id', 100);
$table2 = TestPost::select(['id', 'title', 'user_id'])->where('user_id', 101);
Expand All @@ -198,8 +210,10 @@ public function testPaginationWithMultipleUnionAndMultipleWhereClauses()

public function testPaginationWithMultipleAliases()
{
TestUser::create(['name' => 'A (user)']);
TestUser::create(['name' => 'C (user)']);
TestUser::fillAndInsert([
['name' => 'A (user)'],
['name' => 'C (user)'],
]);

TestPost::create(['title' => 'B (post)']);
TestPost::create(['title' => 'D (post)']);
Expand All @@ -224,9 +238,7 @@ public function testPaginationWithMultipleAliases()

public function testPaginationWithAliasedOrderBy()
{
for ($i = 1; $i <= 6; $i++) {
TestUser::create();
}
TestUser::fillAndInsert([[], [], [], [], [], []]);

$query = TestUser::query()->select('id as user_id')->orderBy('user_id');
$clonedQuery = $query->clone();
Expand All @@ -246,9 +258,10 @@ public function testPaginationWithAliasedOrderBy()
public function testPaginationWithDistinctColumnsAndSelect()
{
for ($i = 1; $i <= 3; $i++) {
TestPost::create(['title' => 'Hello world']);
TestPost::create(['title' => 'Goodbye world']);
$posts[] = ['title' => 'Hello world'];
$posts[] = ['title' => 'Goodbye world'];
}
TestPost::fillAndInsert($posts);

$query = TestPost::query()->orderBy('title')->distinct('title')->select('title');

Expand All @@ -263,12 +276,13 @@ public function testPaginationWithDistinctColumnsAndSelectAndJoin()
$user = TestUser::create();

for ($j = 1; $j <= 10; $j++) {
TestPost::create([
$posts[] = [
'title' => 'Title '.$i,
'user_id' => $user->id,
]);
];
}
}
TestPost::fillAndInsert($posts);

$query = TestUser::query()->join('test_posts', 'test_posts.user_id', '=', 'test_users.id')
->distinct('test_users.id')->select('test_users.*');
Expand Down
Loading