Skip to content
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

[8.x] Handle cursor paginator when no items are found #42963

Merged
merged 2 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/Illuminate/Pagination/AbstractCursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public function previousCursor()
return null;
}

if ($this->items->isEmpty()) {
return null;
}

return $this->getCursorForItem($this->items->first(), false);
}

Expand All @@ -170,6 +174,10 @@ public function nextCursor()
return null;
}

if ($this->items->isEmpty()) {
return null;
}

return $this->getCursorForItem($this->items->last(), true);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Pagination/CursorPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Pagination\Cursor;
use Illuminate\Pagination\CursorPaginator;
use Illuminate\Support\Collection;
use PHPUnit\Framework\TestCase;

class CursorPaginatorTest extends TestCase
Expand Down Expand Up @@ -76,6 +77,26 @@ public function testCanTransformPaginatorItems()
$this->assertSame([['id' => 6], ['id' => 7]], $p->items());
}

public function testReturnEmptyCursorWhenItemsAreEmpty()
{
$cursor = new Cursor(['id' => 25], true);

$p = new CursorPaginator(Collection::make(), 25, $cursor, [
'path' => 'http://website.com/test',
'cursorName' => 'cursor',
'parameters' => ['id'],
]);

$this->assertInstanceOf(CursorPaginator::class, $p);
$this->assertSame([
'data' => [],
'path' => 'http://website.com/test',
'per_page' => 25,
'next_page_url' => null,
'prev_page_url' => null,
], $p->toArray());
}

protected function getCursor($params, $isNext = true)
{
return (new Cursor($params, $isNext))->encode();
Expand Down